2016-11-09 9 views
2

이미지의 원을 감지하기위한 기능을 구현 중입니다. Java 용 서클을 식별하기 위해 OpenCV을 사용하고 있습니다. 그레이 스케일 이미지에는 원이 표시됩니다.OpenCV HoughCircles 서클을 감지하지 못합니다.

Mat gray = new Mat(); 
Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY); 
Imgproc.blur(gray, gray, new Size(3, 3)); 

Mat edges = new Mat(); 
int lowThreshold = 100; 
int ratio = 3; 
Imgproc.Canny(gray, edges, lowThreshold, lowThreshold * ratio); 

Mat circles = new Mat(); 
Vector<Mat> circlesList = new Vector<Mat>(); 

Imgproc.HoughCircles(edges, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 60, 200, 20, 30, 0); 

Imshow grayIM = new Imshow("grayscale"); 
grayIM.showImage(edges); 

어떤 생각이 왜 이런 경우 일 수 있습니다

여기 내 코드입니까?

+0

원본 이미지와 HoughCircle로 전달되는 이미지를 게시 할 수 있습니까? 가능하면 중간 이미지도. – barny

+0

Houghcircles는 가장자리 이미지가 아니라 회색 이미지에 사용해야합니다. – Miki

답변

0

먼저 Miki는 HughCircles를 그레이 스케일에 직접 적용해야한다고 지적했기 때문에 자체 Canny Edge 디텍터가 있습니다.

두 번째로 HughCircles의 매개 변수는 특정 유형의 이미지로 조정되어야합니다. 모든 수식에 맞는 하나의 설정이 없습니다. 원

public static void main(String[] args) { 
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 
    Mat img = Highgui.imread("circle-in.jpg", Highgui.CV_LOAD_IMAGE_ANYCOLOR); 

    Mat gray = new Mat(); 
    Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY); 
    Imgproc.blur(gray, gray, new Size(3, 3)); 

    Mat circles = new Mat(); 
    double minDist = 60; 
    // higher threshold of Canny Edge detector, lower threshold is twice smaller 
    double p1UpperThreshold = 200; 
    // the smaller it is, the more false circles may be detected 
    double p2AccumulatorThreshold = 20; 
    int minRadius = 30; 
    int maxRadius = 0; 
    // use gray image, not edge detected 
    Imgproc.HoughCircles(gray, circles, Imgproc.CV_HOUGH_GRADIENT, 1, minDist, p1UpperThreshold, p2AccumulatorThreshold, minRadius, maxRadius); 

    // draw the detected circles 
    Mat detected = img.clone(); 
    for (int x = 0; x < circles.cols(); x++) { 
     double[] c1xyr = circles.get(0, x); 
     Point xy = new Point(Math.round(c1xyr[0]), Math.round(c1xyr[1])); 
     int radius = (int) Math.round(c1xyr[2]); 
     Core.circle(detected, xy, radius, new Scalar(0, 0, 255), 3); 
    } 

    Highgui.imwrite("circle-out.jpg", detected); 
} 

입력 이미지 :

Input image with circles

감지 원이 빨간색으로되어 있습니다

Detected circles are colored red

이 일부 생성 원에 나를 위해 일한 코드를 기반으로

참고 출력 이미지에서 왼쪽에있는 흰색 원이 흰색에 매우 가까운 것으로 감지되지 않았습니다. p1UpperThreshold=20으로 설정하면됩니다.