OpenCV library.

keylab83

Member
Licensed User
Longtime User
Hi all. Please can someone help me. I'v try to wrap a openCV library for b4a but without successes.My knowledge with java is NONE. I got all the materials need to wrap openCV library but not the knowledge how to do it. I believe most of us need that library so can someone do anything about it pls. there is a java code for that

OpenCV in Android
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
OpenCV in Android
Using OpenCV in Android. This tutorial is tested under Ubuntu 10.04 + Android SDKr07 + Android NDKr4b.
Preparing the development environment
Download and install Android SDK. Details can be found here
Download eclipse and install the ADT plugin. Details can be found here
Download Androdi NDK. This tool is used to cross compile OpenCV source code to Android. Currently (NDK r4) only C is fully supported, so I can only use OpenCV 1.1 under Android. The laterst version of OpenCV uses lots of STL functions. :(
Create the test project
Create a new Android project in eclipse. For example, called testOpenCV. Name the package name as: edu.stanford.android.
In the root directory of the project, create a new folder called jni and extract all files in android_opencv.tar.gz to this folder.
Run “$NDK/ndk-build” from your project directory. It will generate libopencv.so in the libs/armeabi folder.
Write Java code to use OpenCV functions. I have three java files which can be downloaded here. They are a little bit long so I do not want to paste the source code here. The general idea is that I use the intent to start the camera or gallery activities to get the image and send this image to OpenCV. After OpenCV finishes extracting SURF features, it send the processed image back to JVM. The interface between JVM and OpenCV is pretty simple: setSourceImage and getSourceImage.
OpenCV.java
package edu.stanford.zixuanpc;

public class OpenCV {
static{
System.loadLibrary("opencv");
}
public native boolean setSourceImage(int[] pixels, int width, int height);
public native byte[] getSourceImage();
public native void extractSURFFeature();
}
And here is the code how we use these two functions:

Code snippet in testOpenCVActivity.java
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentImagePath);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
opencv.setSourceImage(pixels, width, height);
opencv.extractSURFFeature();
byte[] imageData = opencv.getSourceImage();
bitmap = BitmapFactory.decodeByteArray(imageData, 0,
imageData.length);
mImageView.setImageBitmap(bitmap);
Run the program
Press the menu button to select your camera to capture an image. The down-sampled image is saved in your gallery. Press the menu button again to select it from your gallery and features are automatically extracted.
Notice: if you select a large image in the gallery, the program may crash due to limited memory.
On my DROID phone, it usually takes 4 seconds to extract features from one image.


Thank you guys and hope someone gonna surprise us with OpenCV library soon .
 

Medel

Member
Licensed User
Longtime User
Hi, i'm Luis, i wonder if OpenCV can't be use in B4A, is there anyway to convert an image from the camera into an array of numbers so we can make the image process by ourselves?

I've tried this library: RSImageProcessing Library however i can't transform the image into 3-array integer numbers and also could not do it directly from the mobile camera.

If you could give us an example, that would help us a lot with the image processing so we can do our own libraries of functions to detect color, object, patterns or faces, but the main deal at least for me is to convert the image into 3-array digits numbers from the mobile camera.

Plzz help =)
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Hi, i'm Luis, i wonder if OpenCV can't be use in B4A, is there anyway to convert an image from the camera into an array of numbers so we can make the image process by ourselves?

I've tried this library: RSImageProcessing Library however i can't transform the image into 3-array integer numbers and also could not do it directly from the mobile camera.

If you could give us an example, that would help us a lot with the image processing so we can do our own libraries of functions to detect color, object, patterns or faces, but the main deal at least for me is to convert the image into 3-array digits numbers from the mobile camera.

Plzz help =)
My Accelerated Surface library has a GetPixels function. And the ABExtDrawing lib has one too probably.
 
Upvote 0

csc000

New Member
Licensed User
Longtime User
Agreed, Waiting Opencv library (or as minimums lib project with core functionality) where each of opencv funs can add their necesery functions - in this case we got a good wrapped lib with a lot of computer vision specialists - Erel?
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
Erel is correct. OpenCV is very difficult to use and you must learn a great deal and study their examples. But, at the same time it is very powerful.

I have successfully used OpenCV with B4A. The way I did it is to essentially develop the routines I needed in Java using Eclipse, include functions that could be called from B4A which in turn called the OpenCV program I had developed, then returned the results to B4A.

It is not a general library interface in any stretch of the imagination.

To learn more about OpenCV, I installed Microsoft Visual Studio Express 2012 (free version) and executed some of their C++ tutorials.

Barry.
 
Upvote 0

thecubangenie

New Member
Hi Erel and Barry, thanks for your answer

Have you viited this site (http://boofcv.org/index.php?title=Manual)?

They created a wrapper to use OpenCV in Java.
There is a wrapper of opencv for VS called Emgu that I have used in vb.net.
I wonder, can you or anyone with experience in java can take this wrapper and create a Library for B4A?
It is already in java right? which is the a huge advantege.

I can't do it my self because I know nothing about java but it may worth to try

thanks again for your quick response
 
Upvote 0

thecubangenie

New Member
I forgot to say that this BoofCV wrapper is Opensource and have an Apache license as they state in their Main page:

"BoofCV is an open source Java library for real-time computer vision and robotics applications. Written from scratch for ease of use and high performance, it often outperforms even native libraries. Functionality includes optimized low-level image processing routines, feature tracking, and geometric computer vision. BoofCV has been released under an Apache license for both academic and commercial use."
 
Upvote 0

floatingpoint

Member
Licensed User
Longtime User
Another vote for opencv for B4A.

I found opencv quite straight forward and powerful.
With very little python or opencv experience I've got an opencv app running on a Raspberry Pi V2.
25 FPS at 300 x 400 resolution.
Pretty sure I can optimize this to run faster but would love to have the option to run on Android.

Just out of interest, what performance penalty could be expected for a 'wrapped' opencv for B4A?

FP
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Yes, OpenCV is extremely powerful. I think the answer to your question is: It depends :)

The way I have used OpenCV is for performing image processing. An example is finding the contours of the larger objects in a bitmap and returning rectangles surrounding those objects.

My library does this by accepting a standard Android bitmap as the calling function parameter. The library converts this bitmap to the OpenCV internal matrix representation, performs a dozen or so OpenCV functions that take about two pages of Java code, then return a string array containing the coordinates of the rectangles that surround the detected contours.

My interface to OpenCV requires one bitmap conversion at the start and conversion of the Java rectangles to a string representation (for simplicity) at the end. The stuff in the middle is fairly complex image processing Java OpenCV code. I call this an image processing string.

The OpenCV functions all use their custom matrix representation. Android bitmaps need to be converted to this matrix representation before any functions can be performed.

If one develops a complex image processing string by performing individual OpenCV functions passing and converting the bitmap at the beginning then converting and returning the bitmap at the end, this will become very inefficient. But, if one does a single conversion up front and maybe a single conversion at the end with a whole bunch of stuff in the middle, things will be really efficient.

This is the difficulty with developing a library. How do you develop a library that allows a string of arbitrary OpenCV functions to be called without doing a bunch of unnecessary data conversions back and forth between the development language and OpenCV functions.

There is another side to OpenCV that some are more concerned with – using their camera and video image capture interface. I have not needed to use these functions. I do not know what would be involved to use these.

Barry.
 
Last edited:
Upvote 0

floatingpoint

Member
Licensed User
Longtime User
Yes, OpenCV is extremely powerful. I think the answer to your question is: It depends :)

The way I have used OpenCV is for performing image processing. An example is finding the contours of the larger objects in a bitmap and returning rectangles surrounding those objects.
...
My interface to OpenCV requires one bitmap conversion at the start and conversion of the Java rectangles to a string representation (for simplicity) at the end. The stuff in the middle is fairly complex image processing Java OpenCV code. I call this an image processing string.

The OpenCV functions all use their custom matrix representation. Android bitmaps need to be converted to this matrix representation before any functions can be performed.
...

There is another side to OpenCV that some are more concerned with – using their camera and video image capture interface. I have not needed to use these functions. I do not know what would be involved to use these.

Barry.

Thank you for your insights.

I do similar stuff but in real time at min 25 FPS.
I would need the video image capture interface.
High resolution is not essential.

I'll certainly keep an eye on this forum.

FP
 
Upvote 0
Top