B4J Question Opencv Template Matching (inline java)

jroriz

Active Member
Licensed User
Longtime User
I'm trying to follow this example: https://riptutorial.com/opencv/example/22915/template-matching-with-java

I'm not a java programmer, so I'm having a hard time ...

The program returns the error below:
Waiting for debugger to connect...
Program started.
Error occurred on line: 18 (Main)
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at b4j.example.main._appstart(main.java:86)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
at b4j.example.main.start(main.java:45)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.UnsatisfiedLinkError: no opencv_java430 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at b4j.example.main.main(main.java:98)
... 30 more

I imported the java libraries and created a little program.


B4J project and opencv libs can be downloaded here.

If anyone can help, I appreciate it.

Code:
B4X:
#Region  Project Attributes
    #MainFormWidth: 100
    #MainFormHeight: 100
    #AdditionalJar: opencv-430

#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private NativeMe As JavaObject
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    NativeMe = Me
    Dim s As String = NativeMe.RunMethod("main", Null)
    Log(s)
End Sub

#If JAVA

import org.opencv.core.Core;
import org.opencv.core.Core.MinMaxLocResult;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;   
    
public static String main() {
    
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat source=null;
    Mat template=null;
    String filePath="C:\\meapague\\";
    //Load image file
    source=Imgcodecs.imread(filePath+"onde.jpg");
    template=Imgcodecs.imread(filePath+"oque.png");

    Mat outputImage=new Mat();   
    int machMethod=Imgproc.TM_CCOEFF;
    //Template matching method
    Imgproc.matchTemplate(source, template, outputImage, machMethod);


    MinMaxLocResult mmr = Core.minMaxLoc(outputImage);
    Point matchLoc=mmr.maxLoc;
    //Draw rectangle on result image
    Imgproc.rectangle(source, matchLoc, new Point(matchLoc.x + template.cols(),
            matchLoc.y + template.rows()), new Scalar(255, 255, 255));

    Imgcodecs.imwrite(filePath+"sonuc.jpg", source);
    return "Completed.";
}
 
#End If
 

Daestrum

Expert
Licensed User
Longtime User
I believe System.LoadLibrary only looks on the path or java.library.path for libraries (not our B4? type of libraries but .dll .so etc).
Set the system variable java.library.path to where the files are..
eg;
B4X:
SetSystemProperty("java.library.path","where/the/files/are")
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
I believe System.LoadLibrary only looks on the path or java.library.path for libraries (not our B4? type of libraries but .dll .so etc).
Set the system variable java.library.path to where the files are..
eg;
B4X:
SetSystemProperty("java.library.path","where/the/files/are")
Same error...
I tried SetSystemProperty("java.library.path",File.DirApp) and copied the libs to the file.dirapp.
Notice that the project has the directive #AdditionalJar: opencv-430 and the libs are in the additional libraries folder.
 
Last edited:
Upvote 0

Toky Olivier

Active Member
Licensed User
Longtime User
Using System.load instead of System.loadLibrary and slashes in the path names worked for me.

Java:
#If JAVA

import org.opencv.core.Core;
import org.opencv.core.Core.MinMaxLocResult;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;   
    
public static String UseCSV() {
    System.load("Z:/B4J/Downloaded/opencv libs/opencv_java430.dll");
    Mat source=null;
    Mat template=null;
    String filePath="E:/";
    //Load image file
    source=Imgcodecs.imread(filePath+"logo.png");
    template=Imgcodecs.imread(filePath+"logo_2.png");

    Mat outputImage=new Mat();   
    int machMethod=Imgproc.TM_CCOEFF;
    //Template matching method
    Imgproc.matchTemplate(source, template, outputImage, machMethod);


    MinMaxLocResult mmr = Core.minMaxLoc(outputImage);
    Point matchLoc=mmr.maxLoc;
    //Draw rectangle on result image
    Imgproc.rectangle(source, matchLoc, new Point(matchLoc.x + template.cols(),
            matchLoc.y + template.rows()), new Scalar(255, 255, 255));

    Imgcodecs.imwrite(filePath+"sonuc.jpg", source);
    return "Completed.";
}
 
#End If
 
Upvote 0
Top