Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Unable to convert color image to grayscale in android opencv

This is the part of code which is not working good. I know this question has been asked earlier, but i have done everything i could do but still couldn't figure out the reason. This is the portion of code

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;
}

In my Logcat i see the message "contour size is 0".

When i load the gray mat, i see a coloured image which explains why their were no contours detected.

Please suggest something.