Updated April 10, 2023
Introduction to OpenCV Hough Transform
The following article provides an outline for OpenCV Hough Transform. A feature extraction method used to detect the simple shapes such as lines, circles etc. in an image is called hough transform and such simple shapes can be represented by parameters like slope and intercept are the two parameters to represent a line and the center coordinates and radius are the three parameters to represent a circle and in hough transform we make use of HoughLines() function and HoughLinesP() function to detect the lines in an image and we make use of HoughCircles() function to detect circles in an image and all these functions return an image with lines and circles detected in the image.
Syntax of OpenCV Hough Transform
Syntax to define HoughLines() function and HoughLinesP() function in OpenCV:
HoughLines(edges, lines, rho, theta, threshold)
HoughLinesP(edges, lines, rho, theta, threshold)
Where,
- edges represent the edges found using edge detector.
- lines represent the vector in which the start of the line coordinates and end of the lines coordinates is stored.
- rho represents the resolution parameter p in pixels.
- theta represents the resolution parameter theta in radians.
- threshold represents the minimum number of intersecting points needed to detect a line.
P in HoughLinesP() function stands for Probabilistic hough transform.
Syntax to define HoughCircles() function in OpenCV:
HoughCircles(input_image, method, inverse_ratio, min_distance, parameter1_and_parameter2, min_radius, max_radius)
Where,
- input_image is the source image in which the circles are to be detected.
- method represents the method used for detection of circles.
- inverse_ratio represents the inverse ratio of resolution of accumulator and the resolution of the image.
- min_distance represents the minimum distance between the centres of the circles detected.
- parameter1_and parameter2 represents the two parameters specific to the method.
- min_radius represents the minimum radius value of the circle to be detected.
- max_radius represents the maximum radius value of the circle to be detected.
Working of Hough Transform in OpenCV
- Simple shapes like lines, circles etc. in a given image can be detected using a feature extraction method called hough transform.
- The lines in a given image can be detected using HoughLines() function and HoughLinesP() function.
- Detection of lines in a given image works by first initializing the accumulator.
- Then the edges are detected using any of the edge detector methods.
- Then the pixels forming the edges are voted.
- The circles in a given image can be detected using HoughCircles() function.
- Detection of circles in a given image works by first finding the edges in the given image using any of the edge detector methods.
- Then the threshold is set for the maximum radius value and minimum radius value.
- Then the 3 dimensional accumulator array collects the evidence for presence of circle in the image.
- The HoughLines() function and HoughLinesP() function returns an image with the lines detected in the image.
- The HoughCircles() function returns an image with the circles detected in the image.
Examples of OpenCV Hough Transform
Given below are the examples of OpenCV Hough Transform:
Example #1
OpenCV program in python to perform hough transform on a given image to determine the lines present in the image using HoughLinesP() function and display the resulting image as the output on the screen.
Code:
#importing the required modules
import cv2
import numpy as np
#reading the image in which the lines must be detected using imread() function
Imageread = cv2.imread('C:/Users/admin/Desktop/images/boxes.jpg', cv2.IMREAD_COLOR)
#converting the input image to grayscale image using cvtColor() function
imagegray = cv2.cvtColor(imageread, cv2.COLOR_BGR2GRAY)
#finding the edges in the image using canny detector
resultedges = cv2.Canny(imagegray, 50, 200)
#using HoughLinesP() function to detect points on the lines present in the image
line_points = cv2.HoughLinesP(resultedges, 1, np.pi/180, 70, minLineLength=10, maxLineGap=250)
#using for loop to draw the lines by joining the intersecting points
for eachline in line_points:
x1, y1, x2, y2 = eachline[0]
cv2.line(imageread, (x1, y1), (x2, y2), (255, 0, 0), 3)
#displaying the resulting image as the output on the screen
cv2.imshow("Resulting_image", imageread)
cv2.waitKey(0)
Output:
In the above program, the necessary modules are imported. Then we reading the image in which the lines must be detected using imread() function. Then we are converting the input image to grayscale image using cvtColor() function. Then we are finding the edges in the image using canny detector. Then we are using HoughLinesP() function to detect points on the lines present in the image. Then we are using for loop to draw the lines by joining the intersecting points. Then we are displaying the resulting image as the output on the screen.
Example #2
OpenCV program in python to perform hough transform on a given image to determine the circles present in the image using HoughCircles() function and display the resulting image as the output on the screen.
Code:
#importing the required modules
import cv2
import numpy as np
#reading the image in which the circles must be detected using imread() function
Imageread = cv2.imread('C:/Users/admin/Desktop/images/oranges.jpg', cv2.IMREAD_COLOR)
#converting the input image to grayscale image using cvtColor() function
imagegray = cv2.cvtColor(imageread, cv2.COLOR_BGR2GRAY)
#using medianBlur() function on the input image to remove the noise from the image
imageblur = cv2.medianBlur(imagegray, 5)
#using HoughCircles() function to detect circles present in the image
circles = cv2.HoughCircles(imageblur, cv2.HOUGH_GRADIENT, 1, imageread.shape[0]/64, param1=100, param2=10, minRadius=20, maxRadius=30)
#drawing the detected circles on the input image
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
cv2.circle(imageread, (i[0], i[1]), i[2], (0, 255, 0), 2)
cv2.circle(imageread, (i[0], i[1]), 2, (0, 0, 255), 3)
#displaying the resulting image as the output on the screen
cv2.imshow("Resulting_image", imageread)
cv2.waitKey(0)
Output:
In the above program, the necessary modules are imported. Then we reading the image in which the circles must be detected using imread() function. Then we are converting the input image to grayscale image using cvtColor() function. Then we are using medianBlur() function on the input image to remove the noise from the image. Then we are using HoughCircles() function to detect the circles present in the image. Then we are using for loop to draw the detected circles on the input image. Then we are displaying the resulting image as the output on the screen.
Conclusion
In this article, we have seen the concept of hough transform using HoughLines() function and HoughLinesP() function to detect the lines in a given image and HoughCircles() function to detect the circles in a given image with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
We hope that this EDUCBA information on “OpenCV Hough Transform” was beneficial to you. You can view EDUCBA’s recommended articles for more information.