2016-11-09 68 views
2

私はイメージの円を検出するために関数を実装しています。私はOpenCVをJava用に使ってサークルを識別しています。グレースケール画像は円を示す。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

エッジ画像ではなく、灰色の画像上にハーフサークルを使用する必要があります – Miki

答えて

0

最初に、Mikiが指摘したように、HughCirclesはグレースケールに直接適用する必要がありますが、独自のCanny Edge検出器があります。

第2に、HughCirclesのパラメータは、特定の種類の画像に合わせて調整する必要があります。すべての数式に適合する1つの設定はありません。円と

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を設定すると表示されます。

関連する問題