gpt4 book ai didi

python - OpenCV-仅检测图像中的特定行

转载 作者:行者123 更新时间:2023-12-02 17:02:20 28 4
gpt4 key购买 nike

我试图隔离下图中的一行,我知道一些方法,例如CannyEdge Detection,它可以检测图像中的所有行,但是却在努力获取我感兴趣的行。

非常感谢OpenCV中有关工具的任何信息。 enter image description here

我们的目标是检测出球场顶部的红色轮廓线(我已用蓝色框出)

enter image description here

最佳答案

在Python / OpenCV中,您可以在线条的红色上设置阈值,然后获得最大轮廓或在面积上大于某个阈值的轮廓上,这是我在下面显示的。

输入:

enter image description here

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)

阈值图片:

enter image description here

产生的轮廓:

enter image description here

关于python - OpenCV-仅检测图像中的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60150480/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com