android-opencv converting mat to grayscale with using matToBitmap/bitmapToMat

3,949 views
Skip to first unread message

Tuncay

unread,
Dec 10, 2011, 12:08:25 AM12/10/11
to android-opencv
I am using newer opencv library of willowgarage in eclipse. And I want
to convert a mat variable into grayscale, I've tried everything I
found on the net but they didnt work for me.

Here is my code

package com.deneme.deneme;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class main extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageView img=(ImageView) findViewById(R.id.pic);

Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.p26);

Mat imgToProcess=Utils.bitmapToMat(bmp);

//******
//right here I need to convert this imgToProcess to grayscale for
future opencv processes
//******

Bitmap bmpOut = Bitmap.createBitmap(imgToProcess.cols(),
imgToProcess.rows(), Bitmap.Config.ARGB_8888);

Utils.matToBitmap(imgToProcess, bmpOut);
img.setImageBitmap(bmpOut);
}

Roman

unread,
Dec 10, 2011, 3:22:38 AM12/10/11
to android-opencv
Hi,
try Imgproc.cvtColor(imgToProcess, imgToProcessGray,
Imgproc.COLOR_BGR2GRAY);

Roman

Tuncay

unread,
Dec 10, 2011, 4:26:21 AM12/10/11
to android-opencv
Hi Roman,
Unfortunately its not working. I am testing this application on
samsung galaxy s2 android phone and it exits from application when I
edited the code like this. Anyway thanks for your answer.

package com.deneme.deneme;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class main extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageView img=(ImageView) findViewById(R.id.resimBir);


Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.p26);
Mat imgToProcess=Utils.bitmapToMat(bmp);

Mat imgToProcessGray = new Mat();
Imgproc.cvtColor(imgToProcess,
imgToProcessGray,Imgproc.COLOR_BGR2GRAY);

Bitmap bmpOut = Bitmap.createBitmap(imgToProcessGray.cols(),
imgToProcessGray.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(imgToProcessGray, bmpOut);
img.setImageBitmap(bmpOut);

Roman

unread,
Dec 10, 2011, 6:32:50 AM12/10/11
to android-opencv
Hmm, do you allocate new mat before processing... imgToProcess=new
Mat() ?

try direct drawable->Mat method, Mat imgToProcess =
Utils.loadResource(context, R.drawable.p26);

Roman

Hardik

unread,
Dec 10, 2011, 12:30:26 PM12/10/11
to android...@googlegroups.com
Hi,

Try this.......

Mat original = new Mat();

        Mat converted = new Mat(200, 200, CvType.CV_8UC1, new Scalar(0));//put something height n width

        try {

            original = Utils.loadResource(this, R.drawable.p26, Highgui.CV_LOAD_IMAGE_COLOR);

        } catch (IOException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Imgproc.cvtColor(original , converted, Imgproc.COLOR_RGB2GRAY, 4);  // convert Image to grayscale




--
Regards,

Pansuria Hardik

Android Developer





Tuncay

unread,
Dec 12, 2011, 3:47:48 AM12/12/11
to android-opencv
Hi again,
Roman and Hardik thanks for your replies, but I am still getting a
failed result at the end.
The closest code to success is this so far... And the result image is
here for you to see( http://www.rudral.com/result.jpg ). I think the
problem is about channel numbers. Any idea? Again thanks for
assistance.


package com.deneme.deneme;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


public class main extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageView img=(ImageView) findViewById(R.id.picOne);


Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.n);


Mat imgToProcess=Utils.bitmapToMat(bmp);
Mat imgToProcessGray = new Mat(imgToProcess.cols(),
imgToProcess.rows(), CvType.CV_8UC1, new Scalar(0));
Imgproc.cvtColor(imgToProcess,
imgToProcessGray,Imgproc.COLOR_RGB2GRAY,4);

hardik pansuria

unread,
Dec 12, 2011, 5:00:17 AM12/12/11
to android-opencv
hi,

What abt if you use ..

Imgproc.cvtColor(imgToProcess,
imgToProcessGray,Imgproc.COLOR_RGB2GRAY,1);


means destination channel no is 1.....if you think problem is related
to channel

On Dec 12, 1:47 pm, Tuncay <tuncaycakma...@gmail.com> wrote:
> Hi again,
> Roman and Hardik thanks for your replies, but I am still getting a
> failed result at the end.
> The closest code to success is this so far... And the result image is

> here for you to see(http://www.rudral.com/result.jpg). I think the

Andrey Pavlenko

unread,
Dec 14, 2011, 8:36:10 AM12/14/11
to android...@googlegroups.com
1) OpenCV-2.3.1 for Android supports conversion of Bitmaps ARGB_8888 to Mat and correspondingly Mats 8UC4 to Bitmaps ARGB_8888 only.
2) regardless of the 4th argument cvtColor(src, dst, COLOR_RGB2GRAY) returns a single channel gray image; to make it 4-channel gray one call cvtColor (COLOR_GRAY2RGBA).
3) BTW, if you need only grayscale image, you can load it with Utils.loadResource(context, resID, Highgui.CV_LOAD_IMAGE_GRAYSCALE); it will be of type 8UC1.

ster_va7

unread,
Dec 14, 2011, 6:11:44 PM12/14/11
to android-opencv
I am having problems too with converting to grey...

cvtColor(src, dst, COLOR_RGB2GRAY) is not working, neither is
cvtColor(src, dst, COLOR_RGB2GRAY,1), neither using COLOR_RGBA2GRAY...

The only thing I managed to work was:

capture.retrieve(mat1, Highgui.CV_CAP_ANDROID_GREY_FRAME)
Imgproc.cvtColor(mat1, mat2,Imgproc.COLOR_GRAY2RGBA, 4)

but then I get a 4 channel gray image... so how can I get a single
channel gray image?? actually, how can I get a single channel at all
(e.g green channel)? I tried extractChannel and didn't work!

Hardik

unread,
Dec 15, 2011, 12:06:56 AM12/15/11
to android...@googlegroups.com
Can you show some code ??  In my case , i got frame from camera and convert to RGB then again convert to Grayscale image  .......

Andrey Pavlenko

unread,
Dec 15, 2011, 4:22:47 AM12/15/11
to android...@googlegroups.com
did you look into the provided samples?
they contain the working code you're talking about...

Tuncay

unread,
Dec 15, 2011, 9:58:55 AM12/15/11
to android-opencv
Hi again,
Now my code is working,
here is the code for loading mat image in grayscale

Mat img= Highgui.imread(Utils.exportResource(context,
R.drawable.imageToLoad), Highgui.CV_LOAD_IMAGE_GRAYSCALE);

After your processes are finished with this image, you must save this
mat variable and then load it again to display with imageview.

Thanks for your replies.

ster_va7

unread,
Dec 15, 2011, 5:22:00 PM12/15/11
to android-opencv
In my case I am using:

capture.retrieve(mat, Highgui.CV_CAP_ANDROID_GREY_FRAME);
Imgproc.cvtColor(mat, dst, Imgproc.COLOR_GRAY2RGBA, 4);

This is working but I am getting a 4 channel grayscale which I think
it's not necessary, a single channel grayscale mat might be easier and
faster for image processing, isn't it?

Doing this is not working for me:

capture.retrieve(mat, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);

cvtColor(mat, dst, COLOR_RGB2GRAY);
OR
cvtColor(nat, dst, COLOR_RGB2GRAY,1);// neither using
COLOR_RGBA2GRAY...

so, how can one get a single channel grayscale mat? and what about
extracting one color channel of a mat?
In order to get a single channel I tried the following but didn't
work:

capture.retrieve(mat, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
Core.extractChannel(mat,dst, 1);

Is there any other way to achieve this?

Andrey Pavlenko

unread,
Dec 16, 2011, 12:12:11 AM12/16/11
to android...@googlegroups.com
The "capture.retrieve(mat, Highgui.CV_CAP_ANDROID_GREY_FRAME)" call provides you a single channel grayscale image.
To display it you need to call the "Imgproc.cvtColor(mat, dst, Imgproc.COLOR_GRAY2RGBA, 4)" to make it 4-channel, but for processing you should use the single channel one.
Reply all
Reply to author
Forward
0 new messages