Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Cách theo dõi màu sắc trong OpenCV C++


Giới thiệu

Theo dõi màu sắc mở rộng từ phát hiện màu: tính diện tích đối tượng phát hiện, xác định tâm (centroid) qua moments, vẽ đường line nối các tâm liên tiếp để hiện thực hóa quỹ đạo chuyển động.

Chương trình theo dõi màu sắc

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

int main(int argc, char** argv) {
    VideoCapture video_load(0);
    namedWindow("Adjust");
    int Hue_Low = 0, Hue_high = 22;
    int Sat_Low = 99, Sat_high = 255;
    int Val_Low = 0, Val_high = 255;

    createTrackbar("LowH", "Adjust", &Hue_Low, 179);
    createTrackbar("HighH", "Adjust", &Hue_high, 179);
    createTrackbar("LowS", "Adjust", &Sat_Low, 255);
    createTrackbar("HighS", "Adjust", &Sat_high, 255);
    createTrackbar("LowV", "Adjust", &Val_Low, 255);
    createTrackbar("HighV", "Adjust", &Val_high, 255);

    int Horizontal_Last = -1, vertical_Last = -1;
    Mat temp; video_load.read(temp);
    Mat track_motion = Mat::zeros(temp.size(), CV_8UC3);

    while (true) {
        Mat actual_Image; video_load.read(actual_Image);
        Mat converted_to_HSV; cvtColor(actual_Image, converted_to_HSV, COLOR_BGR2HSV);
        Mat adjusted_frame;
        inRange(converted_to_HSV, Scalar(Hue_Low, Sat_Low, Val_Low), Scalar(Hue_high, Sat_high, Val_high), adjusted_frame);

        // Morphological operations: opening then closing
        erode(adjusted_frame, adjusted_frame, getStructuringElement(MORPH_ELLIPSE, Size(5,5)));
        dilate(adjusted_frame, adjusted_frame, getStructuringElement(MORPH_ELLIPSE, Size(5,5)));
        dilate(adjusted_frame, adjusted_frame, getStructuringElement(MORPH_ELLIPSE, Size(5,5)));
        erode(adjusted_frame, adjusted_frame, getStructuringElement(MORPH_ELLIPSE, Size(5,5)));

        Moments detecting_object = moments(adjusted_frame);
        double vertical_moment = detecting_object.m01;
        double horizontal_moment = detecting_object.m10;
        double tracking_area = detecting_object.m00;

        if (tracking_area > 10000) {
            int posX = horizontal_moment / tracking_area;
            int posY = vertical_moment / tracking_area;
            if (Horizontal_Last >= 0 && vertical_Last >= 0 && posX >= 0 && posY >= 0) {
                line(track_motion, Point(posX, posY), Point(Horizontal_Last, vertical_Last), Scalar(0, 0, 255), 2);
            }
            Horizontal_Last = posX;
            vertical_Last = posY;
        }
        imshow("Detected_Object", adjusted_frame);
        actual_Image = actual_Image + track_motion;
        imshow("Actual", actual_Image);
        cout << "Vị trí đối tượng: " << Horizontal_Last << "," << vertical_Last << endl;
        if (waitKey(30) == 27) break;
    }
    return 0;
}

Kết quả

Cửa sổ "Adjust" có 6 trackbar điều chỉnh HSV. Cửa sổ "Detected_Object" hiển thị mask nhị phân màu. Cửa sổ "Actual" hiển thị video gốc với đường đỏ vẽ quỹ đạo chuyển động của đối tượng màu.