B4A Library OpenCV 3.x

OpenCV (Open Source Computer Vision Library) is a really huge project/framework actively developed, mainly written in C++ . It is released under a BSD license.
Read more here: http://opencv.org/
OpenCV versions: https://opencv.org/releases/


OpenCV library for B4A: wraps the official OpenCV 3.x release for Android (in fact not all, about 95% of it)
  • Feel free to test and use it. You can even
  • License: You can use it for your projects, but you are not allowed to distribute nor sell this library. Of course you can distribute apps that use it (remember that OpenCV itself has BSD license as stated before)
  • Supported hardware is: armeabi-v7a and arm64-v8
  • Versions
    • 1.04 (2020/05/17)
      • This version wraps OpenCV 3.4.1 Android release and fixes some bugs (mostly some instance methods and some not exposed classes) of the previous version.
      • One of the major additions is the DNNmodule and related classes.
      • Link to the B4A library files: HERE :)
    • 1.00 (2017/09/27)
      • First B4A library wrapper (OpenCv320forB4A V1.00) which replicates (95%) the official OpenCV 3.20 Java API for Android.
      • (removed link. Use 1.04)

  • Please note that my support will be limited to issues with the wrapper itself, not to help translating OpenCV code from other languages to B4A 🤷‍♂️


========================================================================

(There may be some inaccuracies in what I expose in this post, related to the OpenCV project, since I am relatively new to it and don't know all about its internals. If you find any, please let me know and I will correct it)

A bit of explanation
There exist 'official' OpenCV wrappers for different languages and platforms. Android is one of them.
The official OpenCV 3.20 for Android API includes a lot of classes, organized in modules. But it does not include "all" the original OpenCV modules (since there are other 'experimental', non-free, or platform specific modules which may be present in other platforms but not for Android). Also, there are build options to "tune" it...

I have played quite a lot with it this last year, with a huge project which I started with inline Java, and also translating examples or testing features. But what I have used is just a small percent of the classes and methods exposed. So, there may be some (let's hope not too many) things to fix.

How to learn OpenCVforB4A
If you have worked before with OpenCV, the learning curve will be easy.
If it was using Java with OpenCV for Android, then it will be immediate, since all the methods have exactly the same syntax (except for initializers, polimorphism, and some special cases where simply I did my best).
Anyhow, the ways that I can think of, are (will add links later, also suggestions as online tutorials,.. are welcome)
  • Attached examples.
  • B4A OpenCV Tutorials. I will write a couple of them with what I consider the most important building pieces (for instance the Mat class, which in B4A is OCVMat) and modules
  • Internet examples: there are A LOT of examples over there, written in C++, Java, Python, JavaCV. I would look for examples in the language that is easier to understand for you and then try to translate. Some tips about it (based on my experience)
  • OpenCV syntax has changed as new versions. So there is an 'old' syntax in which nearly everything started with "cv...". Since version 3.X, there was 'cleaner' organization (project was written in C++ instead of C), and there were major syntax changes.
  • JavaCV: Translating from JavaCV to OpenCV should be quite easy but not always direct. JavaCV uses a mix of the old OpenCV syntax with some of its own, and at the beginning it can be a bit confusing, but then it is also easy.
  • Python: there is a lot of material...

First steps. Prepare for some crashes...
  • In OpenCV nearly everything takes part in the native code.
  • When we call a Sub/method/algorithm, it performs some internal checks to see if all the input data is correct. This check is perfomed in the native side. If something is not correct (wrong OCVMat dimensions, some incoherent parameters,...) it throws an exception and crashes. If we are lucky, perhaps we see in the log some clues about the check that made it crash.
  • On the good side, it is very easy to achieve results with OpenCV (check the examples). The real difficult part, as with many other things, is to fine-tune it: OpenCV has a collection of really powerful 'primitive' objects and operations, and really complex algorithms that can do many things. But it is the user who has to glue all of them to achieve the desired results.


(from the previous Beta announcement)
  • IMPORTANT: you must take this into account:
  • OpenCV (the included binary modules) is a free(*) project, but subject to license terms as described here: http://opencv.org/
    • (*): There are some modules in the OpenCV project which are on-free, but here I am refering to the ones included in the library
  • My work: (the B4A library) is free to test and use, but you can for it :). I'll keep donators updated with "advanced" material and examples
  • If you are interested, please PM me with your mail address and I will send you a link with the library and some basic examples. (be patient if you don't receive it immediately, I'll do it as soon as possible).
  • There is no documentation. In short, the syntax is nearly-exactly the same as the OpenCV3.20 Java API, adding "OCV" prefix and only the minimum modifications to adapt to B4A, For reference (taking into account described syntax changes) you can look at http://docs.opencv.org/java/3.1.0/ (which is not the latest one, but the API is nearly the same).
  • It would be preferable if you have worked before with OpenCV and/or can translate examples from Java/C++ and/or simply are interested in it.
  • I recommend starting with the examples and try to understand what is done. Just experimenting can lead to crash after crash of the native libraries with nearly no useful information, and can be very discouraging.
  • I forgot, the included binaries are for ameabi-v7a and arm64-v8 devices
---------------------------------------------------------------------
Some screenshots taken from the examples
Canny operator - Features2D - Color space conversion
s1.png
2D-FFT
s2.png
Color Blob detection
s3.png
 

Attachments

  • JavaCameraView2.zip
    2.7 KB · Views: 1,679
  • CameraOpenCvTest7.zip
    8.9 KB · Views: 1,162
  • BlobDetector5.zip
    15.3 KB · Views: 1,090
  • FaceDetector8.zip
    21 KB · Views: 1,149
Last edited:

Oke

Member
Licensed User
Longtime User
Greetings jordyCp, how to get face data to be stored in a database and match it in the future, like in an attendance machine ?
 

JordiCP

Expert
Licensed User
Longtime User
Greetings jordyCp, how to get face data to be stored in a database and match it in the future, like in an attendance machine ?
Hi Oke,

Face Detection is just the first step for Face Recognition. The second is a bit more complicated.

Opencv 3.20 for Android (from where the B4A lib was made) includes the OpenCV 3.20 'standard' modules. It can perform face detection but not recognition, since it would need one of the 'contrib' modules (specially, the module needed for face recognition is 'face', which includes EigenFaces, FisherFaces and LBPH). May be added in the future.

As an alternative approach, once you have the set of training images (you'll need a few of them), you could perhaps try Tensorflow deep learning (see THIS wrapper lib by @moster67 ). Not an expert here, but made some tests when it was published and it's really great!
 

zmrcic

Member
Licensed User
Hi there....working on some number detection app. I will report progress here as I go. It's working fine in Python...but when I try to write same code in B4A I have some problems, numpy first...
Can someone inspect my code?
B4A
Dim alpha As Float : alpha= 2.5
Dim threshold As Int : threshold = 37
Dim adjustment As Int : adjustment= 5
Dim iterations As Int : iterations= 1
Dim blur As Int : blur= 9
Dim mCore As OCVCore
Dim mScalarTmp As OCVScalar
mScalarTmp.Set(Array As Double(alpha,alpha))


Dim img As OCVMat:img = ROI

mCore.multiply5(img, mScalarTmp,img)
mImgProc.cvtColor(img, img, mImgProc.COLOR_BayerBG2GRAY,1)
Dim SPECTRUM_SIZE As OCVSize
SPECTRUM_SIZE.Set(Array As Double(blur,blur))

mImgProc.GaussianBlur(img, img, SPECTRUM_SIZE ,blur,blur, 0)
mImgProc.threshold ( img, img,threshold,mImgProc.ADAPTIVE_THRESH_GAUSSIAN_C, mImgProc.THRESH_BINARY)

Dim kernel_SIZE As OCVSize
kernel_SIZE.Set(Array As Double(1,1))

Dim kernel As OCVMat
kernel=mImgProc.getStructuringElement1(mImgProc.MORPH_RECT, kernel_SIZE)
mImgProc.erode1(img,img, kernel,Null, iterations)

Python
alpha = float(2.5)
threshold = 37
adjustment = 5
iterations = 1
blur = 9

img = cv2.imread('Moje.jpg')
exposure_img = cv2.multiply( img, numpy.array( [alpha])) #problem one....B4A and numpy
gray = cv2.cvtColor(exposure_img, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (blur, blur), 0)
thresh = cv2.adaptiveThreshold(blurred,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,threshold,adjustment) #problem two....no adjustment in B4A OpenCV

kernel = cv2.getStructuringElemen( cv2.MORPH_RECT, (1, 1))
eroded = cv2.erode (thresh, kernel, iterations)

#edged = cv2.Canny(gray, 30, 100)
inverse = (255 - eroded) #inverse image? bitwise_not or something else?

thanks....
 

JordiCP

Expert
Licensed User
Longtime User
A new version based on OpenCV 3.4.1 (so the thread title will be changed from OpenCV320... to OpenCV 3.X) is ready and will be released during today, after some tests.:D

Besides some minor bug fixes in the previous wrapper, one of the main additions is the DNN module, which allows for image classification using trained networks from other frameworks.

Here is the result of this OpenCV example ported to B4A

 

syerif

Active Member
Licensed User
Longtime User
OpenCV (Open Source Computer Vision Library) is a really huge project/framework actively developed, mainly written in C++ . It is released under a BSD license.
Read more here: http://opencv.org/
OpenCV versions: https://opencv.org/releases/


OpenCV library for B4A: wraps the official OpenCV 3.x release for Android (in fact not all, about 95% of it)
  • Feel free to test and use it. You can even
  • License: You can use it for your projects, but you are not allowed to distribute nor sell this library. Of course you can distribute apps that use it (remember that OpenCV itself has BSD license as stated before)
  • Supported hardware is: armeabi-v7a and arm64-v8
  • Versions
    • 1.04 (2020/05/17)
      • This version wraps OpenCV 3.4.1 Android release and fixes some bugs (mostly some instance methods and some not exposed classes) of the previous version.
      • One of the major additions is the DNNmodule and related classes.
      • Link to the B4A library files: HERE :)
    • 1.00 (2017/09/27)
      • First B4A library wrapper (OpenCv320forB4A V1.00) which replicates (95%) the official OpenCV 3.20 Java API for Android.
      • (removed link. Use 1.04)

  • Please note that my support will be limited to issues with the wrapper itself, not to help translating OpenCV code from other languages to B4A 🤷‍♂️


========================================================================

(There may be some inaccuracies in what I expose in this post, related to the OpenCV project, since I am relatively new to it and don't know all about its internals. If you find any, please let me know and I will correct it)

A bit of explanation
There exist 'official' OpenCV wrappers for different languages and platforms. Android is one of them.
The official OpenCV 3.20 for Android API includes a lot of classes, organized in modules. But it does not include "all" the original OpenCV modules (since there are other 'experimental', non-free, or platform specific modules which may be present in other platforms but not for Android). Also, there are build options to "tune" it...

I have played quite a lot with it this last year, with a huge project which I started with inline Java, and also translating examples or testing features. But what I have used is just a small percent of the classes and methods exposed. So, there may be some (let's hope not too many) things to fix.

How to learn OpenCVforB4A
If you have worked before with OpenCV, the learning curve will be easy.
If it was using Java with OpenCV for Android, then it will be immediate, since all the methods have exactly the same syntax (except for initializers, polimorphism, and some special cases where simply I did my best).
Anyhow, the ways that I can think of, are (will add links later, also suggestions as online tutorials,.. are welcome)
  • Attached examples.
  • B4A OpenCV Tutorials. I will write a couple of them with what I consider the most important building pieces (for instance the Mat class, which in B4A is OCVMat) and modules
  • Internet examples: there are A LOT of examples over there, written in C++, Java, Python, JavaCV. I would look for examples in the language that is easier to understand for you and then try to translate. Some tips about it (based on my experience)
  • OpenCV syntax has changed as new versions. So there is an 'old' syntax in which nearly everything started with "cv...". Since version 3.X, there was 'cleaner' organization (project was written in C++ instead of C), and there were major syntax changes.
  • JavaCV: Translating from JavaCV to OpenCV should be quite easy but not always direct. JavaCV uses a mix of the old OpenCV syntax with some of its own, and at the beginning it can be a bit confusing, but then it is also easy.
  • Python: there is a lot of material...

First steps. Prepare for some crashes...
  • In OpenCV nearly everything takes part in the native code.
  • When we call a Sub/method/algorithm, it performs some internal checks to see if all the input data is correct. This check is perfomed in the native side. If something is not correct (wrong OCVMat dimensions, some incoherent parameters,...) it throws an exception and crashes. If we are lucky, perhaps we see in the log some clues about the check that made it crash.
  • On the good side, it is very easy to achieve results with OpenCV (check the examples). The real difficult part, as with many other things, is to fine-tune it: OpenCV has a collection of really powerful 'primitive' objects and operations, and really complex algorithms that can do many things. But it is the user who has to glue all of them to achieve the desired results.


(from the previous Beta announcement)
  • IMPORTANT: you must take this into account:
  • OpenCV (the included binary modules) is a free(*) project, but subject to license terms as described here: http://opencv.org/
    • (*): There are some modules in the OpenCV project which are on-free, but here I am refering to the ones included in the library
  • My work: (the B4A library) is free to test and use, but you can for it :). I'll keep donators updated with "advanced" material and examples
  • If you are interested, please PM me with your mail address and I will send you a link with the library and some basic examples. (be patient if you don't receive it immediately, I'll do it as soon as possible).
  • There is no documentation. In short, the syntax is nearly-exactly the same as the OpenCV3.20 Java API, adding "OCV" prefix and only the minimum modifications to adapt to B4A, For reference (taking into account described syntax changes) you can look at http://docs.opencv.org/java/3.1.0/ (which is not the latest one, but the API is nearly the same).
  • It would be preferable if you have worked before with OpenCV and/or can translate examples from Java/C++ and/or simply are interested in it.
  • I recommend starting with the examples and try to understand what is done. Just experimenting can lead to crash after crash of the native libraries with nearly no useful information, and can be very discouraging.
  • I forgot, the included binaries are for ameabi-v7a and arm64-v8 devices
---------------------------------------------------------------------
Some screenshots taken from the examples
Canny operator - Features2D - Color space conversion
2D-FFT
Color Blob detection
hi, i try your code for face detector and return error not found file, xml and png where i can download it

Thanks
 

rbghongade

Active Member
Licensed User
Longtime User
Hi JordiCP,
Great work. Tried to compile the example. But am getting this compile error.
OpenCV Error.png

Thanks for the great library.
 

syerif

Active Member
Licensed User
Longtime User
I removed them by mistake. Just updated the project in the first post with the correct files.:)

i got new error can not load cascade file, i have attach your cascade file in asset, any sugestion to run your code

Thanks
 

JordiCP

Expert
Licensed User
Longtime User
i got new error can not load cascade file, i have attach your cascade file in asset, any sugestion to run your code
Now solved (Facedetector8 in the first post)
The original one was a bit old and made use of a low targetSDK (14) --> raised it to 28 and made 2 small changes
  • Cascade files need to be copied to internal storage first (OpenCV can't load it directly from assets). The original example made use of File.DirRootExternal. Now changed to File.DirInternal
  • Added runtimepermission for camera
 

rbghongade

Active Member
Licensed User
Longtime User
Thanks Erel and JordiCP, able to run it with real device.
One (silly) question: Can we use the same library with B4J?
 

JordiCP

Expert
Licensed User
Longtime User
This version is only for Android, some of the internal libs needed (specially the camera) are Android-specific.
A B4J version will come but can't say when 😉
 

wang xue feng

New Member
Licensed User
This version is only for Android, some of the internal libs needed (specially the camera) are Android-specific.
A B4J version will come but can't say when 😉
I want to complete the function of green screen button image, can it be realized? colorkey? thank you!
 
Top