Converting Bitmap to Mat and vice versa

2,214 views
Skip to first unread message

sanks

unread,
Oct 3, 2011, 3:17:10 AM10/3/11
to android-opencv
Hello,

I am new to OpenCV android. I downloaded the Android binary package
with Eclipse and got it installed in windows. I then loaded the
examples given (which worked when I ran it on my device perfectly) ,
added OpenCV 2.3.1 Library to my new android project. I am already
calling the camera intent of Android using which I take a photo.
I will now have to process the image using OpenCV.

So after I take the photo (as a JPEG) , a new activity fires up and I
use this

Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
to get my bitmap. I would now like to convert it into a Mat for me to
be able to use it, to do further processing.

I have two approaches in mind.
a) save the bitmap and then read it again ( I would preferably like to
save and load from internal phone memory)
b) since I already have the bitmap, might as well convert it to a
matrix and use it directly.

for approach b)
I read another similar thread in this forum and used a function posted
by Mr.Aaron Brown
which is Bitmap JPEGtoRGB888(Bitmap img). It seemed to work when
converting from bitmap -> mat,
but I was not able to convert it back using mat -> bitmap. It either
breaks or returns nothing.

for approach a)
This seemed to work partially,

//get cameraimage
.
.
.
//save it to internal memory
.
.
//load it as a drawable
.
.
.
ImageView image = (ImageView) findViewById(R.id.cameraphotoview);
if(bitmap != null)
{

Mat img;
try {
img = Utils.loadResource(getBaseContext(), R.drawable.im1);
Imgproc.blur(img, img, new Size(30, 30));
Bitmap bmp =
Bitmap.createBitmap(img.cols(),img.rows(),Bitmap.Config.ARGB_8888);
org.opencv.android.Utils.matToBitmap(img, bmp);
image.setImageBitmap(bmp);

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

by partially I mean, it garbled the output bitmap if my drawable was a
jpeg. if it was a png, I could convert mat to bitmap and see the image
blur, but the color property got messed up, meaning there was a blue
tint over the image. I am sure I am missing the right conversion
parameter here. Just dont know where!

As an other alternate approach, is it a good idea to use JNI and pass
the bit map as a byte array or something and get back the result?
Sorry for such a long post. Been hacking at it all day and it pains me
to not be able to do such a trivial bitmap conversion.

Appreciate for the help in advance,
Thanks,
sanks

sanks

unread,
Oct 4, 2011, 9:00:25 PM10/4/11
to android-opencv
Never mind. I used JNI and ported all my openCV code onto a native
function...it is able to load and save images directly to the phone
memory now. I will just have to pass the filename - string an argument
to the native function after taking the photo using the android camera
intent to manipulate it I suppose.

Thanks,
sanks

Aaron Brown

unread,
Oct 6, 2011, 11:02:34 PM10/6/11
to android-opencv
Nice work. Sorry my methods didn't work for you.

They work so far on my test device, but other people have reported
issues with the conversions. Once I'm no longer fighting towards a
demo, I'll try to see about testing them across platform and trying to
get a canonical solution to the problem.

Best of luck!

-AMB

l Shankar

unread,
Oct 9, 2011, 1:17:17 AM10/9/11
to android...@googlegroups.com
yup. Thanks for your solution. I think it actually worked. When I stepped through the code it came out of your function successfully and did not break cvsmooth.The problem I faced was converting it back to an android bitmap again. I may have specified the file format incorrectly for all we know. But I have a lot of image processing function calls where I find the contour and stuff. So I thought copying the code across would decrease my workload of having to rewrite all the existing C code I put together. Also C runs way more faster. So I ended up figuring out the jni way.
--
Shankar Lakshmanan,
GIS Graduate Research Assistant
Department of Computer Science and Engineering
University Of Nebraska Lincoln,
Lincoln, NE, USA.
E-mail - slak...@cse.unl.edu

Reply all
Reply to author
Forward
0 new messages