在opencv中的霍夫圆变换没有检测到任何圆圈

时间:2016-08-04 17:20:44

标签: java opencv

我正在编写一个程序,使用Houghcircles()方法检测图像中特定颜色的圆圈。

Imgproc.HoughCircles(thresholdedImg,circles,Imgproc.CV_HOUGH_GRADIENT,1d,100,200,100,1,100);

打印圆圈Mat在这个声明给我这个,即使我运行程序以获得完美圆圈的图像:

Mat [ 0*0*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0x894140, dataAddr=0x0 ]

该方法显然没有圆圈,因为行数和列数为零。

以下是整个代码:

public static void main(String args[]) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    Mat srcImg = imread("img.png");

    Mat thresholdedImg = new Mat(srcImg.rows(),srcImg.cols(),CvType.CV_8UC1);

    byte black[] = {0,0,0};
    byte w = getUnsignedByte(255);
    byte white[] = {w,w,w};

    double requiredBlue[] = {225.0,105.0,65.0}; // BGR values of shade to be detected


    for(int i=0;i<srcImg.rows();i++) {
        for(int j=0;j<srcImg.cols();j++) {
            double currentPixel[] = srcImg.get(i,j);    
            if(currentPixel[0]==requiredBlue[0] && currentPixel[1]==requiredBlue[1] && currentPixel[2]==requiredBlue[2])
                thresholdedImg.put(i, j, black);
            else
                thresholdedImg.put(i, j, white);
        }
    }

    Mat circles = new Mat(srcImg.rows(),srcImg.cols(),CvType.CV_8UC1);

    Imgproc.HoughCircles(thresholdedImg,circles,Imgproc.CV_HOUGH_GRADIENT,1d,100,200,100,1,100);

    displayImage(Mat2BufferedImage(circles));
}

最后一行中的Mat2BufferedImage()因为空Mat而抛出异常,如预期的那样:

  

线程“main”中的异常java.lang.IllegalArgumentException:Width(0)和height(0)必须是&gt; 0

我用非空垫测试了Mat2BufferedImage()并且它有效。如果我也应该发布它,请告诉我。

我在发布之前寻找解决方案,我遇到的所有帖子都与不正确的检测有关;没有人有Houghcircles()生成一个空的Mat。

Here is the image I get after thresholding

0 个答案:

没有答案