我有一个树莓派相机对着一张白色背景上的卡片,但是局部阴影似乎妨碍了我用于检测卡片轮廓的闭合,这意味着检测整体失败。以下是我的意思的屏幕截图:
你可以看到尤其是底部角落周围变得粗糙。这是我用来做到这一步的代码:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.blur(gray, (5,5))
gray = cv2.bilateralFilter(gray, 11, 17, 17) #blur. very CPU intensive.
cv2.imshow("Gray map", gray)
edges = cv2.Canny(gray, 30, 120)
cv2.imshow("Edge map", edges)
#find contours in the edged image, keep only the largest
# ones, and initialize our screen contour
# use RETR_EXTERNAL since we know the largest (external) contour will be the card edge.
_, cnts, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:1]
screenCnt = None
# loop over our contours
for c in cnts:
# approximate the contour
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.3 * peri, True)
cv2.drawContours(image, [cnts[0]], -1, (0, 255, 0), 2)
# if our approximated contour has four points, then
# we can assume that we have found our card
if len(approx) == 4:
screenCnt = approx;
break
有没有一种方法可以强制它关闭特定的轮廓?如果我将图像模糊得更平滑以消除阴影,那也不起作用,因为它会简单地忽略那些角落,认为它们没有边缘。令人恼火的是,它距离闭合轮廓只有几个像素,但它从未这样做...
编辑:我现在有了一个更真实的设置,其中背景是米色的,并且有更多的阴影干扰。米色是必需的,因为有一些带有白色边框的卡片,所以白色行不通。边缘检测在左侧阴影最多的地方失败。