I am building an Android App that includes image processing techniques. From the Runtime point of view, which is better JavaCV or OpenCV ?
Their runtime overhead seems to be about the same, but the android-opencv wrappers do not give access to raw data via direct NIO buffers, rendering custom processing in Java a lot less efficient. JavaCV is more efficient for those tasks. Being the author of JavaCV, I also like its API better :) It's closer to the original C/C++ API than android-opencv.
EDIT: Another difference is that android-opencv forces users to use the NDK and JNI to write functions. Since JavaCV is based on JavaCPP, we can write native functions more easily without having to deal with neither the NDK nor JNI.
Writing native code using Android NDK will usually be much faster than using the SDK.
You can find nice tutorial here, using opencv with NDK. Notice that with NDK framework you have access to openGL functions.
JavaCV is just a Java wrapper around the native OpenCV library. OpenCV does provide a Java/SWIG binding. I have used both bindings and didn't see a significant difference between the two.
If you are writing a native only app go with native OpenCV.
Related
I'm working on an OpenCV project that is currently prototyped in python, but I want to make work in Android.
I thought it would be better to start with straight java and then move that to Android, but OpenCV for java doesn't seem to be as fully implemented as it is for Python and C++ (for example it doesn't have imshow). So now I'm not sure how easy the conversion from Python to Java will be.
Since Python to C++ has been relatively easy in the past for me when working with OpenCV, it got me thinking of other options. I see that you can run pre-compiled C++ code in Android, so that may be an option. The issue that I see is that I'm not sure how well I can pass the data I need. What sorts of limitations are there when communicating between Android Java and the pre-compiled C++? And is the pre-compiled C++ allowed access to the filesystem, so it can read images?
You can pass any data that java supports. Obviously you can't pass C++ classes as is (though you can pass a pointer as long, keep in Java and then pass it back to C++ where it can be used again).
On C++ side, through JNI APIs you can do everything you can do with Java code. However, it will require an extra work and clear understanding about thread binding, JNI context and JNI/Java memory management.
On Java side, you can pass to C functions strings, primitive types (easy) or complex types and then use JNI to access fields, call methods, throw exceptions and so on.
There are some pitfalls about memory management, if you allocate Java heap from inside C++/JNI, read carefully about allocations, lifecycle and reference types.
Android-specific, you also may want to read some JNI Tips
Android native can access IO and filesystem at least with the same functionality as regular Android Java apk. You may look here for an example and a discussion File Operations in Android NDK
Can anyone explain me what is the difference between OpenCV and OpenCL? What is suitable for Android image processing in Java?
OpenCL is a standard for large scale parallel processing, it can help image processing but it is very low level and is designed for simplify the way to take advantage of many cpu cores and gpu stream processors.
OpenCV is a library for computer vision, includes a lot of generic image processing routines and high level functions to support face recognition etc.
It is quite easy to have an eye on wikipedia or google with two terms.
Just to add another point, there are functions in OpenCV that are implemented using OpenCL. These can be called using the "ocl" modules. Have a look here: ocl documentation in OpenCV
I'm a complete newbie in the computer vision world and recently I implemented some examples using OpenCV with Java. I'm impressed with the potential this area has and wish to continue learning more.
I learned that OpenCV is written in C++ and while Java has a wrapper (JavaCV) I understood that the applications in Java are slower than in C++ and most enterprise application are written in C++.
My question is this: I have few years of experience in Java and I feel very comfortable to write any application with it; would it be smart to move to C++ to learn CV or should I stick with Java and use its wrapper.
Computer vision is a demanding area - and while it is true that you'd best stay with what you know, and move to opencv only if performance is needed, another truth is that you'll need to go deep into mathematics, pointers and algorithms to learn and build a good computer vision app. And to do that in Java can be more cumbersome than learning c++.
So, if all you want to do is to apply a filter over an image for some school project - go for Java. But if you want to stay more with OpenCV, to learn vision algorithms or to write your own, my strong suggestion is to learn C++ - isn't that scary!
A reason to write native code is flexibility - you'll want to do all kind of tricks that are difficult or performance-killers in Java.
Shortly speaking, learning C++ is much simpler than computer vision. And OpenCV is not just a library you want to call to do some processing out there. It's bleeding edge technology - you'll want to understand it, to hack into it, to build over it, to go through the code, much more than call someJNIfunc();
And if you do so, please be aware that OpenCV offers two interfaces - one for C and one for C++. And while they offer the same functionality, the C++ one is much like Java - with automatic memory management and more sweeties. You can refer to this post to see the differences
I suggest sticking with what you're comfortable with for now. Only switch to C++ when you find that it doesn't expose certain APIs you want or performance actually becomes a problem. Right now, you're in the learning phase.
JavaCV uses a wrapper called JavaCPP to call OpenCV from Java programs. JavaCPP automatically generates temporary native libraries that form a bridge used by JNI to let Java programs call the OpenCV native libraries.
The solution is elegant and it works well, but is quite finicky about installing just the right versions and having classpaths set correctly. You can get a glimpse of the difficulties people face at the JavaCV discussion forum, and at http://code.google.com/p/javacv/wiki/Windows7AndOpenCV.
I got this working with OpenCV-2.3.1 on XP, Windows 7, and Ubuntu 10.11, and still it took me several days to get it all updated to OpenCV-2.4.2. ffmpeg is especially tricky to get right across all platforms.
There is little or no speed overhead if you are using Java for high level program control because image objects and list-based data structures are maintained on the native side through pointers. One pitfall is knowing who is responsible for releasing allocated memory, so be prepared for VM crashes with complicated programs.
There is a bit overhead in transferring data objects to the Java side. I find that it takes about 1 microsecond to copy a keypoint location into a Java-side Point object. This doesn't sound like much but in a real-time application with thousands of keypoints it can make a difference. We also copy JavaCV IplImage objects to Java through ByteBuffers. This takes a millisecond or less so is quite feasible for real-time use.
In our case, we have a substantial body of Java code to leverage against OpenCV. And Java's garbage collection makes many things much much easier. I are satisfied that the overhead of learning JavaCV has been well worth it.
I found it necessary to build the project in Eclipse and compile JavaCV from source instead of using javacv.jar. (You'll need the other .jar files though.) This lets you examine exceptions to track down library version and classpath errors. And the JavaCV source is needed to understand how JavaCV exposes the OpenCV API.
How much time is spent in the OpenCV library and how much time is spend in your program? If your program entirely in C++ it cannot reduce the time spent in your program (outside the library) to less than nothing. e.g. if you spend 99% of your time in the library, using C++ cannot make it more than 1% faster.
For simple programs, Java and C++ doesn't make any significant difference in speed. But for a large code with lot of computational complexity, C++ turns out to be faster.
Wrappers do have a problem of overhead. But this will be negligible for a small program. If you write a complex code in Java, it wont be easy to rewrite it into C++. This is because of the large no of functions available in Java which will be so much different from C++.
Whether you should use Java or C++ for OpenCV depends on your motive.
If you seriously want to lean OpenCV and work on some big projects , I suggest you to move on to C++. But if you are looking for having just some fun with OpenCV, it will be better to stick to what you know.
Among C++ or Java, better use C++. I have almost no experience in Java, but the reason I would recommend C++ is its common usage among ML&CV libraries.
One of the best solution of powerful and flexible computer vision application may be next sequence:
training a model using flexible Python and popular machine learning libraries;
storing pre-trained weights;
rewriting the best model architechture to C++ using OpenCV library with useful, but not extensive Mat class or compiling to C++ ML model;
compiling with definite specification of your device processor.
Advances of this solution are application work speed, code safety, development speed and compatibility with many devices.
Moreover, Python Tensorflow and other models could be loaded to C++, e.g. PyTorch.
Besides, it is much more easier to get help from CV community with the C++ OpenCV code.
Disadvantages are tons of development problems and development speed (which may not be omitted with Java), sometimes strictful flexibility limits of OpenCV lib, and many others you may not think of at the beginning.
I'm looking into using OpenCV for an Android application and am running into some problems compiling the samples which use Native OpenCV.
So, my question is:
When using OpenCV, when will I need to use Native OpenCV code? Can I get by without using it (as in Tutorial 1 within the samples)?
Apologies for the vague question, I can be more specific if that would help.
Thanks for any help in advance.
It depends on what you want to do, really. Native calls are very expensive in terms of speed, so if this is relevant for your application, you should reduce them to the minimum possible. Most of the Android OpenCV libraries rely on native calls, so going native or using OpenCV Java calls should not give a relevant increase in terms of performance time. But there is nothing like testing it.
You would also like to use native code if you already have C/C++ OpenCV implemented code. That would save you some time re-writing it in Java. You should also use native code, if the functions you want weren't already ported to Android (it is up to 42%, currently).
I've been playing around with the NDK recently, finding that many of the tutorials available online really don't help. I've been using this tutorial and I've got it running great.
However. Is this the correct use of the NDK? I mean if I have a game say with many classes all in C++ that I wish to port over to the android. Do I have to really manually change all my methods to the likes of:
JNIEXPORT jstring JNICALL Java_com_domain_ndk_NativeLib_hello
(JNIEnv * env, jobject obj) {
return (*env)->NewStringUTF(env, "Hello World!");
}
I can't see this being a very efficient way of porting my code over and I get the feeling I'm using the NDK wrong. I also have no idea how the NDK samples are supposed to work. Could anyone point me in the right direction?
Thanks
You do not need to change ANY of your existing methods to use the "auto-bound" JNI format. There aren't good examples in the Google NDK samples, but the Google SDK uses the other JNI binding method internally: Search for RegisterNatives() in the JNI docs. But even then, you really only need to create a wrapper for the few places that the Android SDK needs to talk to your library: user input/sensors in general, a draw callback, and possibly an "update" callback. Aside from that the rest of your app should remain untouched, except to get it to build.
To simplify things, I'd recommend you not use the native widgets; if you keep your game entirely in OpenGL, you can avoid going through Java to, e.g., draw textures to the screen, at least if you target 1.6 devices and newer, where the NDK has native bindings for OpenGL.
Contrary to some opinions (including those of Google), the NDK can and frequently is used for porting games to Android. I'm using it that way right now, in fact. You need to pass user events from Java to C++, and you need to have Java set up the basic GL context (there are no EGL bindings in the NDK), but you can do pretty much everything else in C++ with OpenGL and, once your basic Java messages are being passed to C++, you can (mostly) ignore Java. Thank goodness. :)
You'll still need to talk to Java for sound, and to get access to your files: Both still need to go through Java, and so you'll probably want to use "reverse JNI," where you call a Java function from C++. Also not hard, and there are examples all over the place.
I talk more about how to access your assets here: File Operations in Android NDK
Good luck.
I'd say your way isn't wrong, but be aware that passing data between Java and C/C++ code is a time consuming thing. Because of that I would suggest you write the most of the code actually in C/C++ and just call C/C++ functions from Java if it can't be avoided. For example you will need to pass data back for the GUI. Especially when you are programming a real time game I would avoid passing data between Java and C/C++ code too much. So just changing all the methods and then calling them in your Java code wouldn't be such a great idea.
Btw if you have to pass something back from C/C++ to Java, I would not pass it as a return value, but rather give the destination as a parameter in the function call.
The NDK is just a native C and C++ development environment for Android. Bionic (the Android version of libc) is more slightly more limited not by much. The only real complication is integrating you new component into the Android build tool chain. Android has a highly customized Makefile system. Downloading the source and looking at simple native components should give you an idea how to use it though.
Now if you want to integrate Java with C or C++ you will need to understand the Java Native Interface (JNI). You should be able to find good documentation of the JNI online.
The new release of the ndk (r5) now includes the capability to write apps entirely in native code.