无法在android opencv中将彩色图像转换为灰度

时间:2015-04-21 12:09:13

标签: java android opencv

这是代码中不能正常工作的部分。我知道这个问题早些时候已被提出过,但我已经做了我能做的一切,但仍然无法弄清楚原因。这是代码的一部分

    Mat imgSource = original_image.clone();

    // apply gaussian blur to smoothen lines of dots
    Log.i(TAG, "Blur");
    Imgproc.GaussianBlur(imgSource, imgSource, new Size(5, 5), 5);

    // convert the image to black and white
    Log.i(TAG, "B&W");
    Log.i(TAG,Integer.toString(imgSource.type()));
    Log.i(TAG,Integer.toString(imgSource.channels()));

    Mat gray = new Mat(imgSource.size(),CvType.CV_8UC1);
    Imgproc.cvtColor(imgSource, gray, Imgproc.COLOR_BGR2GRAY);

    // convert the image to black and white does (8 bit)
    Log.i(TAG, "Canny");
    Imgproc.Canny(gray, gray, 50, 50);

    // find the contours
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Log.i(TAG, "Contours");
    Imgproc.findContours(gray, contours, new Mat(), Imgproc.RETR_LIST,
            Imgproc.CHAIN_APPROX_SIMPLE);
    Log.i(TAG,"Contours done");

    //if no contours are detected
    if(contours.size() == 0){
        Log.i(TAG,"contour size is 0");
        return gray;
    }

在我的Logcat中,我看到消息&#34;轮廓大小为0&#34;。

当我加载灰色垫子时,我看到一个彩色图像,这解释了为什么它们没有检测到轮廓。

请提出建议。

1 个答案:

答案 0 :(得分:0)

您好我认为您可以从代码中删除此行:

Imgproc.Canny(gray, gray, 50, 50);