Path: blob/master/samples/java/tutorial_code/features2D/feature_detection/SURFDetectionDemo.java
16354 views
import org.opencv.core.Core;1import org.opencv.core.Mat;2import org.opencv.core.MatOfKeyPoint;3import org.opencv.features2d.Features2d;4import org.opencv.highgui.HighGui;5import org.opencv.imgcodecs.Imgcodecs;6import org.opencv.xfeatures2d.SURF;78class SURFDetection {9public void run(String[] args) {10String filename = args.length > 0 ? args[0] : "../data/box.png";11Mat src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);12if (src.empty()) {13System.err.println("Cannot read image: " + filename);14System.exit(0);15}1617//-- Step 1: Detect the keypoints using SURF Detector18double hessianThreshold = 400;19int nOctaves = 4, nOctaveLayers = 3;20boolean extended = false, upright = false;21SURF detector = SURF.create(hessianThreshold, nOctaves, nOctaveLayers, extended, upright);22MatOfKeyPoint keypoints = new MatOfKeyPoint();23detector.detect(src, keypoints);2425//-- Draw keypoints26Features2d.drawKeypoints(src, keypoints, src);2728//-- Show detected (drawn) keypoints29HighGui.imshow("SURF Keypoints", src);30HighGui.waitKey(0);3132System.exit(0);33}34}3536public class SURFDetectionDemo {37public static void main(String[] args) {38// Load the native OpenCV library39System.loadLibrary(Core.NATIVE_LIBRARY_NAME);4041new SURFDetection().run(args);42}43}444546