【问题标题】:yard inside garden detection (cv2)院子内花园检测 (cv2)
【发布时间】:2020-07-14 00:25:23
【问题描述】:

我想用栅栏地下室和石头把花园里的草和外面的草分开。该脚本应该能够绘制您可以在第二张图像中看到的红线。 cv2应该可以的

我尝试了一个在车道检测系统中找到的代码,但不幸的是它不适用于草地。

gray = cv2.cvtColor(gras_image, cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
canny = cv2.Canny(blur, 50, 200)

感谢您的帮助

【问题讨论】:

    标签: python image colors computer-vision cv2


    【解决方案1】:

    请参阅下面我的代码中的 cmets,了解如何在图像中仅选择您的院子。

    如果您的院子与您的院子之间的边界颜色发生变化,这将不是一个可靠的解决方案。通过在 GIMP 中打开图像并使用 GIMP 的颜色选择器工具选择右侧的棕色边框对象,我找到了合适的 HSV 范围。奇迹般地,这种颜色也适用于灰色块。通过使用颜色范围,或者甚至为棕色块和灰色块创建单独的蒙版,您可能可以获得更好的结果。

    import cv2
    import numpy as np
    
    #load the image
    image = cv2.imread("meadow.jpg")
    
    #define the lower and upper bounds of colors to threshold for in the HSV color space.
    hsv_lower = (35//2, 0, 120)
    hsv_upper = (45//2, 255, 255)
    
    #convert the image to HSV color space
    hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    
    #find the areas of the image corresponding to the colors set above
    mask = cv2.inRange(hsv_image, hsv_lower, hsv_upper)
    
    #set the detected areas to white and other areas to black
    new_image = np.where(mask, np.uint8(0), np.uint8(255))
    
    #erode to fill in the gaps between the black pixels.
    new_image = cv2.erode(new_image, kernel=np.ones((7,7)))
    
    #find connected components (the white areas)
    labels, stats = cv2.connectedComponentsWithStats(new_image)[1:3]
    
    #create a mask for the area excluding the largest component
    not_my_yard = labels != np.argmax(stats[:,cv2.CC_STAT_AREA])
    
    #set the color of the area excluding the largest component to black
    image[not_my_yard] = 0
    
    #save the new image
    cv2.imwrite("my_yard.jpg", image)
    

    【讨论】:

    • 对不起,我忘了添加没有想象中的红线的图片。我想要的 python 脚本应该执行该步骤。而且它应该独立于草的颜色,能够识别边缘。因为栅栏底座和岩石清晰可见,您认为使用 cv2 canny() 和 HoughLinesP() 可以实现吗?
    • 我假设你想检测边框而不是草的颜色,这就是我选择棕色块的原因。看起来它们在您的新图像中不那么明显,并且此方法可能不再有效。我不认为 Canny 或 HoughLinesP 会为此工作。尝试和我做的一样,但为你的深绿色栅栏和白砖创建一个面具,然后组合面具。然后使用 findContours 代替连接的组件。找到轮廓后,您可以使用 drawContours 将边框绘制为红色。如果我能找到更多时间,我会在稍后提供帮助。
    • 你知道是否有一个模式查找器可以获取gras和fence的颜色范围?为什么我应该在这里使用 HSV 而不是 RGB?
    • 我不知道找到颜色范围的简单方法。如果您想要更精确的范围,则可以裁剪目标部分,将其加载到 Python 脚本中,并获取每个通道的最小-最大范围(色调、饱和度、值)。如果您愿意,您可以使用 RGB 代替 HSV,但找到正确的颜色范围可能会相当困难。
    • 是否有用于混凝土或其他类似材料的模式检测系统?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 2018-03-05
    相关资源
    最近更新 更多