我正在尝试设计一个类似于 camscanner 的应用程序。为此,我必须拍摄一张图像,然后在其中找到文档。我从这里描述的代码开始 -http://opencvpython.blogspot.in/2012/06/sudoku-solver-part-2.html http://opencvpython.blogspot.in/2012/06/sudoku-solver-part-2.html
我发现轮廓和最大面积的矩形轮廓应该是所需的文件。对于每个轮廓,我都找到一个近似闭合的 PolyDP。在所有尺寸为 4 的 PolyDP 中,面积最大的应该是所需的文档。然而,这个方法不起作用。
The input image for the process is this
I tried to print the contour with max area and this resulted in this (Contour inside letter 'C')
Code:
img = cv2.imread('bounce.jpeg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
def biggestRectangle(contours):
biggest = None
max_area = 0
indexReturn = -1
for index in range(len(contours)):
i = contours[index]
area = cv2.contourArea(i)
if area > 100:
peri = cv2.arcLength(i,True)
approx = cv2.approxPolyDP(i,0.1*peri,True)
if area > max_area: #and len(approx)==4:
biggest = approx
max_area = area
indexReturn = index
return indexReturn
indexReturn = biggestRectangle(contours)
cv2.imwrite('hola.png',cv2.drawContours(img, contours, indexReturn, (0,255,0)))
这到底是怎么回事?还有其他方法可以捕获这张图片中的文档吗?