简体   繁体   English

java.lang.UnsatisfiedLinkError 尝试使用来自 OpenCV 的 SURF

[英]java.lang.UnsatisfiedLinkError when trying to use SURF from OpenCV

Hello I am currently trying to execute Feature Matching with FLANN from OpenCV in Java.您好,我目前正在尝试从 Java 中的 OpenCV 执行与 FLANN 的特征匹配。

Here is code of this Tutorial: https://docs.opencv.org/master/d5/d6f/tutorial_feature_flann_matcher.html这是本教程的代码: https://docs.opencv.org/master/d5/d6f/tutorial_feature_flann_matcher.html

My Project was created with "Java with Ant"我的项目是用“Java with Ant”创建的

I have added the following dependencies我添加了以下依赖项

aistcv-4.5.3.jar, opencv-453.jar and opencv_java453.dll to Project folder. aistcv-4.5.3.jar、opencv-453.jar 和 opencv_java453.dll 到项目文件夹。

When I try to run this code an error message comes up.当我尝试运行此代码时,会出现一条错误消息。

run:
Exception in thread "main" java.lang.UnsatisfiedLinkError: org/opencv/xfeatures2d/SURF.create_0(DIIZZ)J
at org.opencv.xfeatures2d.SURF.create(SURF.java:92)
at surfflannmatchingdemo.SURFFLANNMatching.run(SURFFLANNMatchingDemo.java:43)
at surfflannmatchingdemo.SURFFLANNMatchingDemo.main(SURFFLANNMatchingDemo.java:80)
C:\Users\Juergen\AppData\Local\NetBeans\Cache\12.5\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\Juergen\AppData\Local\NetBeans\Cache\12.5\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 0 seconds)

What am I doing wrong?我究竟做错了什么?

SURF is still patented. SURF 仍然获得专利。

you can only use it IF you build from src with OPENCV_ENABLE_NONFREE=ON.如果您使用 OPENCV_ENABLE_NONFREE=ON 从 src 构建,则只能使用它。

(you also probably dont have any contrib modules if it is the prebuilt jar file from SF) (如果它是来自 SF 的预构建 jar 文件,您也可能没有任何贡献模块)

try to replace it with SIFT尝试用SIFT替换它

OpenCV Java Feature Matching with FLANN OpenCV Java 与 FLANN 的特征匹配

If any body have the same trouble like me, here is the Solution.如果有人遇到像我一样的麻烦,这里是解决方案。

import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.DMatch;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Scalar;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.Features2d;

import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.xfeatures2d.SURF;

import org.opencv.features2d.SIFT;




class SURFFLANNMatching {
 public void run(String[] args) {
    String filename1 = args.length > 1 ? args[0] : "foto_111.png";
    String filename2 = args.length > 1 ? args[1] : "foto_222.png";
    Mat img1 = Imgcodecs.imread(filename1, Imgcodecs.IMREAD_GRAYSCALE);
    Mat img2 = Imgcodecs.imread(filename2, Imgcodecs.IMREAD_GRAYSCALE);
    if (img1.empty() || img2.empty()) {
        System.err.println("Cannot read images!");
        System.exit(0);
    }
    
    
    
    //-- Step 1: Detect the keypoints using SURF Detector, compute the descriptors
    double contrastThreshold = 0.03; 
    double edgeThreshold = 2.0;
    double sigma = 1.0;
    int nOctaveLayers = 3; 
    int hessianThreshold = 400;
    boolean extended = false; 
    boolean upright = false;
            
// make error   SURF detector = SURF.create(hessianThreshold, nOctaves, nOctaveLayers, extended, upright);
    
// Solution start.
    SIFT detector = SIFT.create(hessianThreshold, nOctaveLayers, contrastThreshold, edgeThreshold, sigma);
// Solution stop.
    
    MatOfKeyPoint keypoints1 = new MatOfKeyPoint(), keypoints2 = new MatOfKeyPoint();
    Mat descriptors1 = new Mat(), descriptors2 = new Mat();
    
    detector.detectAndCompute(img1, new Mat(), keypoints1, descriptors1);
    detector.detectAndCompute(img2, new Mat(), keypoints2, descriptors2);
    
    //-- Step 2: Matching descriptor vectors with a FLANN based matcher
    // Since SURF is a floating-point descriptor NORM_L2 is used
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
    List<MatOfDMatch> knnMatches = new ArrayList<>();
    matcher.knnMatch(descriptors1, descriptors2, knnMatches, 2);
    
    //-- Filter matches using the Lowe's ratio test
    float ratioThresh = 0.7f;
    List<DMatch> listOfGoodMatches = new ArrayList<>();
    for (int i = 0; i < knnMatches.size(); i++) {
        if (knnMatches.get(i).rows() > 1) {
            DMatch[] matches = knnMatches.get(i).toArray();
            if (matches[0].distance < ratioThresh * matches[1].distance) {
                listOfGoodMatches.add(matches[0]);
            }
        }
    }
    MatOfDMatch goodMatches = new MatOfDMatch();
    goodMatches.fromList(listOfGoodMatches);
    //-- Draw matches
    Mat imgMatches = new Mat();
    Features2d.drawMatches(img1, keypoints1, img2, keypoints2, goodMatches, imgMatches, Scalar.all(-1),
            Scalar.all(-1), new MatOfByte(), Features2d.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS);
    //-- Show detected matches
    HighGui.imshow("Good Matches", imgMatches);
    HighGui.waitKey(0);
    System.exit(0);
 }
}

public class SURFFLANNMatchingDemo {
  public static void main(String[] args) {
    // Load the native OpenCV library
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    new SURFFLANNMatching().run(args);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM