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.
import numpy as np
import cv2
import matplotlib.pyplot as plt
img=cv2.imread("peacock.jpg",0)
img_c=cv2.imread("peacock.jpg",1)
img_c=cv2.cvtColor(img_c,cv2.COLOR_BGR2RGB)
gray=cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
gray = cv2.GaussianBlur(gray,(3,3),0)
plt.figure(figsize=(13,13))
plt.subplot(1,2,1)
plt.imshow(img_c)
plt.title("Original Image")
plt.axis("off")
plt.subplot(1,2,2)
plt.imshow(gray)
plt.title("Gray Image")
plt.axis("off")
plt.show()
canny=cv2.Canny(gray,120,150)
plt.imshow(canny)
plt.title("Canny Edge Detector")
plt.axis("off")
plt.show()
lines=cv2.HoughLinesP(canny,1,np.pi/180,threshold=80,minLineLength=50,maxLineGap=250)
for line in lines:
x1,y1,x2,y2=line[0]
cv2.line(img_c,(x1,y1),(x2,y2),(255,0,0),3)
plt.imshow(img_c)
plt.title("Result Image")
plt.axis("off")
plt.show()
Thus the program is written with python and OpenCV to detect lines using Hough transform.