Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

วิธีตรวจจับ Key Points ของภาพด้วย OpenCV Java

เมธอด detect() ของคลาส org.opencv.features2d.Feature2D (abstract) ตรวจจับ key points ของภาพที่กำหนด ต้องส่งอ็อบเจ็กต์ Mat แทน source image และอ็อบเจ็กต์ MatOfKeyPoint ว่างเพื่อเก็บ key points

คุณสามารถวาด key points ที่ตรวจพบบนภาพได้ด้วยเมธอด drawKeypoints() ของคลาส org.opencv.features2d.Features2d

หมายเหตุ

  • เนื่องจาก Feature2D เป็น abstract class คุณต้องสร้างอินสแตนซ์ของ subclass เพื่อเรียก detect() ที่นี่ใช้ FastFeatureDetector class
  • Features2D และ Features2d เป็นคลาสต่างกันในแพ็กเกจ features2d อย่าสับสน...

ตัวอย่างโค้ด

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 {
        // โหลด OpenCV core library
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        
        // อ่านเนื้อหาภาพ
        String file = "D:\\Images\\javafx_graphical.jpg";
        Mat src = Imgcodecs.imread(file);
        
        // อ่าน key points ของภาพ
        Mat dst = new Mat();
        MatOfKeyPoint matOfKeyPoints = new MatOfKeyPoint();
        FastFeatureDetector featureDetector = FastFeatureDetector.create();
        featureDetector.detect(src, matOfKeyPoints);
        
        // วาด key points ที่ตรวจพบ
        Features2d.drawKeypoints(src, matOfKeyPoints, dst, new Scalar(0, 0, 255));
        
        HighGui.imshow("Feature Detection", dst);
        HighGui.waitKey();
    }
}

รูปภาพ Input

วิธีตรวจจับ Key Points ของภาพด้วย OpenCV Java

ผลลัพธ์

วิธีตรวจจับ Key Points ของภาพด้วย OpenCV Java