Skip to content

Instantly share code, notes, and snippets.

@nikhil9
Created October 31, 2012 16:52
Show Gist options
  • Save nikhil9/3988244 to your computer and use it in GitHub Desktop.
Save nikhil9/3988244 to your computer and use it in GitHub Desktop.
Hough Circle detection in Javacv
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
public class circleDetection{
public static void main(String[] args){
IplImage src = cvLoadImage("img2.png");
IplImage gray = cvCreateImage(cvGetSize(src), 8, 1);
cvCvtColor(src, gray, CV_BGR2GRAY);
cvSmooth(gray, gray, CV_GAUSSIAN, 3);
CvMemStorage mem = CvMemStorage.create();
CvSeq circles = cvHoughCircles(
gray, //Input image
mem, //Memory Storage
CV_HOUGH_GRADIENT, //Detection method
1, //Inverse ratio
100, //Minimum distance between the centers of the detected circles
100, //Higher threshold for canny edge detector
100, //Threshold at the center detection stage
15, //min radius
500 //max radius
);
for(int i = 0; i < circles.total(); i++){
CvPoint3D32f circle = new CvPoint3D32f(cvGetSeqElem(circles, i));
CvPoint center = cvPointFrom32f(new CvPoint2D32f(circle.x(), circle.y()));
int radius = Math.round(circle.z());
cvCircle(src, center, radius, CvScalar.GREEN, 6, CV_AA, 0);
}
cvShowImage("Result",src);
cvWaitKey(0);
}
}
@edess
Copy link

edess commented Mar 16, 2015

Hello ! sorry to disturb,.

I am trying your code, put in my case the new green circle drawn on the src image is not around the existing image!

Do you know with parameter , I should play with to make the green circle match my existing image?

Thanks!
Elder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment