Prebuilt OpenCV 2.3.1 - Converting to Grayscale

1,734 views
Skip to first unread message

greven

unread,
Oct 13, 2011, 11:12:09 PM10/13/11
to android-opencv
Hi guys, wanna thank anyone that helps me in advance.

I'm failing miserably at something I think it should be very easy to
achieve. I'm importing a Bitmap from a .jpg file I have in drawable
converting it to mat and then trying to change it to a grayscale
image, converting it back to bitmap to display.

This is the code I use:


package com.greven.test.contour;

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

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class ContourTestActivity extends Activity {

ImageView iv;
Mat mMat, mAux;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView) findViewById(R.id.my_image);

mMat = new Mat();
mAux = new Mat();

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

mMat = Utils.bitmapToMat(bmp);

Imgproc.cvtColor(mMat, mAux, Imgproc.COLOR_RGB2GRAY, 0);

Utils.matToBitmap(mMat, bmp);

iv.setImageBitmap(bmp);
}
}



I've tried with different space colors and I always get this error as
long as I try to convert it to Gray.

The error I get is this:


10-14 04:10:38.626: ERROR/AndroidRuntime(3343): Caused by: CvException
[org.opencv.core.CvException: /home/andreyk/OpenCV2/trunk/
opencv_2.3.1.b2/modules/imgproc/src/color.cpp:2834: error: (-215) scn
== 3 || scn == 4 in function void cv::cvtColor(const cv::_InputArray&,
const cv::_OutputArray&, int, int).

Anyone knows what am I doing wrong?

Thank you.

abdelbar nasri

unread,
May 20, 2012, 11:18:33 AM5/20/12
to android...@googlegroups.com
hi ;
i have the same problem ,, so please if you resolve it ,, tell me the solution :)
thanks in advance :)

Rui Marques

unread,
May 20, 2012, 11:33:01 AM5/20/12
to android...@googlegroups.com
These errors are quite common when you are starting with opencv, so you might have this error while doing something else.

The post and code from greven is quite old so i will not debug it. 

If you post your own code that makes this error and the error message from logcat, we might be able to help.

abdelbar nasri

unread,
May 20, 2012, 5:38:50 PM5/20/12
to android...@googlegroups.com
my code is :



public class TakePictureActivity extends Activity {

private static final int TAKE_PHOTO_CODE = 1;  
 ImageView imagetake;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
takePhoto();
this.setContentView(R.layout.camsurf);
imagetake = (ImageView) findViewById(R.id.view);
}

private void takePhoto(){  
 final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) );   
 startActivityForResult(intent, TAKE_PHOTO_CODE);  
}  
 
private File getTempFile(Context context){  
 //it will return /sdcard/image.tmp  
 final File path = new File( Environment.getExternalStorageDirectory(), context.getPackageName() );  
 
 if(!path.exists()){ 
    path.mkdir();    }  
 return new File(path, "image.jpg");  
}  


@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
   switch(requestCode){  
     case TAKE_PHOTO_CODE:  
       final File file = getTempFile(this);  
       try {  
         Bitmap captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(file) ); 
         // do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc)
        Mat barcodeProcessingMat = new Mat();
        barcodeProcessingMat=Utils.bitmapToMat(captureBmp);
         
        //Imgproc.cvtColor(barcodeProcessingMat, barcodeProcessingMat, Imgproc.COLOR_RGB2GRAY);
        Utils.matToBitmap(barcodeProcessingMat, captureBmp);
      
        imagetake.setImageBitmap(captureBmp);
        
        //imagetake.setImageBitmap(LocatingWithoutOrientation(captureBmp));
         
       } catch (FileNotFoundException e) {  
         e.printStackTrace();  
       } catch (IOException e) {  
         e.printStackTrace();  
       }  
     break;  
   }  
 
}  
.........................................................................................................

LogCat

05-20 22:22:04.319: W/dalvikvm(22705): threadid=1: thread exiting with uncaught exception (group=0x40018578)
05-20 22:22:04.319: E/AndroidRuntime(22705): FATAL EXCEPTION: main
05-20 22:22:04.319: E/AndroidRuntime(22705): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.emsir.BarCodeV12.app/com.emsir.BarCodeV12.cam.TakePictureActivity}: CvException [org.opencv.core.CvException: /home/andreyk/OpenCV2/trunk/opencv_2.3.1.b2/modules/imgproc/src/color.cpp:2789: error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(const cv::_InputArray&, const cv::_OutputArray&, int, int)
05-20 22:22:04.319: E/AndroidRuntime(22705): ]
05-20 22:22:04.319: E/AndroidRuntime(22705): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
05-20 22:22:04.319: E/AndroidRuntime(22705): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
05-20 22:22:04.319: E/AndroidRuntime(22705): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
05-20 22:22:04.319: E/AndroidRuntime(22705): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
05-20 22:22:04.319: E/AndroidRuntime(22705): at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 22:22:04.319: E/AndroidRuntime(22705): at android.os.Looper.loop(Looper.java:123)
05-20 22:22:04.319: E/AndroidRuntime(22705): at android.app.ActivityThread.main(ActivityThread.java:3687)
05-20 22:22:04.319: E/AndroidRuntime(22705): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 22:22:04.319: E/AndroidRuntime(22705): at java.lang.reflect.Method.invoke(Method.java:507)
05-20 22:22:04.319: E/AndroidRuntime(22705): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-20 22:22:04.319: E/AndroidRuntime(22705): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-20 22:22:04.319: E/AndroidRuntime(22705): at dalvik.system.NativeStart.main(Native Method)
05-20 22:22:04.319: E/AndroidRuntime(22705): Caused by: CvException [org.opencv.core.CvException: /home/andreyk/OpenCV2/trunk/opencv_2.3.1.b2/modules/imgproc/src/color.cpp:2789: error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(const cv::_InputArray&, const cv::_OutputArray&, int, int)
05-20 22:22:04.319: E/AndroidRuntime(22705): ]
05-20 22:22:04.319: E/AndroidRuntime(22705): at org.opencv.imgproc.Imgproc.cvtColor_1(Native Method)
05-20 22:22:04.319: E/AndroidRuntime(22705): at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:3743)
05-20 22:22:04.319: E/AndroidRuntime(22705): at com.emsir.BarCodeV12.cam.TakePictureActivity.onActivityResult(TakePictureActivity.java:91)
05-20 22:22:04.319: E/AndroidRuntime(22705): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
05-20 22:22:04.319: E/AndroidRuntime(22705): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
05-20 22:22:04.319: E/AndroidRuntime(22705): ... 11 more


thanks sir Rui Marques

Le vendredi 14 octobre 2011 05:12:09 UTC+2, greven a écrit :

peppschmier

unread,
May 21, 2012, 2:47:28 AM5/21/12
to android...@googlegroups.com
Hi there,

i think Android gives you the bitmap in RGB8888 - then you have to use RGBA or BGRA.
and in 2.3.1 matToBitmap() can only convert ARGB (if I remembered correctly) - so you have to
convert your grayscale to ARGB at first.

Michael

Andrey Pavlenko

unread,
May 21, 2012, 10:37:18 AM5/21/12
to android...@googlegroups.com
This looks like you passes an empty Mat to cvtColor() input.
BTW, you'd better use OpenCV for Android 2.4.0 that has better implementation of Mat <-> Bitmap conversion.

abdelbar nasri

unread,
May 21, 2012, 3:39:45 PM5/21/12
to android...@googlegroups.com
thanks  it work with 
Imgproc.cvtColor(barcodeProcessingMat, barcodeProcessingMat, Imgproc.COLOR_RGB2BGRA);
Reply all
Reply to author
Forward
0 new messages