Playing around with B4J and OpenCV

Cableguy

Expert
Licensed User
Longtime User
"I saw you!"
Very cool... Will it work with raspian and it's camera?
 

moster67

Expert
Licensed User
Longtime User
Never tested it with a Linux-OS but it should work since Linux is supported.
 

Cableguy

Expert
Licensed User
Longtime User
Can u propose a tutorial of some sort..?
I believe many will be interested in this
 

moster67

Expert
Licensed User
Longtime User
I had a look at the tutorial here which explains how to use OpenCV with JavaFX. Then I simply wrote a small wrapper exposing some of the methods in the code while other code is executed in the Java-code. Before that, I watched some YouTube videos which explained how to set up OpenCV on Windows using Intellij.

OpenCV is huge and would probably take ages to wrap for B4J/B4A. I have some ideas of wrapping some of the underlying classes but I think that if you want to use OpenCV with B4J, you really need to mix and match by wrapping certain java-code and run the rest as Java (or using perhaps inline Java) and write the code specific for a certain project/task. In this way, I think one should be able to integrate OpenCV in a B4J-project.

It might be easier to wrap/do something with JavaCV but I haven't yet tried it.
 

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,

I know there would be a lot of interest in Interfacing to OpenCV to B4J.

I played around with OpenCV and B4A a while ago. If you need this type of image processing, OpenCV is extremely powerful. With Android and B4A I quickly ran out of memory when processing images.

What I found myself doing was sending a bitmap from B4A to a library I had developed to interface to Android OpenCV. Within the library I would convert the bitmap to the Mat OpenCV structure, perform a bunch of image processing routines, then convert the result Mat back to a bitmap and return to B4A.

I can imagine something similar to this procedure would be powerful in B4J - convert a bitmap to the Mat structure, be able to invoke a bunch of OpenCV routines (without converting between bitmap and Mat with each routine), then return the Mat structure back to a bitmap for B4J.

I found there are couple other useful structures in OpenCV that were used all the time. Being able to convert these between OpenCV and B4J would be useful, but there were not that many.

I would love to see a B4J – OpenCV interface.

Barry.
 

JordiCP

Expert
Licensed User
Longtime User
It is a long-waited one, also for B4A

I played around with OpenCV and B4A a while ago. If you need this type of image processing, OpenCV is extremely powerful. With Android and B4A I quickly ran out of memory when processing images.

I have been playing with B4A + JavaCV, and at the end I could achieve some nice real-time processing from the camera. However, I found two big misses
  • There are some functions which are not optimized at all for Android (they are generally fast, but for instance some can take 10msec when a more complex one takes only 1msec), and replacing them by a pure 'C', without further optimizations, went much faster.
  • JavaCV syntax is based, at least in part, in the old -OpenCV C header style, which has been abandoned long ago. So it is a mess when looking at the documentation (specially if you are a beginner in OpenCV)

Now I got to build OpenCV library for Android, based on OpenCV Android SDK. No memory leaks, no strange things (up to now), and can call all its functions from inline Java (it uses the new syntax, but one gets easilly used to it). My project runs quite faster, but there are still some functions which I think are not totally optimized for Android. However, each new version has more and more improvements regarding efficiency.


Back to the thing, I also think it is impossible to make a whole wrapper for B4A/B4J, since there are so many functions and types that would make it giant.

But perhaps it wouldn't be such a nonsense to have wrapper B4X classes with Inline Java in them to make the "dirty" work (wrap the OpenCv SDK for Android classes in case of B4A). So it could become some sort of collaborative project in which people could contribute without need to recompile libraries. ...just a thought :rolleyes:. Still don't know if possible, but I will try to build a short example of what I mean.
 

moster67

Expert
Licensed User
Longtime User
My idea would be to run the processing in Java and return the result/output to B4J or B4A. One could write a simple wrapper (or use inline Java) to call the native methods for the processing. In my example, I simply returned the processed bitmap to B4J.
I guess the OpenCV camera could be wrapped as a B4J/B4A-object but Mat and other functionality is simply to big to wrap.
Anyway, it would be interesting as a project. Personally I am interested in motion, object and color-detection.
Maybe we could set up a Github-project and add functionality gradually.
 
Top