Friday, November 25, 2011

Image Overlap in Android OpenCV

Its been recently that I have been playing around with OpenCV on Android.

Btw, My other programs in OpenCV will be posted here.

Basically, I've written a program that overlaps two images. One image exists in the "res" folder in android project and other image is the Image coming from the camera.

Image coming from the camera is available in array of bytes. We convert it into a Bitmap, then into a "Mat". The other image is read as a "Mat" directly using Utils.loadResource method.

Now since both images are in Mat format, I use Mat.getNativeObjAddr() method from android-opencv project to pass the image to JNI code. This is more useful as we can reconstruct cv::Mat in JNI code more easily than passing bytes (lookup samples from android-opencv project) and reconstructing them.

Also, one thing to take care in number of channels in the images/Mat being processed. We need to make sure that all the images being operated on should be of same number of channels. During this process, you might have to convert RGB to Gray or RGB to RGBA, etc.

In the sample program, I've written a code to send RGBA image, RGB image to JNI (OpenCV C++) code and then convert RGB image to RGBA using cvtColor function. Then do a "addweighted" function call on the 2 images and return it to Java code.

I got the following image on my App where I performed merging  of two images:





We can use more intense image processing stuff in C++ which is more faster than it is on Android (java).

If you are looking for the program to merge images in OpenCV, take a look at this program.

No comments:

Post a Comment