About Line detection by using OpenCV

半世苍凉 提交于 2021-02-10 07:53:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!