有没有一种方法可以旋转这些图像,去掉背景空白或任何背景,然后得到这样的图像
我试图删除背景如果图像没有任何旋转,我可以删除背景空白使用这个脚本,但如果图像有任何旋转,它不会删除任何空间 我跟着这个How to crop or remove white background from an image
import cv2
import numpy as np
img = cv2.imread('cheque_img\rotate.PNG')
## (1) Convert to gray, and threshold
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th, threshed = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)
## (2) Morph-op to remove noise
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11,11))
morphed = cv2.morphologyEx(threshed, cv2.MORPH_CLOSE, kernel)
## (3) Find the max-area contour
cnts = cv2.findContours(morphed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
cnt = sorted(cnts, key=cv2.contourArea)[-1]
## (4) Crop and save it
x,y,w,h = cv2.boundingRect(cnt)
dst = img[y:y+h, x:x+w]
cv2.imwrite("001.png", dst)
请尝试与任何扫描的图像,旋转它,并尝试摆脱背景空白,旋转到它的原始尺寸进行计算机视觉操作
考虑到你不知道旋转的角度,而且每个扫描的图像都可能不同,你需要先找到它。在
将您已经做的与this问题的已接受答案结合起来。在
对于您提供的图像:
使用cv2.boundingRect将提供适合轮廓的最小非旋转矩形。cv2.boundingRect结果:
您将需要使用cv2.boundingRect,而不是使用cv2.minareRect来获得一个适合轮廓的矩形。cv2.minarerect结果:
在获得旋转矩形信息后,需要找到模型点与当前点之间的仿射变换矩阵。当前点是在旋转矩形中找到的点,模型点是原始对象的点。在本例中,对象的初始位置(0,0)和旋转矩形的宽度和高度。在
仿射在这里可能有点过头了,但为了一般性,使用了仿射变换。在
详细说明在代码中。在
结果:
如果保证背景是饱和的白色(值255),而文档主要是非饱和值,则在阈值255以下进行二值化并拟合一个边框。在
相关问题 更多 >
编程相关推荐