首页 > 解决方案 > 查找 Contour opencv 时缺少部分轮廓(受白色背景、文档扫描仪影响)

问题描述

我正在尝试对各种背景和光照条件下的文档进行透视变换。目前,我无法在此图像上执行此操作,因为轮廓不接近。

原始图像

图像边缘检测

到目前为止,我已经尝试过自适应阈值、高斯模糊、中值模糊、各种形态学操作、霍夫变换和直方图均衡,并使用其中的所有参数,但没有任何效果。

谁能帮我解决这个问题?

标签: pythonpython-3.xopencvcomputer-visionopencv-contour

解决方案


使用 Canny Edges、Dilation 和 Erosion 获得此输出

在此处输入图像描述

imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(img,20,40)

kernel = np.ones((5,5),np.uint8)
dilation = cv2.dilate(edges,kernel,iterations = 1)
erosion = cv2.erode(dilation,kernel,iterations = 1)

ret,thresh = cv2.threshold(erosion, 200, 255, 0)

contours, hierarchy = cv2.findContours(thresh,  
    cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) 

largest_areas = sorted(contours, key=cv2.contourArea)

cv2.drawContours(img, [largest_areas[-2]], -1, 255, 2)

cv2.imshow("image", img)
cv2.waitKey(0)

推荐阅读