Skip to content

Commit d5fadc2

Browse files
Merge pull request prathimacode-hub#893 from harshved/featureBranch
Added Code to Detect Lane from Image
2 parents 52681a3 + 3382e51 commit d5fadc2

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed
Loading
Loading
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Package/Script Name
2+
3+
## Aim
4+
5+
The main aim of the project is to detect the Lane from the image.
6+
7+
## Purpose
8+
9+
Purpose of the script is to detect Lane from the Image and make it more and more accurate.
10+
11+
## Short description of package/script
12+
13+
- This project help us to detect the Road Lane.
14+
- Libraries Imported :
15+
- CV2
16+
- Matplotlib
17+
- Numpy
18+
19+
20+
## Workflow of the Project
21+
22+
- Here Firstly the image is read from Images folder using OpenCV library.
23+
- Setting the region of interest between which the road lane is their. (mentioned data is according to the given input, If u want to try your own input image try to change the value and after testing fix the region).
24+
- Converting the image to grey scale image.
25+
- Using canny's edge detection function, detecting the edge.
26+
- Using HoughLines function detecting the points from the canny's detected image.
27+
- At last drawing the line and showing the output as Detected_Lane.jpg file.
28+
29+
30+
31+
## Setup instructions
32+
33+
First install the above mentioned library, If you want to detect lane on your given input just changge the path in cv2.imread function and set your path and run the file.
34+
35+
## Compilation Steps:
36+
- Install CV2, Matplotlib and Numpy library in your system. (syntax : pip install library_name)
37+
- Clone the project on local system.
38+
- Execute/Run .py file and get the output.
39+
40+
## Output
41+
- Input Image
42+
- ![road](https://user-images.githubusercontent.com/69030530/127738802-a2c310ba-e59e-4c2f-9960-16bd2fc4faab.jpg)
43+
44+
- Output Image
45+
- ![Detected Lane](https://user-images.githubusercontent.com/69030530/127738814-3cc4f9c3-eca5-4cb1-a2e2-8bcb91b7bf36.jpg)
46+
47+
48+
49+
## Author(s)
50+
51+
Harsh Ved
52+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#Importing the library
2+
import matplotlib.pylab as plt
3+
import cv2
4+
import numpy as np
5+
6+
#Function to find region of interest
7+
def region_of_interest(img, vertices):
8+
mask = np.zeros_like(img)
9+
match_mask_color = 255
10+
cv2.fillPoly(mask, vertices, match_mask_color)
11+
masked_image = cv2.bitwise_and(img, mask)
12+
return masked_image
13+
14+
#Function to draw the line on output image
15+
def draw_the_lines(img, lines):
16+
img_copy = np.copy(img)
17+
blank_image = np.zeros((img_copy.shape[0], img_copy.shape[1], 3), np.uint8)
18+
19+
for line in lines:
20+
for x1, y1, x2, y2 in line:
21+
cv2.line(blank_image, (x1,y1), (x2,y2), (0, 255, 0), thickness=10)
22+
23+
img = cv2.addWeighted(img, 0.8, blank_image, 1, 0.0)
24+
return img
25+
26+
#Fetching the image and printing the shape
27+
image = cv2.imread('image/road.jpg')
28+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
29+
30+
print(image.shape)
31+
height = image.shape[0]
32+
width = image.shape[1]
33+
34+
#setting the region of interest
35+
region_of_interest_vertices = [
36+
(50, height),
37+
(width/2.2, height/3),
38+
(width-240, height)
39+
]
40+
41+
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
42+
43+
#adjusting the threshold value
44+
canny_image = cv2.Canny(gray_image, 20, 100)
45+
cropped_image = region_of_interest(canny_image,
46+
np.array([region_of_interest_vertices], np.int32),)
47+
48+
lines = cv2.HoughLinesP(cropped_image,
49+
rho=2,
50+
theta=np.pi/260,
51+
threshold=100,
52+
lines=np.array([]),
53+
minLineLength=100,
54+
maxLineGap=50)
55+
image_with_lines = draw_the_lines(image, lines)
56+
57+
plt.imshow(image_with_lines)
58+
plt.show()
59+
60+
cv2.imwrite("Detected_Lane.jpg",image_with_lines)
61+
cv2.waitKey(0)
62+
cv2.destroyAllWindows()
63+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The libraries required to install and import in the project are:
2+
- CV2
3+
- Matplotlib
4+
- Numpy

0 commit comments

Comments
 (0)