【问题标题】:Problems drawing a specific contour using cv2.drawContours() Python 3使用 cv2.drawContours() Python 3 绘制特定轮廓的问题
【发布时间】:2018-02-14 06:22:54
【问题描述】:

我在使用 python 时遇到 cv2.drawContours() 问题

问题:我无法显示单个轮廓。我只想得到曲目

代码如下:

original_image = np.array(ImageGrab.grab(bbox))

crop_img = original_image[200:307, :, :]

# Convert BGR to HSV
hsv = cv2.cvtColor(crop_img, cv2.COLOR_BGR2HSV)

# define range of track (grey) color in HSV
lower_grey = np.array([0, 0, 0])
upper_grey = np.array([255, 40, 150])

# Threshold the HSV image to get only gery colors
grey_mask = cv2.inRange(hsv, lower_grey, upper_grey)
grey_mask2 = grey_mask.copy()

_, contours, heir = cv2.findContours(grey_mask2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(grey_mask2, contours, 0, (0, 255, 0), 3)

cv2.imshow('Orig Image', crop_img)
cv2.imshow('Grey Mask', grey_mask2)

if cv2.waitKey(25) & 0xFF == ord('q'):
    cv2.destroyAllWindows()
    break

Original Image When drawContours() is Set to, 0

但如果我将轮廓数设置为显示 = -1(全部),它似乎会得到一些轮廓

When drawContours() is Set to, -1

我已经尽力解决这个问题,任何建议都将不胜感激

【问题讨论】:

    标签: python opencv cv2


    【解决方案1】:
    cv2.drawContours(image, contours, contourIdx, color, thickness)
    

    画出图像中的所有轮廓:contourIdx = -1 要在列表中绘制一个特定的轮廓,比如第三个轮廓,然后设置 contourIdx = 2

    所以如果你想要有赛道的轮廓,然后找到它的索引并绘制它。否则,假设赛道的轮廓面积最大。您可以简单地执行以下操作:

    _, contours, heir = cv2.findContours(grey_mask2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    c = max(contours, key = cv2.contourArea)
    cv2.drawContours(grey_mask2,[c],0,(0, 255, 0),3)
    

    【讨论】:

      猜你喜欢
      • 2017-12-27
      • 1970-01-01
      • 2017-05-25
      • 2021-07-19
      • 1970-01-01
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多