Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
19b88a17
Commit
19b88a17
authored
Nov 28, 2013
by
Roman Donchenko
Committed by
OpenCV Buildbot
Nov 28, 2013
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1873 from abak:hough_24
parents
d5ead208
38904c9a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
80 additions
and
28 deletions
+80
-28
HoughCircle_Demo.cpp
samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp
+80
-28
No files found.
samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp
View file @
19b88a17
...
...
@@ -7,49 +7,101 @@
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using
namespace
cv
;
/**
* @function main
*/
int
main
(
int
,
char
**
argv
)
namespace
{
// windows and trackbars name
const
std
::
string
windowName
=
"Hough Circle Detection Demo"
;
const
std
::
string
cannyThresholdTrackbarName
=
"Canny threshold"
;
const
std
::
string
accumulatorThresholdTrackbarName
=
"Accumulator Threshold"
;
const
std
::
string
usage
=
"Usage : tutorial_HoughCircle_Demo <path_to_input_image>
\n
"
;
// initial and max values of the parameters of interests.
const
int
cannyThresholdInitialValue
=
200
;
const
int
accumulatorThresholdInitialValue
=
50
;
const
int
maxAccumulatorThreshold
=
200
;
const
int
maxCannyThreshold
=
255
;
void
HoughDetection
(
const
Mat
&
src_gray
,
const
Mat
&
src_display
,
int
cannyThreshold
,
int
accumulatorThreshold
)
{
// will hold the results of the detection
std
::
vector
<
Vec3f
>
circles
;
// runs the actual detection
HoughCircles
(
src_gray
,
circles
,
CV_HOUGH_GRADIENT
,
1
,
src_gray
.
rows
/
8
,
cannyThreshold
,
accumulatorThreshold
,
0
,
0
);
// clone the colour, input image for displaying purposes
Mat
display
=
src_display
.
clone
();
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
]);
// circle center
circle
(
display
,
center
,
3
,
Scalar
(
0
,
255
,
0
),
-
1
,
8
,
0
);
// circle outline
circle
(
display
,
center
,
radius
,
Scalar
(
0
,
0
,
255
),
3
,
8
,
0
);
}
// shows the results
imshow
(
windowName
,
display
);
}
}
int
main
(
int
argc
,
char
**
argv
)
{
Mat
src
,
src_gray
;
Mat
src
,
src_gray
;
if
(
argc
<
2
)
{
std
::
cerr
<<
"No input image specified
\n
"
;
std
::
cout
<<
usage
;
return
-
1
;
}
/
// Read the image
src
=
imread
(
argv
[
1
],
1
);
// Read the image
src
=
imread
(
argv
[
1
],
1
);
if
(
!
src
.
data
)
{
return
-
1
;
}
if
(
!
src
.
data
)
{
std
::
cerr
<<
"Invalid input image
\n
"
;
std
::
cout
<<
usage
;
return
-
1
;
}
/
// Convert it to gray
// Convert it to gray
cvtColor
(
src
,
src_gray
,
COLOR_BGR2GRAY
);
/
// Reduce the noise so we avoid false circle detection
// Reduce the noise so we avoid false circle detection
GaussianBlur
(
src_gray
,
src_gray
,
Size
(
9
,
9
),
2
,
2
);
vector
<
Vec3f
>
circles
;
//declare and initialize both parameters that are subjects to change
int
cannyThreshold
=
cannyThresholdInitialValue
;
int
accumulatorThreshold
=
accumulatorThresholdInitialValue
;
/// Apply the Hough Transform to find the circles
HoughCircles
(
src_gray
,
circles
,
CV_HOUGH_GRADIENT
,
1
,
src_gray
.
rows
/
8
,
200
,
100
,
0
,
0
);
// create the main window, and attach the trackbars
namedWindow
(
windowName
,
WINDOW_AUTOSIZE
);
createTrackbar
(
cannyThresholdTrackbarName
,
windowName
,
&
cannyThreshold
,
maxCannyThreshold
);
createTrackbar
(
accumulatorThresholdTrackbarName
,
windowName
,
&
accumulatorThreshold
,
maxAccumulatorThreshold
);
/// Draw the circles detected
for
(
size_t
i
=
0
;
i
<
circles
.
size
();
i
++
)
// infinite loop to display
// and refresh the content of the output image
// until the user presses q or Q
int
key
=
0
;
while
(
key
!=
'q'
&&
key
!=
'Q'
)
{
Point
center
(
cvRound
(
circles
[
i
][
0
]),
cvRound
(
circles
[
i
][
1
]));
int
radius
=
cvRound
(
circles
[
i
][
2
]);
// circle center
circle
(
src
,
center
,
3
,
Scalar
(
0
,
255
,
0
),
-
1
,
8
,
0
);
// circle outline
circle
(
src
,
center
,
radius
,
Scalar
(
0
,
0
,
255
),
3
,
8
,
0
);
}
// those paramaters cannot be =0
// so we must check here
cannyThreshold
=
std
::
max
(
cannyThreshold
,
1
);
accumulatorThreshold
=
std
::
max
(
accumulatorThreshold
,
1
);
/// Show your results
namedWindow
(
"Hough Circle Transform Demo"
,
WINDOW_AUTOSIZE
);
imshow
(
"Hough Circle Transform Demo"
,
src
);
//runs the detection, and update the display
HoughDetection
(
src_gray
,
src
,
cannyThreshold
,
accumulatorThreshold
);
// get user key
key
=
waitKey
(
10
);
}
waitKey
(
0
);
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment