Ask Your Question
0

Unable to convert color image to grayscale in android opencv

asked 2015-04-21 08:44:45 -0600

Mudda gravatar image

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.

edit retag flag offensive close merge delete

Comments

Please upload the image under test

essamzaky gravatar imageessamzaky ( 2015-04-21 11:19:18 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-07-22 15:28:17 -0600

gi gravatar image

If gray mat is still coloured image, then maybe the problem is on Imgproc.cvtColor. Have you tried using new Mat without parameter?

Mat gray = new Mat();

or, maybe turn it to grayscale and put it back to imgSource should work fine too

Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY);

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-04-21 08:44:45 -0600

Seen: 2,412 times

Last updated: Jul 22 '16