当前位置:网站首页>Opencv -- Hough transform and some problems encountered
Opencv -- Hough transform and some problems encountered
2022-06-28 02:44:00 【51CTO】
Catalog
- problem 1 : Color space conversion function parameter problem :CV_BGR2GRAY vs CV_GRAY2BGR
- problem 2:cvRound()、cvFloor()、cvCeil() The usage function
- The meaning of Hough transform
- Standard Hough line transformation
- Principle and algorithm steps :
- Explanation of parameters of Hoff circle transformation function
problem 1 : Color space conversion function parameter problem :CV_BGR2GRAY vs CV_GRAY2BGR
OpenCV Color space conversion function :
dstCn Now it has been changed to COLOR_GRAY2BGR And so on COLOR At the beginning .
CV_BGR2GRAY : take RGB The graph is transformed into GRAY chart
CV_GRAY2BGR: take GRAY The graph is transformed into RGB chart
Light ink source code
cvtColor(midImage, dstImage, COLOR_GRAY2BGR);
// take canny The binary graph scanned by the operator is transformed into RGB chart , The reason is that when you visualize the Hough transform effect later , The curves drawn are colored .
problem 2:cvRound()、cvFloor()、cvCeil() The usage function
cvRound(): Returns the integer value closest to the parameter , I.e. round off ;
cvFloor(): Returns the maximum integer value not greater than the parameter , I.e. round down ;
cvCeil(): Returns the smallest integer value that is not less than the parameter , That is, round up ;
The meaning of Hough transform
I have covered this in the marginal article , Post links directly :
javascript:void(0) There is also a link I refer to :
javascript:void(0)
Standard Hough line transformation
Explanation of Hough line transformation function parameters
● The first parameter ,InputArray Type of image, The input image , Source image . Need to be 8 Bit single channel binary image
● The second parameter ,InputArray Type of lines, After calling HoughLines Function is used to store Huo The output vector of the line is detected by the Fourier transform . Each line consists of a vector with two elements ( ρ, 0) Express , among ,ρ It's from the origin of the coordinates (0,0) ( That's the upper left corner of the image ) Distance of ,θ It's the arc line rotation angle (0 Degrees indicate vertical lines ,π/2 Degrees indicate horizontal lines ).
● The third parameter ,double Type of rho, Distance precision in pixels . another - One way to express
The formula is the unit radius of the progressive dimension in line search .(Latex in /rho It indicates that the ρ )
● Fourth parameter ,double Type of theta,
Angular accuracy in radians . Another expression The method is the unit angle of the progressive dimension when searching the line .
● Fifth parameter ,int Type of threshold, Threshold parameters of accumulation plane , That is, identify a part as A line in a graph is the value it must reach in the accumulation plane . Greater than the national value threshold The line segment can be detected and returned to the result .
● Sixth parameter ,double Type of srn,, Have default values 0. For the multi-scale Hough transform , this Is the third parameter progressive dimension rho The divisor distance of . The rough accumulator progress size is directly the third parameter rho, And the precise progressive size of the accumulator is rho/sm.
● Seventh parameter ,double Type of stn, Have default values 0, For the multiscale Hough transform ,sm Represents the unit angle of the fourth parameter progressive dimension theta The divisor distance of . And if sr and stn Also for 0, That means using the classical Hough transform . otherwise , Both parameters should be positive numbers .
understand : stay XY Plane , The line has two parameters :K、B( Slope and intercept ), Visualize lines , It needs to be constructed x,y Axis , hypothesis XY The minimum accuracy of the shaft is 1, The line drawn is actually a series of discrete points . With XY Increased accuracy , The visualized data will be more and more like a line .
Empathy , stay rho/theta Plane , A straight line corresponds to a plane with rho Abscissa ,tehta Is a point of the ordinate .
rho/theta The higher the accuracy of , The more accurate the line is . I think here is the meaning of the third parameter and the fourth parameter .
We usually use rho=1,theta=1 Degree precision to construct the voting space .
// Macro defined for window title
using
namespace
cv;
using
namespace
std;
//================================== Standard Hough transform ============================================
int
main()
{
SetConsoleTextAttribute(
GetStdHandle(
STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY
|
FOREGROUND_GREEN);
// The font is green
Mat
srcImage
=
imread(
"D:\\opencv_picture_test\\ Hough transform .png");
// Judge whether the image is loaded successfully
if (
srcImage.
empty())
{
cout
<<
" Image load failed !"
<<
endl;
return
-
1;
}
else
cout
<<
" Image loaded successfully !"
<<
endl
<<
endl;
Mat
midImage,
dstImage;
Canny(
srcImage,
midImage,
50,
200,
3);
// Do this canny edge detection
//【3】 Do the Hough line transformation
vector
<
Vec2f
>
lines;
// Define a vector structure lines Used to store the set of line segment vectors
HoughLines(
midImage,
lines,
1,
CV_PI
/
180,
150,
0,
0);
//【4】 Draw each line segment in turn
for (
size_t
i
=
0;
i
<
lines.
size();
i
++)
{
float
rho
=
lines[
i][
0],
theta
=
lines[
i][
1];
Point
pt1,
pt2;
double
a
=
cos(
theta),
b
=
sin(
theta);
double
x0
=
a
*
rho,
y0
=
b
*
rho;
pt1.
x
=
cvRound(
x0
+
1000
* (
-
b));
pt1.
y
=
cvRound(
y0
+
1000
* (
a));
pt2.
x
=
cvRound(
x0
-
1000
* (
-
b));
pt2.
y
=
cvRound(
y0
-
1000
* (
a));
line(
midImage,
pt1,
pt2,
Scalar(
255,
255,
255),
1,
LINE_AA);
}
//【5】 Show the original image
imshow(
"【 The original picture 】",
srcImage);
//【6】 Image after edge detection
imshow(
"【 Image after edge detection 】",
midImage);
//【7】 Show renderings
//imshow("【 design sketch 】", dstImage);
waitKey(
0);
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
Cumulative probability Hough transform
The first parameter ,InputArray Type of image, The input image , Source image , Need to be 8 Bit single channel binary image , You can load any source diagram and change it to this format by the function , And fill it in here .
The second parameter ,InputArray Type of lines, After calling HoughLinesP Function stores the output vector of the detected line , Each line consists of a vector with four elements (x_1,y_1, x_2, y_2) Express , among ,(x_1, y_1) and (x_2, y_2) Is the end point of each detected line segment .
The third parameter ,double Type of rho, Distance precision in pixels . Another way to describe it is the unit radius of the progressive dimension in line search .
Fourth parameter ,double Type of theta, Angular accuracy in radians . Another way to describe it is the unit angle of progressive dimension in line search .
Fifth parameter ,int Type of threshold, Threshold parameters of accumulation plane , That is to say, when identifying a part as a straight line in the graph, it must reach the value in the accumulation plane . Greater than threshold threshold The line segment can be detected and returned to the result .
Sixth parameter ,double Type of minLineLength, Have default values 0, Represents the length of the lowest line segment , Line segments shorter than this setting parameter cannot be displayed .
Seventh parameter ,double Type of maxLineGap, Have default values 0, The maximum distance allowed to connect points in the same row with each other .
By setting the line length threshold , Ignore long or short straight lines .
//================================== Cumulative probability Hough transform ============================================
int
main()
{
// Read original picture
Mat
Image
=
imread(
"D:\\opencv_picture_test\\ Hough transform .png");
// Show the original picture
namedWindow(
"【 Original picture 】");
imshow(
"【 Original picture 】",
Image);
Mat
srcImage
=
Image.
clone();
Mat
midImage,
dstImage;
// Carry out edge detection and transform the gray image into RGB chart
Canny(
srcImage,
midImage,
50,
200,
3);
cvtColor(
midImage,
dstImage,
COLOR_GRAY2BGR);
// Define vector structure lines Used to store the set of line segment vectors
vector
<
Vec4i
>
lines;
// Perform Hough transform
HoughLinesP(
midImage,
lines,
1,
CV_PI
/
180,
80,
50,
10);
// Draw each line segment in the graph in turn
for (
size_t
i
=
0;
i
<
lines.
size();
i
++) {
Vec4i
l
=
lines[
i];
line(
dstImage,
Point(
l[
0],
l[
1]),
Point(
l[
2],
l[
3]),
Scalar(
0,
0,
255),
1,
LINE_AA);
}
imshow(
"【 Image after edge detection 】",
midImage);
imshow(
"【 design sketch 】",
dstImage);
waitKey(
0);
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
Hough transform circle transform
Principle and algorithm steps :

Explanation of parameters of Hoff circle transformation function
Input : Input 8-bit、 Single channel gray image . circle_storage: Circular storage bin detected . It can be a memory Repository ( In this case , A sequence of line segments is created in the repository , And the function returns ) Or a special type with a single line that contains a circle parameter / Uniserial CV_32FC3 Type matrix (CvMat*). The matrix header is modified by the function , Make it cols/rows Will contain a set of detected circles . If circle_storage It's a matrix , The number of actual circles exceeds the size of the matrix , Then the maximum possible number of circles is returned . Each circle is represented by three floating-point numbers : The coordinates of the center of the circle (x,y) Sum radius .
method:Hough Change the way , at present Only support HOUGH_GRADIENT
dp: The resolution of the accumulator image . This parameter allows the creation of an accumulator with a lower resolution than the input image .( This is because it is reasonable to think that the circles in the image will naturally be reduced to the same number as the width and height of the image ). If dp Set to 1, Then the resolution is the same ; If set to a higher value ( such as 2), The resolution of the accumulator will be reduced by this ( In this case, it is half ).dp The value of cannot be compared with 1 Small .
min_dist: This parameter is the minimum distance between two different circles that can be clearly distinguished by the algorithm .
param1: be used for Canny The upper limit of the edge threshold , The lower limit is set to half the upper limit .
param2: The threshold of the accumulator .
min_radius: Minimum circle radius .
max_radius: Maximum circle radius .
==================================
Hough transform circle transform
============================================
int
main()
{
// Load the original drawing and Mat Variable definitions
Mat
srcImage
=
imread(
"D:\\opencv_picture_test\\ Morphological operation \\ Holes .png");
Mat
midImage,
dstImage;
// Show the original image
imshow(
"【 The original picture 】",
srcImage);
// Turn to grayscale , Smooth the image
cvtColor(
srcImage,
midImage,
COLOR_BGR2GRAY);
// The image after edge detection is transformed into gray image
GaussianBlur(
midImage,
midImage,
Size(
9,
9),
2,
2);
// Fuzzy denoising
// Do the Hough circle transformation
vector
<
Vec3f
>
circles;
// Circular memory , Number of stored circles , Center coordinates and radius
HoughCircles(
midImage,
circles,
HOUGH_GRADIENT,
1.5,
10,
200,
100,
0,
0);
//inputImage circle_storage The way of Hoff transformation The resolution of the accumulator image The distance between two different circles
// Draw circles in the diagram in turn
for (
size_t
i
=
0;
i
<
circles.
size();
i
++)
{
Point
center(
cvRound(
circles[
i][
0]),
cvRound(
circles[
i][
1]));
int
radius
=
cvRound(
circles[
i][
2]);
// Draw the center of the circle
circle(
srcImage,
center,
3,
Scalar(
0,
0,
255),
3,
8,
0);
// Draw the outline of a circle
circle(
srcImage,
center,
radius,
Scalar(
0,
0,
255),
3,
8,
0);
}
// Show renderings
imshow(
"【 design sketch 】",
srcImage);
while ((
char)
waitKey(
1)
!=
'q') {}
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
Hough transform summary
1、 Why is it that when Hough transform is used to solve the linear equation , In polar coordinates
An oblique cut cannot mean perpendicular to x Axis line , Polar coordinates can express .
2、 Hough transform to solve linear equation , Compared with the using least square method to solve minimum equation of the straight line , What are the advantages and disadvantages
advantage : When there are outlier sampling points, the least square method will have a large error, but the Hough transform will not
shortcoming : Time complexity and space complexity are very high , Only the direction of the segment can be detected , The length of the line segment cannot be determined
Reference link
http://www.manongjc.com/article/42132.html https://stackoverflow.com/questions/5929125/opencv-houghcircles-param1-param2 javascript:void(0) Light ink great God blog
边栏推荐
- 【 amélioration de la correction d'image de Code bidimensionnel】 simulation du traitement d'amélioration de la correction d'image de Code bidimensionnel basée sur MATLAB
- ROS+Gazebo中红绿黄交通灯如何实现?
- The system administrator has set the system policy to prohibit this installation. Solution
- Win11无法使用动态壁纸怎么办?Win11用不了动态壁纸的解决方法
- 【历史上的今天】6 月 17 日:术语“超文本”的创造者出生;Novell 首席科学家诞生;探索频道开播
- SQL injection bypass (IV)
- 简单文件传输协议TFTP
- Flask基础:模板渲染+模板过滤使用+控制语句
- 如何以数据驱动「客户全生命周期管理」,提高线索转化率及客户满意度?
- 「大道智创」获千万级preA+轮融资,推出科技消费机器人
猜你喜欢

迪赛智慧数——柱状图(折柱混合图):2021年毕业季租房价格和房租收入比

【历史上的今天】6 月 10 日:Apple II 问世;微软收购 GECAD;发明“软件工程”一词的科技先驱出生

【历史上的今天】6 月 19 日:iPhone 3GS 上市;帕斯卡诞生;《反恐精英》开始测试

【历史上的今天】6 月 3 日:微软推出必应搜索引擎;Larry Roberts 启动阿帕网;Visual Basic 之父出生

Win11无法使用动态壁纸怎么办?Win11用不了动态壁纸的解决方法

毕业季来临,2022届高校毕业生人数首次突破千万大关

低代码DSL里面在数仓中的实践

Low code solution - a low code solution for digital after-sales service covering the whole process of work order, maintenance and Finance

Interpretation of bilstm-crf in NER forward_ algorithm

数智学习 | 流批一体实时数仓建设路径探索
随机推荐
Low code solution - a low code solution for digital after-sales service covering the whole process of work order, maintenance and Finance
yarn下载报错There appears to be trouble with your network connection. Retrying.
SQL 注入绕过(五)
ShardingSphere-proxy-5.0.0建立mysql读写分离的连接(六)
【历史上的今天】6 月 3 日:微软推出必应搜索引擎;Larry Roberts 启动阿帕网;Visual Basic 之父出生
Keil "St link USB communication error" solution
Is it safe for qiniu to open an account? How do I open an account online?
SQL reported an unusual error, which confused the new interns
Complex and inefficient logistics? Three steps to solve problems in enterprise administration
在线JSON转PlainText工具
From how to use to how to implement a promise
Wangxinling, tanweiwei Shanhai (extended version of Chorus) online audition lossless FLAC Download
一种低成本增长私域流量,且维护简单的方法
LeetCode - Easy - 197
High reliability application knowledge map of Architecture -- the path of architecture evolution
There appears to be a failure with your network connection Retrying.
文件传输协议--FTP
Anonymous Mount & named mount
我今天忘带手机了
SQL injection bypass (2)