B4A Library OpenCV 3.x

BobsYourUncle

Member
Licensed User
@JordiCP this looks like a really great library! Very clever - I'm impressed! Just to confirm, will it allow me to change colors and contrast of the camera preview image in realtime? (Can the altered preview be saved?) If it works I will definitely donate generously!!
 

JordiCP

Well-Known Member
Licensed User
Sure, you can do all of these things with OpenCV, even from a live preview and save the modified preview. You can change a given color, or those ones that are "similar" to it (with a predefined distance function), since two consecutive frames in a preview won't necessarily give the exact same colors for an object (due to light changes, noise, ...)

However, the max resolution at which it will work "smooth" will depend on the device and the amount of operations made. Also, depending on how you want to control the camera, you can use CameraEx (larger code, but more control) or JavaCameraView (simplest code, no control).
 

biometrics

Active Member
Licensed User
@JordiCP

Is it possible to use a OCVJavaCameraView without a panel? I want to convert our app to a service and I can't have a panel in a service. I don't need to display the camera frames, I just want to process them in the newFrame event.

If I try initialize it and pass null for the panel parameter then I get this error, as can be expected:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.addView(android.view.View, android.view.ViewGroup$LayoutParams)' on a null object reference
 
Last edited:

JordiCP

Well-Known Member
Licensed User
OCVJavaCameraView (and also CameraEx), need a Panel since they are intended to be used inside an activity

The good news is that the Android camera object itself doesn't need to be in an activity. When it is initted you must link it to a preview surface, but it can be of any size, out of the screen, and not necessarily belonging to an activity.

So there are (at least) 2 options :)
  • Init the Android camera from inline Java in a service, and then raise a B4A event with the preview data where you will make the same processing as now (I think I have a half-made example somewhere, PM me if you are interested in it)
  • I can change the JavaCameraView behaviour so that it doesn't need a Panel (then you'll be able to call it from a service), but I can't tell you when, since I'm a bit short of time.
 

ZJP

Active Member
Licensed User
Hi,

Trying to make an sort of 'Plickers Card Reader' with your lib (a very nice one), i'm stuck actually with the usage of findContours1.

Do you have an example with this function?

Thanks.
 

JordiCP

Well-Known Member
Licensed User

JordiCP

Well-Known Member
Licensed User
Please note that in the example you have both the original Java part and ALSO the B4A translation.

(These lines are also in the example, and are the equivalent of the commented Java part)
B4X:
    Dim contours As List
    contours.Initialize
    mImgProc.findContours1(mDilatedMask,contours,mHierarchy,mImgProc.RETR_LIST,mImgProc.CHAIN_APPROX_SIMPLE)
 

BarryW

Active Member
Licensed User
I am happy to announce that OpenCv320forB4A V1.0 is (at last) available.
(Thanks a lot to beta-testers for feedback received)

************************************************
(2017/09/27) This is a link to the library ;). I may remove or change it in the future.
https://drive.google.com/drive/folders/0B-eBY3pamAunWFN3dHl4NXIxODQ?usp=sharing

************************************************


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


OpenCV320forB4A: is a B4A library wrapper which replicates (95%) the official OpenCV 3.20 Java API for Android.
  • The lib is too big to be uploaded. If you want to use it, just PM me with your mail address and I will send it to you.
  • It is free to test and use. You can and this will encourage me to maintain it, and also build additional material for donators.
  • 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


(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 sir. How about the face recognition. Is it supported?
 

JohnC

Expert
Licensed User
Can this lib be used to do this...

I have a database of pictures of various objects (less then 1000), and I would like to develop an app that allows the user to take a picture of one of those objects and have the app find the image in the database that is the closest match to the picture that was just taken by the user.

Can this lib be used to do that?

I'm hoping that the accuracy can be relatively high because of the limited dataset (the 1000 images) that it needs to find a match with.
 

JordiCP

Well-Known Member
Licensed User
Hard to say without details...
Are you talking about one object tor many objects at once? Does the background move? Is the background homogeneous? Does the source come from the device camera? Which kind of movement do they have? ....

If there is a low-level algorithm in C++ or Matlab that does it, it can be "possibly" translated to OpenCV. If this algorithm makes use of libs, then you'll have to see if there exists something similar in OpenCV.
 

JordiCP

Well-Known Member
Licensed User
Not in a near future, sorry :(
If I'm not mistaken, DNN module was part of the 'extra' modules in v3.20, so they weren't part of the official Android 3.20 release. It was moved to the 'main' modules since v3.30.

Perhaps TENSORFLOW lib from @moster67 would also suit your needs :)?
 

rdkartono

Member
Licensed User
I am wrapping OpenCV version 4 for B4A now, and having problem with JavaCameraView. I want to make like your OCVJavaCameraView, so simple in use.
If I put JavaCamera2View.java and CameraBridgeViewBase.java in Eclipse, can compile them to B4A Library (no error).
When I am trying to load them, there is a error as attachment.

Could you help sharing your OCVJavaCameraView code ?
 

Attachments

Guillermo Gonzalez

New Member
Licensed User
Dear Jordi,
I am trying to use the "OCVImgproc.fillPoly1(img As OCVMat, pts As java.util.List, color As OCVScalar)" command to define a region of interest in a Mat. Can you give me some hints how I can enter the second argument correctly? It is supposed to be an array of vertices, which have x and y coordinates.
 

moster67

Expert
Licensed User
Maybe something like this (not tested and values just invented):

B4X:
           Dim mImgProc As OCVImgproc
    Dim Img As OCVMat
    Img.Initialize
    Dim myScalar As OCVScalar
    myScalar.Initialize3(255,255,255)
    
    Dim p1 As OCVPoint
    p1.Initialize(4,8)
    Dim p2 As OCVPoint
    p2.Initialize(2,10)
    Dim p3 As OCVPoint
    p3.Initialize(16,6)
    Dim p4 As OCVPoint
    p4.Initialize(10,4)
    Dim p5 As OCVPoint
    p5.Initialize(12,4)
    Dim p6 As OCVPoint
    p6.Initialize(2,10)
    
    Dim arrPoint(6) As OCVPoint = Array As OCVPoint(p1,p2,p3,p4,p5,p6)
    
    Dim matPt As OCVMatOfPoint
    matPt.fromArray(arrPoint)
    Dim ppt As List
    ppt.Initialize
    ppt.Add(matPt)
    
    mImgProc.fillPoly1(Img, ppt,myScalar)
 
Top