[英]About Line detection by using OpenCV
I try to mark the road in the following figure, the yellow middle lines and the white edge lines.:我试着在下图中标记道路,黄色的中间线和白色的边缘线:
I use the standard code of Hough Transfrom.我使用 Hough Transfrom 的标准代码。 My code is as following:
我的代码如下:
import cv2
import numpy as np
img = cv2.imread('Road3.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
low_yellow=np.array([18, 94, 140])
up_yellow=np.array([48, 255, 255])
mask=cv2.inRange(hsv, low_yellow, up_yellow)
edges = cv2.Canny(mask,75,150)
lines = cv2.HoughLinesP(edges,1,np.pi/180,50,maxLineGap=250)
for line in lines:
x1,y1,x2,y2 = line[0]
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),5)
cv2.imshow('image', img)
cv2.imshow("edges", edges)
k = cv2.waitKey(0)
cv2.destroyAllWindows()
But there is mistake feedback:但是有错误反馈:
After changing the line with更改线路后
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
My output is as following:我的 output 如下:
It seems this is just part of the picture but I do not know where is the problem.看来这只是图片的一部分,但我不知道问题出在哪里。
Based on Ahmet's answer, I can get the black one picture as follows but the color one is part of the whole picture.根据 Ahmet 的回答,我可以得到如下的黑色图片,但彩色图片是整张图片的一部分。
The problem I guess is in this line:我猜问题出在这一行:
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Here the variable name is hsv
, but the cv2.COLOR_BGR2GRAY
would not genrate a hsv image for you.这里的变量名称是
hsv
,但cv2.COLOR_BGR2GRAY
不会为您生成 hsv 图像。 Instead it will get you a single channel Graysclae image.相反,它将为您提供单通道 Graysclae 图像。 For HSV, you need to use
cv2.COLOR_BGR2HSV
.对于 HSV,您需要使用
cv2.COLOR_BGR2HSV
。
As stated here , the correct way to transform RGB to HSV is using cv2.COLOR_BGR2HSV
flag:如此处所述,将 RGB 转换为 HSV 的正确方法是使用
cv2.COLOR_BGR2HSV
标志:
# Convert BGR to HSV
mask = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
When you change the line, the output:当您更改线路时,output:
I used imwrite
and save image into my local pc.我使用
imwrite
并将图像保存到我的本地电脑中。
cv2.imwrite("out.png", edges)
But if you want to display, first you need to resize to see the whole image.但是如果你想显示,首先你需要调整大小才能看到整个图像。
cv2.imshow("edges", cv2.resize(edges, (640, 480)))
cv2.waitKey(0)
cv2.destroyAllWindows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.