SWAG
SWAG

Reputation: 37

OpenCV for Android: Error when i convert color image to grayscale

I want to convert color image to grayscale. For that I am using cvtColor() method of openCV but its giving following error:

OpenCV Error: Assertion failed (scn == 1 && (dcn == 3 || dcn == 4)) in void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/color.cpp, line 3789

This is my code :

img= Highgui.imread(pathToImage);

    /*Mat convert= new Mat(img.size(), CvType.CV_8UC3);
converted=new Mat(img.size(), CvType.CV_8UC3, new Scalar(255,
        255, 255));*/
    Imgproc.cvtColor(img,img,Imgproc.COLOR_GRAY2RGBA,0); 



    Imgproc.threshold(finalresult, converted, 254, 255, 1 /* THRESH_BINARY_INV */);     

    bitmap1=Bitmap.createBitmap(finalresult.cols(), finalresult.rows(), Bitmap.Config.ARGB_8888);

    System.out.println(""+finalresult);

    Utils.matToBitmap(finalresult, bitmap1);

    canvas = new Canvas(bitmap1);

    iv.setImageBitmap(bitmap1);
    }catch(Exception e)
    {
        System.out.println(e.toString());
    }

Upvotes: 0

Views: 2528

Answers (1)

TheBlueNotebook
TheBlueNotebook

Reputation: 1254

You are using the wrong flag. Change the third parameter in Imgproc.cvtColor from Imgproc.COLOR_GRAY2RGBA to Imgproc.COLOR_BGRA2GRAY.

Upvotes: 2

Related Questions