Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV for Android: Simple example to convert Image to Greyscale

As a starter I just want to convert an Bitmap to greyscale via OpenCV. I have everything running, but it crashes hard as soon as i want to convert the image to greyscale. Can anyone help out? I hope the snippets are enough, if not i can attach the rest.

part of the java file:

                // convert to opencv structure
                Mat image = new Mat();
                Mat grayimage = new Mat();
                Utils.bitmapToMat(b2, image);

                // call opencv for processing
                grayimage = convertToGray (image);

                // convert back
                Utils.matToBitmap(grayimage, b2);

JNI cpp file:

JNIEXPORT jlong JNICALL Java_com_my_sample_MainMenuActivity_convertToGray (JNIEnv*, jobject, jlong addrRGBA)
{
    LOGI("Converting to Gray.");
    Mat *mRGBA  = (Mat*)addrRGBA;

    Mat *_retval_;
    cvtColor(*mRGBA, *_retval_, CV_RGB2GRAY);

    LOGI("Successfully finished Converting to Gray.");
    return (jlong) _retval_;
}

It never gets to it logging successful having converted the image. Seems as if the Bitmap was not properly converted to a Mat. The Bitmap itself does exist, I can show it on a ImageView. Anyone a clue what I'm (obviously) doing wrong?

05-14 21:26:27.082: I/native(22394): Converting to Gray.
05-14 21:26:27.082: A/libc(22394): Fatal signal 11 (SIGSEGV) at 0xcd10001d (code=1), thread 22394 (ialabs.mysample)

Sorry if this question is answered elsewhere, i haven't found an example with Mat, just some with IplImage, it seems.

like image 898
complexM Avatar asked May 14 '13 19:05

complexM


2 Answers

You have two empty Mat objects. You can't convert something that's empty into gray.

Try this:

Mat tmp = new Mat (b.getWidth(), b.getHeight(), CvType.CV_8UC1);
Utils.bitmapToMat(b, tmp);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);

Where b is your image as a Bitmap.

Utils.matToBitmap(tmp, b);

Add this to convert the Mat object back to a Bitmap.

like image 170
Karthik Balakrishnan Avatar answered Sep 28 '22 06:09

Karthik Balakrishnan


The easiest way to do it :

public static Bitmap edgesim(Bitmap first) {

    Bitmap image1;

    ///////////////transform back to Mat to be able to get Canny images//////////////////
    Mat img1=new Mat();
    Utils.bitmapToMat(first,img1);

    //mat gray img1 holder
    Mat imageGray1 = new Mat();

    //mat canny image
    Mat imageCny1 = new Mat();

    //mat canny image
    Mat imageCny2 = new Mat();

    /////////////////////////////////////////////////////////////////

    //Convert img1 into gray image
    Imgproc.cvtColor(img1, imageGray1, Imgproc.COLOR_BGR2GRAY);

    //Canny Edge Detection
    Imgproc.Canny(imageGray1, imageCny1, 10, 100, 3, true);

    ///////////////////////////////////////////////////////////////////

    //////////////////Transform Canny to Bitmap/////////////////////////////////////////
    image1= Bitmap.createBitmap(imageCny1.cols(), imageCny1.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(imageCny1, image1);

    return image1;
}
like image 38
Gaby Bou Tayeh Avatar answered Sep 28 '22 06:09

Gaby Bou Tayeh