图像处理:对同一背景上的物体进行边界检测

2024-10-03 19:31:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用OpenCV.3开发Python3.6。 我正在尝试自动找到一张纸的轮廓,以将其裁剪为正确的大小,下面是示例:

Image

我看过很多关于同一主题的帖子,但最终还是没有什么真正适合我的。在

我使用的代码是:

orig = cv2.imread("Image.jpg")
cv2.imshow('result', imutils.resize(orig, height=600))
cv2.waitKey(0)

img = cv2.cvtColor(orig, cv2.COLOR_BGR2GRAY)
cv2.GaussianBlur(img, (3,3), 0, img)


edges = cv2.Canny(img,10,20,apertureSize = 3)
cv2.imshow('result', imutils.resize(edges, height=600))
cv2.waitKey(0)

这是输出:

canny

所以现在我试着用HougLines或HougLines概率来找到countours。在

^{pr2}$

但我没有任何有用的东西。。(如果我在这里有更多的名声,我可以加上我得到的) 我不知道我是否不需要使用HoughLines,或者只是无法获得正确的参数。。 我已经看过角点检测,但问题是一样的。在

有什么想法吗?在


Tags: 示例主题imgresultcv2帖子轮廓height
2条回答

好吧,我找到了一些很好的东西,给那些即将来到这一页的人。在

首先,找出所有的轮廓线,然后在一张黑色图片上画出来。(我把它们画得更大,就像OpenCv找到的轮廓之间有空间,你不会感到烦恼)

_, contours, _ = cv2.findContours(edges.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contours=list(filter(lambda cont: cv2.arcLength(cont, False) > 60, contours))
# show only contours
blank = np.zeros((orig.shape[0],orig.shape[1],3), np.uint8)
cv2.drawContours(blank, contours,-1,(0,255,0),5)

contours

^{pr2}$

再次应用canny&find contour。这一步其实只是为了能够找到一个真正的轮廓,如果你的图像不完美。在

maxcontour=0
for cnt in contours:
    cv2.drawContours(img, cnt, 0, (255, 255, 0), 2)
    if cv2.contourArea(cnt)>5000:  # remove small areas like noise etc
        hull = cv2.convexHull(cnt)    # find the convex hull of contour
        hull = cv2.approxPolyDP(hull,0.1*cv2.arcLength(hull,True),True)
        if len(hull)==4:
            cv2.drawContours(img,[hull],0,(0,255,0),2)

final

颜色比你想象的要不同。在

平展图像的亮度以校正照明伪影,然后使像素比背景更接近纸张的颜色。在

enter image description here

相关问题 更多 >