从相机图像 OpenCV 中检测虚线

问题描述 投票:0回答:1

我正在制造一辆自动驾驶汽车。例如,我试图弄清楚如何将道路图片中的实线与虚线分开。

我尝试沿着轮廓搜索并查看填充物,但没有成功

我之前在堆栈溢出上发现了一段代码,如果你直接上传图像,它会很好用,但如果你连接相机,它就会完全停止工作

原代码:

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)

尝试添加相机:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
    ret, img = cap.read()

    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('img', img)

    if cv2.waitKey(1) & 0xFF == ord('q'):
         break

cap.release()
cv2.destroyAllWindows()
python python-3.x opencv
1个回答
0
投票

正确的方法。

片段:

import numpy as np
import cv2

cam = cv2.VideoCapture(0)

while (True):

    s, img = cam.read()

    winName = "Movement Indicator"
    cv2.namedWindow(winName, cv2.WINDOW_AUTOSIZE)

    edges = cv2.Canny(img, 100, 200)
    lines = cv2.HoughLinesP(edges, 1, np.pi / 4, 2, None, 10, 1)

    if lines is not None:
        for l in lines:
            for x1, y1, x2, y2 in l:
                cv2.line(img, (x1, y1), (x2, y2), (255, 255, 255), 20, 5)

    cv2.imshow('edges', edges)
    cv2.imshow('original', img)

    if cv2.waitKey(10) == ord('q'):
        break

cam.release()
cv2.destroyAllWindows()
© www.soinside.com 2019 - 2024. All rights reserved.