【问题标题】:How to find Contour based on specific color outline or border?如何根据特定的颜色轮廓或边框找到轮廓?
【发布时间】:2021-10-26 12:01:27
【问题描述】:

我正在尝试根据 AutoCAD 绘图中显示的黄色轮廓检测轮廓计数。

import numpy as np
import cv2

image = cv2.imread('C:/Users/htc/Desktop/capture.png')
original = image.copy()
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([0, 208, 94], dtype="uint8")
upper = np.array([179, 255, 232], dtype="uint8")
mask = cv2.inRange(image, lower, upper)

# Find contours
cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Extract contours depending on OpenCV version
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

# Iterate through contours and filter by the number of vertices 
for c in cnts:
   perimeter = cv2.arcLength(c, True)
   approx = cv2.approxPolyDP(c, 0.04 * perimeter, True)
   if len(approx) > 5:
       cv2.drawContours(original, [c], -1, (36, 255, 12), -1)

cv2.imshow('mask', mask)
cv2.imshow('original', original)
cv2.waitKey()

我期望检测到这两个轮廓圆(轮廓)但未实现的输出。任何人都可以帮助我解决我做错的地方。

输出

【问题讨论】:

    标签: python numpy opencv machine-learning contour


    【解决方案1】:

    您的上边界无法检测到圆圈的颜色。 尝试将其设置为这样的:

    upper = np.array([179, 255, 255], dtype="uint8")
    

    随意玩转边界以选择您的颜色。

    【讨论】:

      【解决方案2】:

      黄色的色调值与其他颜色不同。可以使用第一个通道(色调),取值范围为10:15

      lower = np.array([10, 0, 0], dtype="uint8") 
      upper = np.array([15, 255, 255], dtype="uint8")
      

      色调通道表示图像see this 的“颜色”。您可以显示色调通道

      hue = image[:,:,0]
      cv2.imshow('hue', hue)
      

      它几乎是黑色的。让我们“重新调整”它的值:

      cv2.imshow('hue', (hue-np.min(hue))/(np.max(hue)-np.min(hue))*255)
      

      使用MS Paint找到“白色区域”的范围后,我看到它是170-255np.max(hue) = 15, np.min(hue) = 0,所以原来的范围是10-15

      这就是我在lowerupper 中获得数字的方式。由于您只关心颜色,因此其他通道设置为0,0 用于lower255,255 用于upper

      【讨论】:

      • 感谢您的回复!它以黄色完美工作,但你能告诉我文档链接,我可以在其中找到所有颜色数组
      • @ShahnawazIrfan 我编辑了我的答案,希望对你有帮助
      猜你喜欢
      • 2012-08-29
      • 2021-06-17
      • 2019-03-03
      • 1970-01-01
      • 2023-01-27
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      • 2018-03-18
      相关资源
      最近更新 更多