To write a Python program to detect the lines using Hough Transform.
Anaconda - Python 3.7
Import all the necessary modules for the program.
Load a image using imread() from cv2 module.
Convert the image to grayscale.
Using Canny operator from cv2,detect the edges of the image.
Using the HoughLinesP(),detect line co-ordinates for every points in the images.
Using For loop,draw the lines on the found co-ordinates.
Display the image.
Developed by: Shafeeq Ahamed. S
Register Number: 212221230092
import cv2
import numpy as np
import matplotlib.pyplot as plt
image=cv2.imread("road.png",1)
plt.imshow(image)
plt.title('Original')
plt.axis('off')
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
plt.imshow(image,cmap="gray")
plt.title('gray')
plt.axis('off')
img = cv2.GaussianBlur(src = gray, ksize = (15,15), sigmaX=0,sigmaY=0)
plt.imshow(img)
plt.title('EDGES')
plt.axis('off')
edges = cv2.Canny(image, 120, 150)
plt.imshow(edges)
plt.title('EDGES')
plt.axis('off')
lines=cv2.HoughLinesP(edges,1,np.pi/180,threshold=80,minLineLength=50,maxLineGap=250)
for line in lines:
x1,y1,x2,y2=line[0]
cv2.line(image,(x1,y1),(x2,y2),(0,0,205),2)
plt.imshow(image)
plt.title('HOUGH')
plt.axis('off')
Gray Scale | Gaussian Filter |
---|---|
Canny Edge | Hough transform |
---|---|
Thus the program is written with python and OpenCV to detect lines using Hough transform.