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:

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
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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…