【发布时间】:2019-03-08 00:02:46
【问题描述】:
我正在尝试创建一个程序来检测和删除图片中的边框,目标是检测图片中的文档并清理它......
这是我的代码:
import sys
import cv2
import numpy as np
import rect
image = cv2.imread('./test.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 9)
ret, gray = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
edges = cv2.Canny(gray, 10, 250)
contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)
#x,y,w,h = cv2.boundingRect(contours[0])
#cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),0)
# get approximate contour
for c in contours:
p = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * p, True)
if len(approx) == 4:
target = approx
break
cv2.drawContours(image, [target], -1, (0, 255, 0), 2)
cv2.imwrite('./final.jpg', image)
但现在……它唯一能找到的是:
...根据要求,这是一张有效的图片:
【问题讨论】:
-
你应该改用
cv2.THRESH_BINARY_INV。 -
我做到了...结果是一样的。
-
这意味着它没有找到其他轮廓。你看过阈值图像吗?当我用
cv2.THRESH_BINARY_INV尝试你的代码时,我没有得到轮廓。稍后我会发布解决方案 -
你能把图片贴在合适的地方吗?
-
瑞克,我添加了有效的图像...谢谢
标签: python opencv image-processing