使用OpenCV仅在图像中检测虚线(折线) [英] Detect dotted (broken) lines only in an image using OpenCV

查看:182
本文介绍了使用OpenCV仅在图像中检测虚线(折线)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习有关图像特征检测的技术.

I am trying to learn techniques on image feature detection.

我设法检测到水平线(不间断/连续),但是我在检测图像中的所有虚线/断点时遇到了麻烦.

I have managed to detect horizontal line(unbroken/continuous), however I am having trouble detecting all the dotted/broken lines in an image.

这是我的测试图像,您可以看到虚线和一些文本/框等.

Here is my test image, as you can see there are dotted lines and some text/boxes etc.

到目前为止,我已经使用以下代码仅检测到一条虚线.

So far I have used the following code which detected only one dotted line.

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 line,但是没有运气.

My output image is below. As you can see I only managed to detect the last dotted line. I have played around with the parameters rho,theta,min/max line but no luck.

任何建议都将不胜感激:)

Any advice is greatly appreciated :)

推荐答案

此解决方案:

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)

这篇关于使用OpenCV仅在图像中检测虚线(折线)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆