【发布时间】:2022-08-05 14:28:37
【问题描述】:
我正在尝试获取图像中的最大面积对象。
我应用了 Blur Kernel 5x5,然后应用 Canny 算法来获得边缘。然后我使用了findContours 方法和最大值contourArea,但它返回了错误的对象。
如您所见,它必须返回左侧框,但返回右侧框。 我认为问题在于左右框共享一个共同的边缘,但似乎只属于左侧。
这是代码sn-p:
img_rgb = cv.imread(img_path)
gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
# blur with a kernel size of 5x5
blur = cv.GaussianBlur(gray, (5, 5), 0)
canny = cv.Canny(blur, 50, 50)
#saving canny image
cv.imwrite(\"canny.png\", canny)
_, thresh = cv.threshold(canny, 127, 255, 0)
contours, _ = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
for c in contours:
cv.drawContours(img_rgb, [c], 0, (randrange(255), randrange(255), randrange(255)), 3)
#saving image with contours
cv.imwrite(\"contours.png\", img_rgb)
max_area_contour = max(contours, key=cv.contourArea)
x, y, w, h = cv.boundingRect(max_area_contour)
cv.rectangle(img_rgb, (x, y), (x + w, y + h), (0, 255, 0), 3)
#saving the image with the biggest contour
cv.imwrite(\"max_contour.png\", img_rgb)
-
什么是预期的输出?
-
检测左边的盒子而不是右边的盒子
标签: python opencv computer-vision contour