Skip to content

Instantly share code, notes, and snippets.

@abdullah353
Forked from nikhil9/HoughCircle.java
Created May 29, 2014 10:52
Show Gist options
  • Select an option

  • Save abdullah353/df5dcaad6c23035f9155 to your computer and use it in GitHub Desktop.

Select an option

Save abdullah353/df5dcaad6c23035f9155 to your computer and use it in GitHub Desktop.

Revisions

  1. @nikhil9 nikhil9 created this gist Oct 31, 2012.
    38 changes: 38 additions & 0 deletions HoughCircle.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    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);

    }
    }