简体   繁体   English

如何使用OpenCV在路边画一条线?

[英]How to use OpenCV to draw a line on the curb?

I use this code to do line detection with canny and hough,but the effect is not good.我用这段代码用canny和hough做线检测,但是效果不好。

I want to draw a line on the curb.我想在路边画一条线。 The purpose of drawing a straight line is to caculate the position of the line in the image, because when I do self-driving, I want my ROS car to drive along the curb.My country is right side driving.画一条直线的目的是为了计算出这条线在图片中的位置,因为我在自驾的时候,我想让我的ROS车沿着路边行驶。我的国家是右侧行驶。

import cv2
import numpy as np

img = cv2.imread(r"E:\test_opencv\images\testcanny.jpg")
blur_img = cv2.GaussianBlur(img, (3, 3), 0)
edges = cv2.Canny(blur_img, 250, 450, apertureSize=3)
lines = cv2.HoughLines(edges, 1, np.pi / 180, 118)
minLineLength = 800
maxLineGap = 15
threshold=80
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold, minLineLength, maxLineGap)
for i in range(len(lines)):
    for x1, y1, x2, y2 in lines[i]:
        cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 4)
cv2.imshow('canny', edges)
cv2.imshow('Result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Original picture:原图:

在此处输入图片说明

Canny picture:精明图片:

在此处输入图片说明

Result picture:结果图:

在此处输入图片说明

Only some short green lines on the result picture.结果图片上只有一些短绿线。

In fact,I just want the red part of the curb to become a green line.事实上,我只是想让路边的红色部分变成一条绿线。 在此处输入图片说明

Just like this:像这样:

在此处输入图片说明

How to fix the code?如何修复代码?

Using classic Hough transform ( HoughLines ) gives a better result other than the the probabilistic Hough transform ( HoughLinesP ).使用经典的 Hough 变换 ( HoughLines ) 可以获得比概率 Hough 变换 ( HoughLinesP ) 更好的结果。 I ran this code on your exact image and got these results.我在您的确切图像上运行此代码并获得了这些结果。 The threshold value for the first image is 100 and for the next image is 200第一张图片阈值为 100下一张图片阈值为200

在此处输入图片说明

在此处输入图片说明

The code:编码:

import cv2
import numpy as np

img = cv2.imread("D:\\1.jpg")
blur_img = cv2.GaussianBlur(img, (3, 3), 0)
edges = cv2.Canny(blur_img, 250, 450, apertureSize=3)

rho = 1  #Distance resolution of the accumulator in pixels.
theta = np.pi/180 # Angle resolution of the accumulator in radians.
threshold  = 200 #Accumulator threshold parameter. Only those lines are returned that get enough votes ( >\texttt{threshold} ).

lines = cv2.HoughLines(edges, rho, theta, threshold)

for i in range(len(lines)):
    for r,th in lines[i]:
        a = np.cos(th)
        b = np.sin(th)
        x0 = a*r
        y0 = b*r
        x1 = int(x0 + 1000*(-b))
        y1 = int(y0 + 1000*(a))
        x2 = int(x0 - 1000*(-b))
        y2 = int(y0 - 1000*(a))

        cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imshow('canny', edges)
cv2.imshow('Result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

And further information on Hough transform:以及有关霍夫变换的更多信息:

https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html

For this picture, I use this code to make a better effect.对于这张图,我用这段代码来做出更好的效果。

import cv2
import numpy as np

img = cv2.imread(r"E:\test_opencv\images\testcanny.jpg")
blur_img = cv2.GaussianBlur(img, (3, 3), 0)
gray = cv2.cvtColor(blur_img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 250, 350, apertureSize=3)
rho = 1  #Distance resolution of the accumulator in pixels.
theta = np.pi/180 # Angle resolution of the accumulator in radians.
threshold  = 300 #Accumulator threshold parameter. Only those lines are returned that get enough votes ( >\texttt{threshold} ).
lines = cv2.HoughLines(edges, rho, theta, threshold)
if lines is not None:
    for i in range(len(lines)):
        for r,th in lines[i]:
            a = np.cos(th)
            b = np.sin(th)
            x0 = a*r
            y0 = b*r
            x1 = int(x0 + 1000*(-b))
            y1 = int(y0 + 1000*(a))
            x2 = int(x0 - 1000*(-b))
            y2 = int(y0 - 1000*(a))
            cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.imshow('canny', edges)
cv2.imshow('Result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result picture:结果图:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM