To write a Python program to detect the lines using Hough Transform.
Anaconda - Python 3.7
Import the required modules.
Import the image to operate on.
Convert the imported image from BGR to GRAYSCALE.
Find the edges using canny edge detector and display the image.
Detect the points that form a line using hough transform.
Draw the lines on the image
Display the output
End the program.
# Read image and convert it to grayscale image
import cv2
import matplotlib.pyplot as plt
import numpy as np
image = cv2.imread("suduko.webp")
grayImage = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
plt.imshow(grayImage)
cv2.imshow("Original Image",image)
cv2.imshow("Gray Image",grayImage)
# Find the edges in the image using canny detector and display
import cv2
import matplotlib.pyplot as plt
import numpy as np
image = cv2.imread("sudo.png")
grayImage = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
plt.imshow(grayImage,'gray')
cv2.imshow("Original Image",image)
cv2.imshow("Gray Image",grayImage)
# Detect points that form a line using HoughLinesP
lines = cv2.HoughLinesP(cannyEdges,1,np.pi/180,threshold=80,minLineLength = 50,maxLineGap = 250)
# Draw lines on the image
for line in lines:
x1, y1, x2, y2 = line [0]
cv2.line(image,(x1, y1),(x2, y2),(255, 0, 0),3)
# Display the result
plt.title("Hough Transform")
plt.imshow(image,'gray')
plt.show()
Thus the program is written with python and OpenCV to detect lines using Hough transform.