[Learning OpenCV4] Hough Transform Summary

The content shared in this article comes from the book "Learning OpenCV 4: Python-based Algorithm Combat". The content of the book is as follows:

1章 OpenCV快速入门;2章 图像读写模块imgcodecs;3章 核心库模块core;4章 图像处理模块imgproc(一);5章 图像处理模块imgproc(二);6章 可视化模块highgui;7章 视频处理模块videoio;8章 视频分析模块video;9章 照片处理模块photo;102D特征模块features2d;11章 相机标定与三维重建模块calib3d;12章 传统目标检测模块objdetect;13章 机器学习模块ml;14章 深度神经网络模块dnn

Welcome to the books "Deep Learning Computer Vision Combat" and "Learning OpenCV4: Python-based Algorithm Combat".

insert image description here

Hough transform is one of the methods for identifying basic shapes (such as lines and circles) in image processing. OpenCV encapsulates algorithms for detecting lines and circles using Hough transform.

5.1.1 Case 48: Hough Line Transform

Three Hough line transforms are supported in OpenCV, namely standard Hough transform and multi-scale Hough transform (HoughLines function) and cumulative probability Hough transform (HoughLinesP function).
The definition of the HoughLines function is as follows:

lines = HoughLines(image, rho, theta, threshold, lines=None, srn=None, stn=None, min_theta=None, max_theta=None)

The parameter descriptions are as follows:
image, the input image, the image must be an 8-bit single-channel binary image;
rho, the distance resolution, in pixels;
theta, the angle resolution, in radians;
threshold, the threshold of the accumulated plane Parameters;
lines, transform detected lines (return value);
srn, for multi-scale Hough transform, it is the divisor distance of distance resolution rho;
stn, for multi-scale Hough transform, it is angle-resolved Divisor distance of rate theta;
min_theta, check the minimum angle of the line, between 0 and max_theta;
max_theta, check the maximum angle of the line, between min_theta and CV_PI.
The input image used in this example is shown in Figure 5.1.
insert image description here

Figure 5.1
The case code for line detection using standard Hough transform is as follows:

import cv2
import numpy as np

img = cv2.imread('chess_board.jpg')
draw_lines = np.zeros(img.shape[:], dtype=np.uint8)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Canny边缘检测
edges = cv2.Canny(gray, 50, 150)
#经典霍夫线变换
lines = cv2.HoughLines(edges, 1, np.pi / 180, 150)

#绘制检测到的直线
for line in lines:
    rho, theta = line[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(draw_lines, (x1, y1), (x2, y2), (255, 255, 255))

#图像显示
cv2.imshow("draw_lines", draw_lines)
cv2.waitKey(0)
cv2.destroyAllWindows()

The result of the straight line detected by the standard Hough transform is drawn as shown in Figure 5.2.
insert image description here

Figure 5.2 The
cumulative probability Hough transform function HoughLinesP is defined as follows:

lines = HoughLinesP(image, rho, theta, threshold, lines=None, minLineLength=None, maxLineGap=None)

The parameter descriptions are as follows:
image, the input image, the image must be an 8-bit single-channel binary image;
rho, the distance resolution, in pixels;
theta, the angle resolution, in radians;
threshold, the threshold of the accumulated plane Parameters;
lines, the detected straight line (return value);
minLineLength, the length of the small line segment;
maxLineGap, the maximum distance between two points on the same line; the
cumulative probability Hough transform case code is as follows:

import cv2
import numpy as np

img = cv2.imread('chess_board.jpg')
draw_lines = np.zeros(img.shape[:], dtype=np.uint8)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Canny边缘检测
edges = cv2.Canny(gray, 50, 150)
#累积概率霍夫线变换
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 90, minLineLength=50, maxLineGap=10)
#绘制检测到的线
for line in lines:
    x1, y1, x2, y2 = line[0]
    cv2.line(draw_lines, (x1, y1), (x2, y2), (255, 255, 255), 1, lineType = cv2.LINE_AA)

#图像显示
cv2.imshow("draw_lines", draw_lines)
cv2.waitKey(0)
cv2.destroyAllWindows()

The detected line drawing results are shown in Figure 5.3.
insert image description here

Figure 5.3
The smaller the threshold parameter in the HoughLines function is set, the more lines will be detected. The same is true when the minLineLength and maxLineGap in the HoughLinesP function are set. The smaller the setting is, the more lines will be detected. The prior knowledge of the image sets appropriate parameters, so that a more accurate detection effect can be achieved.

5.1.2 Case 49: Hough Circle Transform

The function HoughCircles of Hough circle transform is provided in OpenCV. The definition of this function is as follows:

circles = HoughCircles(image, method, dp, minDist, circles=None, param1=None, param2=None, minRadius=None, maxRadius=None)

The parameters are explained as follows:
image, input image, 8-bit single-channel binary image needs to be input;
method, detection method;
dp, inverse ratio of accumulator resolution and image resolution;
minDist, minimum distance between circle centers;
circles, the found circle (return value)
param1, the high threshold of the Canny edge detection algorithm, and the low threshold is half of it;
param2, the accumulator threshold, indicating the threshold for determining a circle;
minRadius, the minimum radius of the circle to be detected ;
maxRadius, the maximum radius of the circle to be detected.
The input image used in this case is shown in Figure 5.4.
insert image description here

The case code of Figure 5.4
Hough circle transform is as follows:

import cv2
import numpy as np

img = cv2.imread('circle_src.jpg')
#创建纯黑色图片
draw_circle = np.zeros(img.shape[:], dtype=np.uint8)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Canny边缘检测
edges = cv2.Canny(gray, 50, 150)
# 霍夫圆变换
circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, 1, 100)
#浮点数转整型数
circles = np.int0(np.around(circles))

# 将检测的圆绘制出来
for i in circles[0, :]:
    cv2.circle(draw_circle, (i[0], i[1]), i[2], (255, 255, 255), 2)
#图像显示
cv2.imshow("edges", edges)
cv2.imshow("draw_circle", draw_circle)
cv2.waitKey(0)
cv2.destroyAllWindows()

The result of edge detection using the Canny algorithm is shown in Figure 5.5.
insert image description here

Figure 5.5
The result of the circle detected using the Hough circle transform is shown in Figure 5.6.
insert image description here

Figure 5.6
The detection effect in Figure 5.6 is unsatisfactory, and many unexpected circles are detected and drawn. In this case, if the parameter dp=1 is set, the accumulator has the same resolution as the input image; the set minDist is 100, that is, the minimum distance between the centers of the detected circles is 100. According to the information of the circle in Figure 5.35, the input parameters can be adjusted, set the minimum circle radius minRadius to 130, and the maximum circle radius maxRadius to 180, as shown below:

circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, 1, 100, param2=30, minRadius=130, maxRadius=180)

The test results after setting new parameters are shown in Figure 5.7, and the test results are more accurate.
insert image description here

Figure 5.7

Guess you like

Origin blog.csdn.net/lxiao428/article/details/123081529