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

Java 无法在android opencv中将彩色图像转换为灰度,java,android,opencv,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);

这是代码中工作不好的部分。我知道之前有人问过这个问题,但我已经尽了我所能,但仍然无法找出原因。这是代码的一部分

    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中,我看到消息轮廓大小为0

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


请提出一些建议

您好,我想您可以从代码中删除这一行:

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

不,即使删除canny,轮廓大小为0,加载灰色mat后显示彩色图像。我猜问题出在Imgproc.cvtColorimgSource,gray,Imgproc.COLOR\u BGR2GRAY。当加载灰色垫子时,该行后面显示的是彩色图像,而不是灰色图像。