ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Optical Flow (광류 추적) 코드
    컴공지식/컴퓨터비전 2024. 10. 28. 22:28

    KLT 알고리즘 기반 광류 추적 코드

     

    #include <opencv2/opencv.hpp>
    #include <iostream>
    using namespace std;
    using namespace cv;

    void DrawTrackingPoints(vector<Point2f> &points, Mat &image) {
        for (int i = 0; i < points.size(); i++) {
            circle(image, points[i], 3, Scalar(255, 0, 0), 2);
        }
    }

    int main() {
        VideoCapture cap(0);
        if (!cap.isOpened()) {
            cout << "Cannot open camera" << endl;
            return 0;
        }

        Mat currImage, prevImage, frame;
        vector<Point2f> prevPoints, currPoints;
        bool initialization = false;

        while (true) {
            cap >> frame;
            if (frame.empty()) break;

            cvtColor(frame, currImage, COLOR_BGR2GRAY);
            if (initialization) {
                goodFeaturesToTrack(prevImage, prevPoints, 500, 0.01, 10);
                cornerSubPix(prevImage, prevPoints, Size(11, 11), Size(-1, -1),
                             TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 10, 0.01));
                DrawTrackingPoints(prevPoints, frame);
                initialization = false;
            }

            imshow("Optical Flow", frame);
            char ch = waitKey(33);
            if (ch == 27) break;  // ESC 키로 종료
            if (ch == 32) initialization = true;  // SPACE 키로 초기화
        }

        return 0;
    }

    '컴공지식 > 컴퓨터비전' 카테고리의 다른 글

    Projective transformation  (3) 2024.11.09
    YOLO 모델 로딩하는 코드  (0) 2024.11.04
    추적 (Tracking) 코드  (1) 2024.10.28
    얼굴 인식 코드  (1) 2024.10.28
    Tracking(추적)이란?  (2) 2024.10.28
Designed by Tistory.