Lines detection with Hough Transform – OpenCV 3.4 with python 3 Tutorial 21
We’re going to learn in this tutorial how to detect the lines of the road in a live video using Opencv with Python.
Before going into the lines road detection, we need to understand using opencv what is a line and what isn’t a line.
Hough lines transform:
The Houg lines transform is an algorythm used to detect straight lines. One of the most important features of this method is that can detect lines even when some part of it is missing. And this comes really useful in the road when we have dashed lines, or when for some reason some part of the line is not visible.
Let’s see an example on a simple geometric figure.
We import the opencv and numpy libraries and we load the image.
import cv2 import numpy as np img = cv2.imread("lines.png")
We then simplyfi the images, loading only the grayscale format and detecting the edges.
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 75, 150)
We have above a white image with black lines.
On the edges we apply the lines detection using hough transform:
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 50)
What if this image was a square and simply we can’t see the some part of the lines?
Same case if we are driving on the road and there are dashed lines, or some line is cover by tree leaves, some dirt and so on, how can we still detect the line?
On the houghtransform method have a variable called: maxLineGap. We can change its value to fit our needs. In this case let’s say that the gap between the lines can be up to 250 pixels, so our code will look like this:
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 30, maxLineGap=250)
Detect lines of the road:
To detect the lines of the road the process is really similar to the previous one to detect the line of the rectangle.
Let’s start importing the library and loading our video:
import cv2 import numpy as np video = cv2.VideoCapture("road_car_view.mp4") while True: ret, orig_frame = video.read() if not ret: video = cv2.VideoCapture("road_car_view.mp4") continue frame = cv2.GaussianBlur(orig_frame, (5, 5), 0)
That is on a frame how it looks the road:
The core part of the detection is to correctly extract the lines of the road from all the rest of the images. We can do this applying the hsv color detection. In this way we can detect object by their colors, as the lines of a road can be only yellow or white, we extract the part of the images that contains these two colors only.
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) low_yellow = np.array([18, 94, 140]) up_yellow = np.array([48, 255, 255]) mask = cv2.inRange(hsv, low_yellow, up_yellow)
The white part corresponds to the yellow colors in the original frame, so we can see the lines and also some part of the yellow buildings.
Once we have the mask we find the edges, we use the hough transform method and the detection is done.
edges = cv2.Canny(mask, 75, 150) lines = cv2.HoughLinesP(edges, 1, np.pi/180, 50, maxLineGap=50) if lines is not None: for line in lines: x1, y1, x2, y2 = line cv2.line(frame, (x1, y1), (x2, y2), (0, 255, 0), 5)
We finally show everything on the screen:
cv2.imshow("frame", frame) cv2.imshow("edges", edges) key = cv2.waitKey(1) if key == 27: break video.release() cv2.destroyAllWindows()
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Opencv Beginner Tutorial
- 1) Loading images
- 2) Loading Video and Webcam
- 3) Drawing and writing on images
- 4) Basic operations on images
- 5) Add images and Threshold
- 6) Blending images
- 7) Bitwise Operators
- 8) Trackbars
- 9) Object detection using HSV Color space
- 10) Basic Thresholding
- 11) Histograms
- 12) Basic geometric transformations
- 13) Perspective transformation
- 14) Affine transformation
- 15) Adaptive thresholding5
- 16) Smoothing images
- 17) Morphological transformation
- 18) Edge detection
- 19) Find and Draw Contours
- 20) Template matching
- 21) Lines detection with Hough Transform
- 22) Corners detection
- 23) Image Pyramids
- 24) Image Pyramids (Blending and reconstruction)
- 25) Feature detection (SIFT, SURF, ORB)
- 26) Feature Matching (Brute-Force)
- 27) Mouse Events
- 28) Histogram and Back Projection
- 29) Object tracking with Mean-shift
- 30) Object tracking with Camshift
- 31) Optical Flow with Lucas-Kanade method
- 32) Background Subtraction
- 33) k-Nearest Neighbour classification
- 34) Object tracking using Homography
- 35) Fourier Transform
- 36) Knn handwritten digits recognition
- 37) Face detection using Haar Cascades
- Train YOLO to detect a custom object (online with free GPU)
- YOLO object detection using Opencv with Python
- Detecting colors (Hsv Color Space) – Opencv with Python
- How to install Python 3 and Opencv 4 on Windows
- How to install Dlib for Python 3 on Windows
- Check if two images are equal with Opencv and Python
- Feature detection (SIFT, SURF, ORB) – OpenCV 3.4 with python 3 Tutorial 25
- Eye motion tracking – Opencv with Python