Hough transform
lastmod 2025-01-02created 2017-06-2820 edits
The Hough Transform is a transformation rule that extracts the parts where the lines formed by points in an image overlap the most.
The Hough transform is a term used in digital image processing, computer vision, and so on. It is used to detect straight lines in images.
OpenCV function
HoughCircles Example
# import the necessary packages
import numpy as np
import argparse
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())
# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread(args["image"])
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 100)
# ensure at least some circles were found
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle
# corresponding to the center of the circle
cv2.circle(output, (x, y), r, (0, 255, 0), 4)
cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
# show the output image
cv2.imshow("output", np.hstack([image, output]))
cv2.waitKey(0)
Run python code:
Line find example
import cv2
import numpy as np
img = cv2.imread('dave.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
lines = cv2.HoughLines(edges,1,np.pi/180,200)
for rho,theta in lines[0]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)
cv2.imwrite('houghlines3.jpg',img)
Probabilistic Hough Transform:
import cv2
import numpy as np
img = cv2.imread('dave.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)
for x1,y1,x2,y2 in lines[0]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.imwrite('houghlines5.jpg',img)
See also
- OpenCV
- Canny Edge Detection
- Image Processing
- Image Contours (image contour extraction)
- Lane Detection
- MinAreaQuadrilateral - An algorithm that makes up for the problems of cv2.minAreaRect using the Korean: 허프 변환 (used in ddrm).
Favorite site
- Wikipedia (en) Hough transform
- opencv tutorial - hough line transform
- Finding straight lines with the Hough Transform
OpenCV Guide
- [Recommended] Hough transform - gramman 0.1 documentation
- [Recommended] Python OpenCV course : Lesson 28 - Line detection
- OpenCV #7-2 Example (detecting lines in an image with the Hough transform)
- OpenCV - 23. Hough Transformation
- Hough Transform tutorial using OpenCV
- OpenCV line detection - Hough transform 1
OpenCV Official
- OpenCV: Hough Line Transform
- OpenCV: Hough Circle Transform
- OpenCV: Object detection with Generalized Ballard and Guil Hough Transform
Samples
- [Recommended] python - How to detect lines in OpenCV? - Stack Overflow - Has several examples.
References
-
Eyes33.egloos.com_-OpenCV-_Hough_transform.pdf ↩
Pages that link here