Lane Detection using OpenCV, Transcriptions of Computer Science

The process of analyzing and detecting lanes in images using the opencv library. It covers the key steps involved, including edge detection using the canny algorithm, creating a region of interest mask, and applying the hough transform to identify the lines representing the lanes. The document also discusses challenges such as image noise, blur ratio, and decimal precision issues in the slope and y-intercept calculations. The provided code demonstrates the implementation of these techniques and the resulting visualizations of the detected lanes. This information could be valuable for students and researchers interested in computer vision, advanced driver-assistance systems (adas), and autonomous vehicle development.

Typology: Transcriptions

2021/2022

Uploaded on 03/30/2023

tu-uyen-10
tu-uyen-10 🇻🇳

1 document

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
INDIVIDUAL ASSIGNMENT
Identifying “Driving path” using OpenCV and Python
Name: Chau Tu Uyen
Student ID: 200109
Course: Intelligent Robot Studio: From Theory to Practice
Instructor: Dr. Phung Manh Duong
Date of submission: December 18th, 2022
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Lane Detection using OpenCV and more Transcriptions Computer Science in PDF only on Docsity!

INDIVIDUAL ASSIGNMENT

Identifying “Driving path” using OpenCV and Python

Name: Chau Tu Uyen Student ID: 200109 Course: Intelligent Robot Studio: From Theory to Practice Instructor: Dr. Phung Manh Duong Date of submission: December 18th, 2022

Table of Contents

    1. Introduction.........................................................................................................................................
    1. Methods...............................................................................................................................................
    • 2.1. Gaussian Blur..............................................................................................................................
    • 2.2. Edge Detection............................................................................................................................
    • 2.3. Region of interest.........................................................................................................................
    • 2.4. Hough Transform........................................................................................................................
    • 2.5. Average left and right lines..........................................................................................................
    1. Results...............................................................................................................................................
    1. Discussion.........................................................................................................................................
    • 4.1. Image noise, blur ratio...............................................................................................................
    • 4.2. Edge detect ratio........................................................................................................................
    • 4.3. Finding mask.............................................................................................................................
    • 4.4. Hough Transform function........................................................................................................
    • 4.5. Decimal problem in slope and y intercept equation...................................................................
    1. Appendix...........................................................................................................................................
    • 5.1. Program.....................................................................................................................................

the digital world is made up of pixels. In a black-and-white image, a pixel contains the intensity

  • lightness of the pixel - from 0 to 255. A lower value produces a darker image, while a higher value produces a brighter image; for example, 0 is black, 255 is white, and 128 is grey. The pixel intensity does not change simultaneously over the entire pixel between the brightness and darkness areas but varies on a pixel-by-pixel (gradient). The figure below shows how Canny works: If a strong gradient is detected (brightness varies widely in the area), that pixel is retained. If a weak gradient is detected, that pixel will be removed. That is how the Canny tool detects the edge of an image. To use Canny, the image will first be imported and converted to black and white:

2.1. Gaussian Blur The gaussian blur tool is used to smooth and reduce the noise of an image, which can be detected as an edge. In function cv.GaussianBlur(), the width and height value of gaussian kernel calculated in the function must be specified and the value must be positive and odd. The higher the value, the more blurrier the image. This image is blurred by function: cv2.GaussianBlur(image, (23,23),0). 23,23 stands for width and height value of Gaussian kernel. 2.2. Edge Detection An image contains many pixels. The image can be represented in 2d space in the computer world, where x is the width and y is the height. To detect an edge, Canny will scan for the strong gradient in the image and compute the image from all directions on the x-axis and y- axis to measure the change of the strong gradient for edge detection. This figure is made by the Canny's edge detect function:

2.4. Hough Transform Hough Transform will examine the pixels to check if it is on the same line to detect that line on the image. Its function on OpenCV is shown as below: cv.HoughLinesP(image, rho, theta, threshold, lines, minLineLength, maxLineGap) with the arguments: image: Output of Canna or any edge detected function lines: will store the parameters of detected line (xstart, ystart, xend, yend) rho: resolution of r in pixel theta: Resolution of θ calculated by radian, I will use 1 degree (np.pi/180) = pi/180 = 180/180 = 1 threshold: The minimum number of votes to detect a line minLineLength: the minimum number of points to create a line maxLineGap: The maximum gap between two points to be considered in the same line. The figure below shows the process of increasing one vote unit with a theta value of 22. degrees and a rho value of 1 pixel:

The figure below shows lines detected by Hough Transform used on the masked edge detection image.

  1. Results Figure1a: Figure1b:

Figure1c: Figure1d:

4.3. Finding mask In edge detection, I have to minimize the region of interest I want to capture the edge. The figures below show that if the region of interest is not masked, many unwanted edges are detected, resulting in unwanted lines being detected and affecting the accuracy of the average of the left, right and middle lines: 4.4. Hough Transform function Due to the low resolution of figure1a, I have to increase the rho value to 2, which will affect the accuracy of the detected line. To increase accuracy, a trial-and-error procedure is required to find the sweet spot of the threshold (60) and the minimum length (30) needed for the four provided figures. 4.5. Decimal problem in slope and y intercept equation The equation of slope and y-intercept creates a significant decimal problem, which lowers the program’s performance. To solve this problem, I minimize it to 2 decimals.

  1. Appendix 5.1. Program import cv import matplotlib.pyplot as plt import numpy as np img = cv2.imread('Figure1b.jpg') img_line = img.copy() img_line_add = img.copy() img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_blur = cv2.GaussianBlur(img_gray,(5,5),0) # must be odd number img_edge = cv2.Canny(img_blur,75,150) #1:2 "Canny recommended a lower:upper ratio between 1:2 and 1:3."

create 4 points of rectange

points = np.array([ [(0, img_edge.shape[0]), (img_edge.shape[1], img_edge.shape[0]), (img_edge.shape[1], int(img_edge.shape[0]/4)), (0, int(img_edge.shape[0]/4))] ]) #create black image with the same resolution of original image mask = np.zeros_like(img_edge) #add white(255.255.255) rectange (based on 4 points which created above) to the black image cv2.fillPoly(mask,points, color=(255, 255, 255)) #mask the original image to the rectange, the black still remain masked_image = cv2.bitwise_and(img_edge, mask) #Why create mask? To minimize region of edge detect => minimize detect error #Find line points with Hough transform algorithm on masked_img lines = cv2.HoughLinesP(masked_image, rho = 2, theta = np.pi/180, threshold = 60, lines = np.array([]), minLineLength=30,

x2_avg_R = int((y2_avg_R - right_average[1])/right_average[0]) # x = (y-y_intercept)/slope #create 4 points of middle line based on average of left line and right line y1_M = img.shape[0] #start point y (full img) y2_M = 0 #end point y (full img) x1_M = int((x1_avg_L+x1_avg_R)/2) # x1_M = (x1_L + x1_R)/ x2_M = int((x2_avg_L+x2_avg_R)/2) # x2_M = (x2_L + x2_R)/ #draw line on image cv2.line(img_line,(x1_avg_L,y1_avg_L),(x2_avg_L,y2_avg_L),(255,0,0),2) cv2.line(img_line,(x1_avg_R,y1_avg_R),(x2_avg_R,y2_avg_R),(255,0,0),2) cv2.line(img_line,(x1_M,y1_M),(x2_M,y2_M),(0,0,255),2) plt.figure(1) plt.subplot(331).axis('off'),plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)),plt.title('Original image') plt.subplot(332).axis('off'),plt.imshow(cv2.cvtColor(img_gray, cv2.COLOR_BGR2RGB)),plt.title('Gray image') plt.subplot(333).axis('off'),plt.imshow(cv2.cvtColor(img_blur, cv2.COLOR_BGR2RGB)),plt.title('Blur the image') plt.subplot(334).axis('off'),plt.imshow(cv2.cvtColor(img_edge, cv2.COLOR_BGR2RGB),cmap = "gray"),plt.title('Edge detection') plt.subplot(335).axis('off'),plt.imshow(cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)),plt.title('Mask') plt.subplot(336).axis('off'),plt.imshow(cv2.cvtColor(masked_image, cv2.COLOR_BGR2RGB)),plt.title('Masked edge detect image') plt.subplot(337).axis('off'),plt.imshow(cv2.cvtColor(img_line_add, cv2.COLOR_BGR2RGB)),plt.title('Detected lines') plt.subplot(338).axis('off'),plt.imshow(cv2.cvtColor(img_line, cv2.COLOR_BGR2RGB)),plt.title('Average lines') plt.show()