Python 如何使用OpenCV在路缘上绘制直线?
我用这段代码用canny和hough做线条检测,但是效果不好 我想在路边画一条线。画直线的目的是计算直线在图像中的位置,因为当我自动驾驶时,我希望我的ROS汽车沿着路边行驶。我的国家是右侧驾驶Python 如何使用OpenCV在路缘上绘制直线?,python,python-3.x,opencv,Python,Python 3.x,Opencv,我用这段代码用canny和hough做线条检测,但是效果不好 我想在路边画一条线。画直线的目的是计算直线在图像中的位置,因为当我自动驾驶时,我希望我的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(blu
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()
原图:
精明的图片:
结果图片:
结果图片上只有一些短的绿色线条
事实上,我只是希望路缘的红色部分变成绿线。
就这样,
如何修复代码?使用经典Hough变换(
HoughLines
)比概率Hough变换(HoughLinesP
)的结果更好。我在你的图片上运行了这个代码,得到了这些结果。第一个图像的阈值为100,下一个图像的阈值为200
守则:
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()
以及关于Hough变换的更多信息:
使用经典Hough变换(
HoughLines
)比使用概率Hough变换(HoughLinesP
)得到更好的结果。我在你的图片上运行了这个代码,得到了这些结果。第一个图像的阈值为100,下一个图像的阈值为200
守则:
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()
以及关于Hough变换的更多信息:
对于这张图片,我使用此代码来获得更好的效果
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()
结果图片:
对于这张图片,我使用此代码来获得更好的效果
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()
结果图片:
当我在您的代码中设置
threshold=300
时,路边只有一行,这非常有用。谢谢!当我在你的代码中设置threshold=300
时,路边只有一行,这非常有用。谢谢!