Загрузка
Prev Tutorial: Hough Line Transform
Next Tutorial: Object detection with Generalized Ballard and Guil Hough Transform
| Original author | Ana Huamán |
| Compatibility | OpenCV >= 3.0 |
In this tutorial you will learn how to:
In the line detection case, a line was defined by two parameters \((r, \theta)\). In the circle case, we need three parameters to define a circle:
The sample code that we will explain can be downloaded from here. A slightly fancier version (which shows trackbars for changing the threshold values) can be found here.
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
const char* filename = argc >=2 ? argv[1] : "smarties.png";
// Loads an image
Mat src = imread( samples::findFile( filename ), IMREAD_COLOR );
// Check if image is loaded fine
(.()){
();
(, );
;
}
;
(, , );
(, , );
<> ;
(, , , ,
./,
, , ,
);
( = ; < .(); ++ )
{
= [];
= ([], []);
( , , , (,,), , );
= [];
( , , , (,,), , );
}
(, );
();
;
}The sample code that we will explain can be downloaded from here.
package sample;
import org.opencv.core.*;
import org.opencv.core.Point;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
class HoughCirclesRun {
public void run(String[] args) {
String default_file = "../../../../data/smarties.png";
String filename = ((args.length > 0) ? [] : );
= .(, .);
( .() ) {
..();
..(
+ +);
.(-);
}
= ();
.(, , .);
.(, , );
= ();
.(, , ., ,
().()/,
, , , );
( = ; < .(); ++) {
[] = .(, );
= (.([]), .([]));
.(, , , (,,), , , );
= () .([]);
.(, , , (,,), , , );
}
.(, );
.();
.();
}
}
{
([] ) {
.(.);
().();
}
}The sample code that we will explain can be downloaded from here.
import sys
import cv2 as cv
import numpy as np
def main(argv):
default_file = 'smarties.png'
filename = argv[0] if len(argv) > 0 else default_file
# Loads an image
src = cv.imread(cv.samples.findFile(filename), cv.IMREAD_COLOR)
# Check if image is loaded fine
:
()
( + + )
-
= .(, .)
= .(, )
= .[]
= .(, ., , / ,
=, =,
=, =)
:
= .(.())
[, :]:
= ([], [])
#
.(, , , (, , ), )
#
= []
.(, , , (, , ), )
.(, )
.()
== :
(.[:])The image we used can be found here
const char* filename = argc >=2 ? argv[1] : "smarties.png";
// Loads an image
Mat src = imread( samples::findFile( filename ), IMREAD_COLOR );
// Check if image is loaded fine
if(src.empty()){
printf(" Error opening image\n");
printf(" Program Arguments: [image_name -- default %s] \n", filename);
return EXIT_FAILURE;
} String default_file = "../../../../data/smarties.png";
String filename = ((args.length > 0) ? args[0] : default_file);
// Load an image
Mat src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR);
// Check if image is loaded fine
if( src.empty() ) {
System.out.println("Error opening image!");
System.out.println("Program Arguments: [image_name -- default "
+ default_file +"] \n");
System.exit(-1);
} default_file = 'smarties.png'
filename = argv[0] if len(argv) > 0 else default_file
# Loads an image
src = cv.imread(cv.samples.findFile(filename), cv.IMREAD_COLOR)
# Check if image is loaded fine
if src is None:
print ('Error opening image!')
print ('Usage: hough_circle.py [image_name -- default ' + default_file + '] \n')
return -1 Mat gray;
cvtColor(src, gray, COLOR_BGR2GRAY); Mat gray = new Mat();
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY); gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) medianBlur(gray, gray, 5); Imgproc.medianBlur(gray, gray, 5); gray = cv.medianBlur(gray, 5) vector<Vec3f> circles;
HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
gray.rows/16, // change this value to detect circles with different distances to each other
100, 30, 1, 30 // change the last two parameters
// (min_radius & max_radius) to detect larger circles
); Mat circles = new Mat();
Imgproc.HoughCircles(gray, circles, Imgproc.HOUGH_GRADIENT, 1.0,
(double)gray.rows()/16, // change this value to detect circles with different distances to each other
100.0, 30.0, 1, 30); // change the last two parameters
// (min_radius & max_radius) to detect larger circles rows = gray.shape[0]
circles = cv.HoughCircles(gray, cv.HOUGH_GRADIENT, 1, rows / 8,
param1=100, param2=30,
minRadius=1, maxRadius=30) for( size_t i = 0; i < circles.size(); i++ )
{
Vec3i c = circles[i];
Point center = Point(c[0], c[1]);
// circle center
circle( src, center, 1, Scalar(0,100,100), 3, LINE_AA);
// circle outline
int radius = c[2];
circle( src, center, radius, Scalar(255,0,255), 3, LINE_AA);
} for (int x = 0; x < circles.cols(); x++) {
double[] c = circles.get(0, x);
Point center = new Point(Math.round(c[0]), Math.round(c[1]));
// circle center
Imgproc.circle(src, center, 1, new Scalar(0,100,100), 3, 8, 0 );
// circle outline
int radius = (int) Math.round(c[]);
.(, , , (,,), , , );
} if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
center = (i[0], i[1])
# circle center
cv.circle(src, center, 1, (0, 100, 100), 3)
# circle outline
radius = i[2]
cv.circle(src, center, radius, (255, 0, 255), 3)You can see that we will draw the circle(s) on red and the center(s) with a small green dot
imshow("detected circles", src);
waitKey(); HighGui.imshow("detected circles", src);
HighGui.waitKey(); cv.imshow("detected circles", src)
cv.waitKey(0)The result of running the code above with a test image is shown below:
where \((x_{center}, y_{center})\) define the center position (green point) and \(r\) is the radius, which allows us to completely define a circle, as it can be seen below: