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