Phương thức detect() của org.opencv.features2d.Feature2D (abstract) phát hiện key points. Nhận Mat nguồn và MatOfKeyPoint rỗng.
Vẽ key points bằng drawKeypoints() của org.opencv.features2d.Features2d.
Lưu ý quan trọng
- Feature2D là abstract class → phải instance subclass (ví dụ: FastFeatureDetector).
- Features2D và Features2d là 2 class khác nhau trong package features2d.
Ví dụ minh họa
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Scalar;
import org.opencv.features2d.FastFeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
public class DetectingKeyPoints {
public static void main(String[] args) throws Exception {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String file = "D:\\Images\\javafx_graphical.jpg";
Mat src = Imgcodecs.imread(file);
Mat dst = new Mat();
MatOfKeyPoint matOfKeyPoints = new MatOfKeyPoint();
FastFeatureDetector featureDetector = FastFeatureDetector.create();
featureDetector.detect(src, matOfKeyPoints);
// Vẽ key points
Features2d.drawKeypoints(src, matOfKeyPoints, dst, new Scalar(0, 0, 255));
HighGui.imshow("Feature Detection", dst);
HighGui.waitKey();
}
}
Hình ảnh đầu vào và kết quả
Input: 
Output: 