SURFDetectionDemo.java 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import org.opencv.core.Core;
  2. import org.opencv.core.Mat;
  3. import org.opencv.core.MatOfKeyPoint;
  4. import org.opencv.features2d.Features2d;
  5. import org.opencv.highgui.HighGui;
  6. import org.opencv.imgcodecs.Imgcodecs;
  7. import org.opencv.xfeatures2d.SURF;
  8. class SURFDetection {
  9. public void run(String[] args) {
  10. String filename = args.length > 0 ? args[0] : "../data/box.png";
  11. Mat src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
  12. if (src.empty()) {
  13. System.err.println("Cannot read image: " + filename);
  14. System.exit(0);
  15. }
  16. //-- Step 1: Detect the keypoints using SURF Detector
  17. double hessianThreshold = 400;
  18. int nOctaves = 4, nOctaveLayers = 3;
  19. boolean extended = false, upright = false;
  20. SURF detector = SURF.create(hessianThreshold, nOctaves, nOctaveLayers, extended, upright);
  21. MatOfKeyPoint keypoints = new MatOfKeyPoint();
  22. detector.detect(src, keypoints);
  23. //-- Draw keypoints
  24. Features2d.drawKeypoints(src, keypoints, src);
  25. //-- Show detected (drawn) keypoints
  26. HighGui.imshow("SURF Keypoints", src);
  27. HighGui.waitKey(0);
  28. System.exit(0);
  29. }
  30. }
  31. public class SURFDetectionDemo {
  32. public static void main(String[] args) {
  33. // Load the native OpenCV library
  34. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  35. new SURFDetection().run(args);
  36. }
  37. }