submat converting to & grayscale Bitmap problems

819 views
Skip to first unread message

K-man

unread,
Dec 25, 2011, 11:01:48 PM12/25/11
to android-opencv
Hi all,
I have 2 questions, both with seeming problems converting Mat objects
to bitmaps.

I have modified the face detection sample code provided with the
android-opencv package. For some other reasons, I don't want to
process images directly from the camera, but rather from static
images. I made the appropriate changes. To read in the image and
convert to a Mat object, I am doing the following:

/* Code section reads in an image stored in my res/drawable folder */
public Bitmap processImage() {
Mat face = null;
Bitmap bmp1 = BitmapFactory.decodeResource(getResources(),
R.drawable.mcqueen2);
Bitmap bmp2 = JPEGtoRGB888(bmp1);
mRgba = Utils.bitmapToMat(bmp2);

/* Creating a grayscale version of the RGBA image for face
detection */
mGray = mRgba.clone();
Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY);
Log.i(TAG, "mRgba channels = " + mRgba.channels() + " mGray
channels = " + mGray.channels());

/* Code to detect faces */
if (mCascade != null) {
int height = mGray.rows();
int faceSize = Math.round(height * FdActivity.minFaceSize);
List<Rect> faces = new LinkedList<Rect>();
mCascade.detectMultiScale(mGray, faces, 1.1, 2, 2 // TODO:
objdetect.CV_HAAR_SCALE_IMAGE
, new Size(faceSize, faceSize));

for (Rect r : faces) {
Core.rectangle(mRgba, r.tl(), r.br(), new Scalar(0,
255, 0, 255), 3);
}
face = mRgba.submat(faces.get(0));
Log.i(TAG, "face channels = " + face.channels());
}

// this code section works fine, the bmp displays properly
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(),
Bitmap.Config.ARGB_8888);
if (Utils.matToBitmap(mRgba, bmp)) {
return bmp;
}

// the face does not display properly
Bitmap bmp = Bitmap.createBitmap(face.cols(), face.rows(),
Bitmap.Config.ARGB_8888);
if (Utils.matToBitmap(face, bmp)) {
return bmp;
}

// grayscale does not display properly
Bitmap bmp = Bitmap.createBitmap(mGray.cols(), mGray.rows(),
Bitmap.Config.ARGB_8888);
if (Utils.matToBitmap(mGray, bmp)) {
return bmp;
}
}

/* Takes a JPEG captured by the device camera and converts it to
RGB888 format */
/* Credit for this function goes to Aaron Brown */
private Bitmap JPEGtoRGB888(Bitmap img) {
int numPixels = img.getWidth()* img.getHeight();
int[] pixels = new int[numPixels];
//Get JPEG pixels. Each int is the color values for one
pixel.
img.getPixels(pixels, 0, img.getWidth(), 0, 0, img.getWidth(),
img.getHeight());
//Create a Bitmap of the appropriate format.
Bitmap result = Bitmap.createBitmap(img.getWidth(),
img.getHeight(), Bitmap.Config.ARGB_8888);
//Set RGB pixels.
result.setPixels(pixels, 0, result.getWidth(), 0, 0,
result.getWidth(), result.getHeight());
return result;
}

**********************************
I'm unclear as to why the face or the grayscale doesn't display
properly, while the mRgba image does display properly?

To draw the Bitmap image, I use the following code section:
Bitmap bitmap = processImage();
canvas.drawBitmap(bitmap, (canvas.getWidth() - bitmap.getWidth()) / 2,
(canvas.getHeight() - bitmap.getHeight()) / 2, null);


Thanks in advance,
Kiran

hardik pansuria

unread,
Dec 26, 2011, 12:37:30 AM12/26/11
to android-opencv
Hi Kiran,

For grayscale image.. try

mGray = new Mat(new Size( 576,648), CvType.CV_8UC1,new
Scalar(0)); // put mRgba's col and row size

Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_BGR2GRAY,1);

I amnot sure it works or not... in my case it was working when i
take frames from camera directly.....


Thanks


instead of....
mGray = mRgba.clone();

Yu-Hsuan Lin

unread,
Jan 3, 2012, 8:02:55 PM1/3/12
to android...@googlegroups.com
 // the face does not display properly 
     Bitmap bmp = Bitmap.createBitmap(face.cols(), face.rows(), 
Bitmap.Config.ARGB_8888); 
     if (Utils.matToBitmap(face,  bmp)) { 
          return bmp; 
     } 

The submat's nativeObj is its parent nativeObj, so when you call matToBitmap, it still convert the parent's Mat to Bitmap, so the Bitmap's size didn't fit correctly.
You should use
Utils.matToBitmap(face.clone(),  bmp);

So it will give the new nativeObj into  the function, it should work then.

Y.H
Message has been deleted

Andrey Pavlenko

unread,
Jan 4, 2012, 5:40:25 AM1/4/12
to android...@googlegroups.com
Mat -> Bitmap conversion is buggy in the 2.3.1 Android release of OpenCV, it works for 4-channel Mat-s (i.e. RGBA) only and processes sub-Mat-s incorrectly.
Dev team has plans to improve this code.
Currently to convert a sub-Mat to a Bitmap you need to call a redundant clone() of it.
To convert a gray-scale (1-channel) Mat you need to convert it to 4-channel one, e.g:
  Mat grayC4 = new Mat();
  Imgproc.cvtColor(gray,  grayC4, Imgproc.COLOR_GRAY2RGBA, 4);
  Bitmap bmp = Bitmap.createBitmap( grayC4.cols(),  grayC4.rows(),  Bitmap.Config.ARGB_8888 );
  if ( Utils.matToBitmap( grayC4,  bmp ) )
  { ... }

danphuc2010

unread,
Jan 9, 2012, 11:46:39 AM1/9/12
to android-opencv

i have a same problem.
public class Process {
public static Bitmap process1 (Bitmap bsrc){
Mat src = null;
Mat dst = null;
int width = bsrc.getWidth();
int height = bsrc.getHeight();
// create output bitmap
Bitmap bdst = Bitmap.createBitmap(width, height, bsrc.getConfig());
try {
src = Utils.bitmapToMat(bsrc);
dst = src.clone();
Imgproc.blur(src, dst, new Size(3, 3));
if(Utils.matToBitmap(dst.clone(), bdst))
return bdst;
} catch (Exception e){
e.printStackTrace();
}
return bdst;
}
}

when i run, i saw window with title but don't have image ( it is
black)...
who know mistake of me? help me?
Thanks in advance,

Andrey Pavlenko

unread,
Jan 11, 2012, 6:45:30 AM1/11/12
to android...@googlegroups.com
The ' Utils.bitmapToMat(bsrc); ' call returns correct Mat object for Bitmap of  Config.ARGB_8888 only .
For other formats Bitmaps it returns an empty Mat.

BTW, you don't need to call ' dst = src.clone(); ', just call ' dst = new Mat(); ' instead.
You also don't need to call ' (dst.clone() ' for ' Utils.matToBitmap() ' call if dst isn't sub-Mat of some other Mat;

PHÚC KHƯU THIỆN

unread,
Jan 11, 2012, 6:54:05 AM1/11/12
to android...@googlegroups.com
thanks very much.
but how to convert a others bitmap to a  Bitmap of  Config.ARGB_8888?

2012/1/11 Andrey Pavlenko <andrey....@itseez.com>

Andrey Pavlenko

unread,
Jan 12, 2012, 7:00:22 AM1/12/12
to android...@googlegroups.com
I guess, somehow like this:
Bitmap bmp32 = bmp.copy(Bitmap.Config.ARGB_8888, true);

PHÚC KHƯU THIỆN

unread,
Jan 13, 2012, 5:01:42 AM1/13/12
to android...@googlegroups.com
oh, thanks very much.
first picture
image.png

but why my picture became many small one
image.png
my code:
package com.example.textimage;
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.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class TextImageActivity 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.imageView2); 
        
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.baxa); 
        Bitmap bmp32 = bmp.copy(Bitmap.Config.ARGB_8888, true);
        Mat imgToProcess=Utils.bitmapToMat(bmp32); 
        Mat imgToProcessGray = new Mat(imgToProcess.cols(), imgToProcess.rows(), CvType.CV_8UC1, new Scalar(0)); 
        Imgproc.cvtColor(imgToProcess, imgToProcessGray,Imgproc.COLOR_RGB2GRAY,4); 
        Bitmap bmpOut = Bitmap.createBitmap(bmp32.getWidth(),  bmp32.getHeight(), Bitmap.Config.ARGB_8888); 
        Utils.matToBitmap(imgToProcessGray, bmpOut); 
        img.setImageBitmap(bmpOut); 
    }
}
2012/1/12 Andrey Pavlenko <andrey....@itseez.com>
image.png
image.png

Andrey Pavlenko

unread,
Jan 16, 2012, 7:44:11 AM1/16/12
to android...@googlegroups.com
The correct code looks this way:

        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
        Bitmap bmp32 = bmp.copy(Bitmap.Config.ARGB_8888, true);
        Mat imgToProcess=Utils.bitmapToMat(bmp32); 
        Mat imgToProcessGray = new Mat(); 
        Imgproc.cvtColor(imgToProcess, imgToProcessGray,Imgproc.COLOR_RGBA2GRAY);
        Mat imgToProcessGrayC4 = new Mat();
        Imgproc.cvtColor(imgToProcessGray, imgToProcessGrayC4,Imgproc.COLOR_GRAY2RGBA, 4);
        Bitmap bmpOut = Bitmap.createBitmap(bmp32.getWidth(),  bmp32.getHeight(), Bitmap.Config.ARGB_8888); 
        Utils.matToBitmap(imgToProcessGrayC4, bmpOut); 
        img.setImageBitmap(bmpOut);
Reply all
Reply to author
Forward
0 new messages