Using Open CV with Java

The Open CV library originally released by Intel has indeed come a long way and has now become a go to library for all computer vision enthusiasts and researchers . The cross platform nature has also compounded the usability of the library  . The library initially written in C has now been moved to C++. The popularity of the library ensured many binding for other languages like python , Java , C# etc. Today lets discuss about Java .

Recently , I started some stuff on open CV with android and I was interested in face recognition . I searched around and found that Open CV has java bindings for its own native code , apart from that  I discovered a guy called Samuel Audet has also made Java bindings for Open CV known as Java CV. I was confused at first about choosing which Java binding to use , after a while it became apparent that I will have to use Java CV.

Why? The Face recognition is done   primarily by the following  EigenFaces, FisherFaces and LBHP recognizer classes and none of them have bindings in Java included with Open CV. At this point I could have written my own JNI for the classes or use Java CV which has already taken care of that . I decided to go with Java CV.

Now , I had already started working with Open CV java bindings and I thought the Java CV would be interchangeable   , but I was so wrong. I think an explanation about the basic data structures in Java Cv and Open CV’s java bindings would be helpful.

As mentioned earleir Open CV  has moved mostly to C++ and the underlying data structures also reflect the same . Mat or Matrix is the basic data structure in Open CV. The name for the structure has changed from cvMat to just Mat (from C to C++) . Now there is a corresponding Mat classes in Java CV. One is the cvMat and the other is the OpenCore_Mat . The thing is all these Mats are incompatible with each other .

If you read the Java CV’s code for face recognition it requires data in OpenCore_Mat , and I was using openCV’s camera listener to capture images which uses Open CV’s Mat. After a bit of searching I found the code to convert Open CV’s mat to Java CV’s Mat.

 


public static IplImage MatToIplImage(org.opencv.core.Mat m)
{
Bitmap bmp=Bitmap.createBitmap(m.width(), m.height(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(m, bmp);
return BitmapToIplImage(bmp,WIDTH, HEIGHT);

}

public static IplImage BitmapToIplImage(Bitmap bmp, int width, int height) {

if ((width != -1) || (height != -1)) {
Bitmap bmp2 = Bitmap.createScaledBitmap(bmp, width, height, false);
bmp = bmp2;
}

IplImage image = IplImage.create(bmp.getWidth(), bmp.getHeight(),
IPL_DEPTH_8U, 4);

bmp.copyPixelsToBuffer(image.getByteBuffer());

IplImage grayImg = IplImage.create(image.width(), image.height(),
IPL_DEPTH_8U, 1);

cvCvtColor(image, grayImg, CV_BGR2GRAY);

return grayImg;
}

//Convert Mat to IplImage and then Convert to Java CV Mat

opencv_core.Mat testImage = new opencv_core.Mat(IplImage)

 

This caused some problem for me at the start but things have cleared quite a lot now.
The ideal way if one is working is to use either open CV’s java bindings or use Java CV’s binding and not mix them up , like i did.

Siyanatullah Khan

Siyanatullah Khan

Mobile Software consultant with a penchant for blogging.

More Posts

Follow Me:
TwitterLinkedInGoogle Plus

Add a Comment

HTML Snippets Powered By : XYZScripts.com