gpt4 book ai didi

python - 仅使用 OpenCV 检测图像中的虚线(断线)

转载 作者:行者123 更新时间:2023-12-02 16:14:38 26 4
gpt4 key购买 nike

我正在尝试学习图像特征检测技术。

我已经设法检测到水平线(不间断/连续),但是我无法检测到图像中的所有虚线/断线。

这是我的测试图片,你可以看到有虚线和一些文本/框等。

My test Image

到目前为止,我使用了以下代码,它只检测到一条虚线。

import cv2
import numpy as np

img=cv2.imread('test.jpg')
img=functions.image_resize(img,1000,1000) #function from a script to resize image to fit my screen
imgGray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgEdges=cv2.Canny(imgGray,100,250)
imgLines= cv2.HoughLinesP(imgEdges,2,np.pi/100,60, minLineLength = 10, maxLineGap = 100)
for x1,y1,x2,y2 in imgLines[0]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imshow('Final Image with dotted Lines detected',img)

我的输出图像如下。如您所见,我只设法检测到最后一条虚线。我玩过参数 rho、theta、min/max 线,但没有运气。

非常感谢任何建议:)

My Output Image

最佳答案

这个解决方案:

import cv2
import numpy as np

img=cv2.imread('test.jpg')

kernel1 = np.ones((3,5),np.uint8)
kernel2 = np.ones((9,9),np.uint8)

imgGray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgBW=cv2.threshold(imgGray, 230, 255, cv2.THRESH_BINARY_INV)[1]

img1=cv2.erode(imgBW, kernel1, iterations=1)
img2=cv2.dilate(img1, kernel2, iterations=3)
img3 = cv2.bitwise_and(imgBW,img2)
img3= cv2.bitwise_not(img3)
img4 = cv2.bitwise_and(imgBW,imgBW,mask=img3)
imgLines= cv2.HoughLinesP(img4,15,np.pi/180,10, minLineLength = 440, maxLineGap = 15)

for i in range(len(imgLines)):
for x1,y1,x2,y2 in imgLines[i]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imshow('Final Image with dotted Lines detected', img)

关于python - 仅使用 OpenCV 检测图像中的虚线(断线),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61239652/

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