简体   繁体   中英

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. 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.

在此处输入图像描述

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 .

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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