【问题标题】:OpenCV - Detect only a particular line in an imageOpenCV - 仅检测图像中的特定线
【发布时间】:2020-02-10 12:28:41
【问题描述】:

我正在尝试隔离下图中的一条线,我知道一些方法,例如 CannyEdge 检测,可以检测图像中的所有线,但我正在努力获取我感兴趣的线.

任何有关 OpenCV 工具的信息都可以帮助解决这个问题。

目标是检测球场的顶部红色轮廓(我已用蓝色勾勒出来)

【问题讨论】:

  • 你好,你能不能也发一张未经编辑的图片让我试一试?
  • Hough Line Transform,它检测图像中的所有线条,可能会被调整为只检测你非常主导的线条
  • 线条是否总是红色,背景是否像这张图片中一样干净?如果是,颜色阈值,然后二进制线检测也可能工作得很好

标签: python opencv line object-detection detection


【解决方案1】:

在 Python/OpenCV 中,您可以对线条的红色进行阈值化,然后获得最大轮廓或大于某个区域阈值的轮廓,这就是我在下面展示的内容。..

输入:

import cv2
import numpy as np

# read image as grayscale
img = cv2.imread('red_line.png')

# threshold on red color
lowcolor = (0,0,75)
highcolor = (50,50,135)
thresh = cv2.inRange(img, lowcolor, highcolor)


# apply morphology close
kernel = np.ones((5,5), np.uint8)
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

# get contours and filter on area
result = img.copy()
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
result = img.copy()
for c in contours:
    area = cv2.contourArea(c)
    if area > 5000:
        cv2.drawContours(result, [c], -1, (0, 255, 0), 2)


# show thresh and result    
cv2.imshow("thresh", thresh)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

# save resulting images
cv2.imwrite('red_line_thresh.png',thresh)
cv2.imwrite('red_line_extracted.png',result)


阈值图像:

生成的轮廓:

【讨论】:

  • 反响非常好!干杯
猜你喜欢
  • 2020-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多