Files
opencv-MIRROR/samples/cpp/aliked_lightglue.cpp
Shelia J. fb90451e95 Merge pull request #29361 from SheliaJimenez:xfeat-feature
Xfeat feature - #29361

## PR Description

### Summary

Integrate XFeat into OpenCV's `features` module as a native `Feature2D` implementation, enabling lightweight neural feature detection and descriptor extraction through OpenCV's standard feature extraction API.

---

### What's included

#### New class

- **`cv::XFeat`** extends `Feature2D`
  - CNN-based keypoint detection
  - 64-D descriptor extraction via ONNX/DNN
  - Score-map based keypoint selection
  - Descriptor sampling from the dense feature map

---

### Files added

| File | Description |
|------|-------------|
| `src/feature2d_xfeat.cpp` | XFeat `Feature2D` implementation |
| `test/test_xfeat.cpp` | XFeat unit and regression tests |

---

### Files modified

- `features.hpp`
  - Add `cv::XFeat` declaration and public factory APIs

---

### Usage

```cpp
#include <opencv2/features.hpp>

using namespace cv;

// Feature extraction
Ptr<XFeat> xfeat =
    XFeat::create("xfeat.onnx", 2000, 0.5f, 640);

std::vector<KeyPoint> keypoints;
Mat descriptors;

xfeat->detectAndCompute(image, noArray(), keypoints, descriptors);
```

---


### Test dependency

Depends on the opencv_extra changes adding the XFeat ONNX model and reference outputs.

Required test data:
https://github.com/opencv/opencv_extra/pull/1383

- `xfeat.onnx`
- `xfeat_lena_640_kpts.npy`
- `xfeat_lena_640_desc.npy`

These files are required for the `Features2d_XFeat` tests in the main OpenCV repository to validate XFeat feature extraction and descriptor generation.


### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
2026-08-14 15:07:52 +03:00

209 lines
7.6 KiB
C++

// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
// Learned feature usage examples.
// Demonstrates ALIKED + LightGlue matching and XFeat feature extraction.
#include <opencv2/features.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
static int runXFeatExample(const String& imgPath, const String& xfeatModel, const String& outputPath)
{
Mat img = imread(imgPath);
if (img.empty())
{
cerr << "Error: cannot load image: " << imgPath << endl;
return -1;
}
Ptr<XFeat> xfeat = XFeat::create(xfeatModel, 2000, 0.05f, Size(640, 640));
vector<KeyPoint> keypoints;
Mat descriptors;
xfeat->detectAndCompute(img, Mat(), keypoints, descriptors);
Mat canvas;
drawKeypoints(img, keypoints, canvas, Scalar(0, 255, 0),
DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
if (!outputPath.empty())
{
imwrite(outputPath, canvas);
cout << "Saved XFeat keypoint visualization to: " << outputPath << endl;
}
imshow("XFeat Keypoints", canvas);
cout << "Press any key to exit..." << endl;
waitKey(0);
return 0;
}
int main(int argc, char** argv)
{
// ---- Parse arguments ----
String alikedModel, lightglueModel, imgPath1, imgPath2;
if (argc >= 4 && String(argv[1]) == "--xfeat")
{
const String outputPath = argc >= 5 ? argv[4] : String();
return runXFeatExample(argv[2], argv[3], outputPath);
}
if (argc >= 5)
{
imgPath1 = argv[1];
imgPath2 = argv[2];
alikedModel = argv[3];
lightglueModel = argv[4];
}
else
{
cout << "Usage:" << endl;
cout << " " << argv[0] << " <image1> <image2> <aliked_model> <lightglue_model>" << endl;
cout << " " << argv[0] << " --xfeat <image> <xfeat_model> [output_image]" << endl;
cout << endl;
cout << "Examples:" << endl;
cout << " " << argv[0] << " img1.jpg img2.jpg aliked-n16rot-top1k-640.onnx aliked_lightglue.onnx" << endl;
cout << " " << argv[0] << " --xfeat img.jpg xfeat.onnx xfeat_keypoints.jpg" << endl;
return 0;
}
// ---- Load images ----
Mat img1 = imread(imgPath1);
Mat img2 = imread(imgPath2);
if (img1.empty() || img2.empty())
{
cerr << "Error: cannot load images." << endl;
return -1;
}
// ================================================================
// 1. Create ALIKED feature extractor
// ================================================================
// Method A: From ONNX model file
Ptr<ALIKED> aliked = ALIKED::create(alikedModel);
// Method B: Customize parameters
// ALIKED::Params params;
// params.inputSize = Size(640, 640); // Network input resolution
// params.normalizeDescriptors = true; // L2-normalize descriptors
// Ptr<ALIKED> aliked = ALIKED::create(alikedModel, params);
// Method C: From in-memory model data
// vector<uchar> modelData = readFile(alikedModel);
// Ptr<ALIKED> aliked = ALIKED::create(modelData);
cout << "Descriptor size: " << aliked->descriptorSize() << endl; // 128
cout << "Descriptor type: " << aliked->descriptorType() << endl; // CV_32F
cout << "Default norm: " << aliked->defaultNorm() << endl; // NORM_L2
// ================================================================
// 2. Detect keypoints and compute descriptors
// ================================================================
vector<KeyPoint> kpts1, kpts2;
Mat descs1, descs2;
// Method A: detect + compute in one call (recommended)
aliked->detectAndCompute(img1, Mat(), kpts1, descs1);
aliked->detectAndCompute(img2, Mat(), kpts2, descs2);
// Method B: detect only (no descriptors)
// vector<KeyPoint> kpts;
// aliked->detect(img, kpts);
// Method C: compute only (from existing keypoints)
// Mat descs;
// aliked->compute(img, kpts, descs);
cout << "Image 1: " << kpts1.size() << " keypoints, descriptors " << descs1.rows << "x" << descs1.cols << endl;
cout << "Image 2: " << kpts2.size() << " keypoints, descriptors " << descs2.rows << "x" << descs2.cols << endl;
// ================================================================
// 3. Create LightGlueMatcher
// ================================================================
// Method A: From ONNX model file
Ptr<LightGlueMatcher> lg = LightGlueMatcher::create(lightglueModel);
// Method B: Customize parameters
// LightGlueMatcher::Params lgParams;
// lgParams.scoreThreshold = 0.1f; // Filter low-confidence matches
// lgParams.disableWinograd = false; // Keep Winograd convolution
// Ptr<LightGlueMatcher> lg = LightGlueMatcher::create(lightglueModel, lgParams);
// Method C: From in-memory model data
// vector<uchar> lgData = readFile(lightglueModel);
// Ptr<LightGlueMatcher> lg = LightGlueMatcher::create(lgData);
// ================================================================
// 4. Set keypoint context for LightGlue
// ================================================================
// LightGlue needs keypoint coordinates + image sizes for spatial reasoning.
// Build Nx2 float matrices with pixel coordinates.
Mat kpts1Mat((int)kpts1.size(), 2, CV_32F);
Mat kpts2Mat((int)kpts2.size(), 2, CV_32F);
for (size_t i = 0; i < kpts1.size(); i++)
{
kpts1Mat.at<float>((int)i, 0) = kpts1[i].pt.x;
kpts1Mat.at<float>((int)i, 1) = kpts1[i].pt.y;
}
for (size_t i = 0; i < kpts2.size(); i++)
{
kpts2Mat.at<float>((int)i, 0) = kpts2[i].pt.x;
kpts2Mat.at<float>((int)i, 1) = kpts2[i].pt.y;
}
// setPairInfo must be called before match()/knnMatch()
lg->setPairInfo(kpts1Mat, kpts2Mat, img1.size(), img2.size());
// ================================================================
// 5. Match descriptors
// ================================================================
// Method A: 1-to-1 matching (returns best match per query keypoint)
vector<DMatch> matches;
lg->match(descs1, descs2, matches);
cout << "1-to-1 matches: " << matches.size() << endl;
// Method B: kNN matching (k=1 only for LightGlue)
// vector<vector<DMatch>> knnMatches;
// lg->knnMatch(descs1, descs2, knnMatches, 1);
// // knnMatches[i] contains matches for query keypoint i
// ================================================================
// 6. Filter matches by confidence (optional)
// ================================================================
// DMatch distance = 1.0 - confidence_score
// Lower distance = better match
vector<DMatch> goodMatches;
float distanceThreshold = 0.9f; // confidence > 0.1
for (const auto& m : matches)
{
if (m.distance < distanceThreshold)
goodMatches.push_back(m);
}
cout << "Good matches (distance < " << distanceThreshold << "): " << goodMatches.size() << endl;
// ================================================================
// 7. Visualize results
// ================================================================
Mat canvas;
cv::drawMatches(img1, kpts1, img2, kpts2, goodMatches, canvas,
Scalar::all(-1), Scalar::all(-1), vector<char>(),
DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
imshow("ALIKED + LightGlue Matches", canvas);
cout << "Press any key to exit..." << endl;
waitKey(0);
return 0;
}