问题
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. 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:
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.
回答1:
As stated here, the correct way to transform RGB to HSV is using cv2.COLOR_BGR2HSV
flag:
# Convert BGR to HSV
mask = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
When you change the line, the output:
- How I obtain the result?
I used imwrite
and save image into my local pc.
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()
回答2:
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. Instead it will get you a single channel Graysclae image. For HSV, you need to use cv2.COLOR_BGR2HSV
.
来源:https://stackoverflow.com/questions/63462731/about-line-detection-by-using-opencv