【问题标题】:Using opencv to find all contours in an image使用opencv查找图像中的所有轮廓
【发布时间】:2020-09-30 19:02:27
【问题描述】:

有人可以帮我找出我的代码中的问题吗,我试图找到我图像中的所有轮廓,然后用灰边覆盖它,但它似乎只覆盖了一些轮廓。 '''

image_find_goal = "/absolutePathWays.img"
kernel = np.ones((5,5),np.uint8)
#findGoal(image_find_goal)
img1 = cv.imread(image_find_goal,cv.IMREAD_GRAYSCALE)
ret,mask = cv.threshold(img1, 125, 255, cv.THRESH_BINARY_INV)
contours, hierarchy = cv.findContours(mask,cv.RETR_TREE,cv.CHAIN_APPROX_NONE)
for cnt in contours:
    approx = cv.approxPolyDP(cnt,0.01*cv.arcLength(cnt,True),True)
    if len(approx) == 4:
        cv.drawContours(mask,cnt,-1,(119,256,51),5)
mask3 = cv.resize(mask,(640,640))
cv.imshow('IMAGE', mask3)
cv.waitKey(0)
cv.destroyWindow(mask3)

'''

【问题讨论】:

  • 您要查找哪些地区?全部还是来自特定格式?
  • 您是否收到错误消息?你的代码不能按你的意愿工作怎么办?
  • @dpetrini 我想找到立方体,也就是 3 个红色和白色/灰色的大盒子

标签: python opencv object-detection


【解决方案1】:
  • 我尝试打印 len(approx) 的值,很少有值等于 4,检测到的轮廓可能有轻微的不准确,因此可能会出现意外结果。
  • 我尝试更改
if len(approx) == 4:

if len(approx) >= 4:

Resulting image

  • 您也可以尝试完全删除条件或根据您的要求对其进行编辑。

【讨论】:

    猜你喜欢
    • 2011-06-13
    • 1970-01-01
    • 2019-07-18
    • 2019-01-15
    • 1970-01-01
    • 2023-03-09
    • 2017-11-11
    • 2016-08-09
    • 2012-11-06
    相关资源
    最近更新 更多