| ★ wanayoo — archive 1999 https://www.thepythoncode.com/article/detect-shapes-hough-transform-opencv-python | Nouvelle recherche | Portail wanayoo |
In the previous tutorial, we have seen how you can detect edges in an image. However, that's not usually enough in the image processing phase. In this tutorial, you will learn how you can detect shapes (mainly lines) in images using Hough Transform technique in Python using OpenCV library.
The Hough Transform is a popular feature extraction technique to detect any shape within an image. It is mainly used in image analysis, computer vision and image recognition.
Let's get started, installing the requirements:
pip3 install opencv-python numpy matplotlib
Importing the modules:
import numpy as np
import matplotlib.pyplot as plt
import cv2
I'm gonna use a photo of a computer monitor, make sure you have a photo "monitor.jpg" in your current directory:
# read the image
image = cv2.imread("monitor.jpg")
We need to convert this image to gray scale for edge detection:
# convert to grayscale
grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Let's detect the edges of the image:
# perform edge detection
edges = cv2.Canny(grayscale, 30, 100)
If you're not sure what cv2.Canny is doing, refer to this tutorial.
Now we have detected the edges in the image, it is suited for us to use hough transform to detect the lines:
# detect lines in the image using hough lines technique
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 60, np.array([]), 50, 5)
cv2.HoughLinesP() function finds line segments in a binary image using the probabilistic Hough transform. For more information about its parameters, check this tutorial.
Let's draw the lines:
# iterate over the output lines and draw them
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(image, (x1, y1), (x2, y2), (20, 220, 20), 3)
Showing the image:
# show the image
plt.imshow(image)
plt.show()
Here is my output:

The green lines are the lines we just drew, feel free to tweak the parameters to get better performance.
Here is the full code for detecting lines in your live camera:
import numpy as np
import matplotlib.pyplot as plt
import cv2
cap = cv2.VideoCapture(0)
while True:
_, image = cap.read()
# convert to grayscale
grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# perform edge detection
edges = cv2.Canny(grayscale, 30, 100)
# detect lines in the image using hough lines technique
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 60, np.array([]), 50, 5)
# iterate over the output lines and draw them
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(image, (x1, y1), (x2, y2), (255, 0, 0), 3)
cv2.line(edges, (x1, y1), (x2, y2), (255, 0, 0), 3)
# show images
cv2.imshow("image", image)
cv2.imshow("edges", edges)
if cv2.waitKey(1) == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
Alright, that's it for this tutorial. You can also detect circles using cv2.HoughCircles function !
Here are some references:
RELATED: How to Detect Human Faces in Python using OpenCV.
Check the full code.
Happy Coding ♥
View Full Code