You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

OpenCV Java(3.4.1)中DescriptorExtractor已过时,求替代方案

Fixing Deprecated DescriptorExtractor in OpenCV 3.4.1 Java

Hey there, I totally get the frustration with outdated classes and incomplete docs—let's get this sorted out for your image stitching project.

First, the reason DescriptorExtractor is marked deprecated in OpenCV 3.x is because the library refactored its feature detection/extraction pipeline. The old split between FeatureDetector and DescriptorExtractor was merged into a single Feature2D interface, with concrete implementations for each feature type (ORB, SIFT, SURF, etc.).

Here are the exact replacements you need:

1. Use concrete Feature2D subclasses directly

Instead of instantiating DescriptorExtractor via a factory method, create an instance of the specific feature extractor you want. For example:

  • For ORB (a common free, open alternative):

    // Old (deprecated)
    // DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
    
    // New replacement
    ORB orb = ORB.create();
    // You can also pass parameters like number of features: ORB.create(500);
    
  • For SIFT/SURF (note: SURF may have patent restrictions in some regions):
    These live in the xfeatures2d module, so make sure your precompiled Windows library includes opencv_xfeatures2d341.dll (it should be part of the standard 3.4.1 package):

    SIFT sift = SIFT.create();
    // or
    SURF surf = SURF.create();
    

2. Use the generic Feature2D factory (if you prefer a similar pattern to the old code)

If you want to keep a more flexible, string-based factory approach (like the old DescriptorExtractor.create()), you can use Feature2D.create():

Feature2D extractor = Feature2D.create("ORB");
// Or "SIFT", "SURF", etc.

3. Update your feature extraction workflow

The Feature2D interface combines detection and extraction into one method (detectAndCompute()), which replaces the old separate detect() and compute() calls. Here's how to use it:

MatOfKeyPoint keypoints = new MatOfKeyPoint();
Mat descriptors = new Mat();
// The second parameter is a mask (pass empty Mat if you don't need it)
orb.detectAndCompute(yourImage, new Mat(), keypoints, descriptors);

Quick note on OpenCV Java docs

You're right that the Java docs for 3.4.1 are often incomplete. A trick is to cross-reference the C++ docs—since the Java bindings are auto-generated, the API structure is almost identical. The C++ docs clearly state that DescriptorExtractor is deprecated in favor of Feature2D subclasses, which translates directly to Java.

Example snippet for your image stitching setup

Here's a condensed example showing the updated feature extraction part:

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.features2d.ORB;
import org.opencv.imgcodecs.Imgcodecs;

public class ImageStitcher {
    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat img1 = Imgcodecs.imread("first_image.jpg");
        Mat img2 = Imgcodecs.imread("second_image.jpg");

        // Initialize ORB descriptor extractor/replacer
        ORB featureExtractor = ORB.create(500);

        // Extract features for both images
        MatOfKeyPoint kp1 = new MatOfKeyPoint();
        Mat desc1 = new Mat();
        featureExtractor.detectAndCompute(img1, new Mat(), kp1, desc1);

        MatOfKeyPoint kp2 = new MatOfKeyPoint();
        Mat desc2 = new Mat();
        featureExtractor.detectAndCompute(img2, new Mat(), kp2, desc2);

        // Proceed with feature matching, homography calculation, and stitching...
    }
}

Just make sure your project is linked against the correct OpenCV 3.4.1 native libraries, and you should be good to go!

内容的提问来源于stack exchange,提问作者Tim Cooper

火山引擎 最新活动