下面是一个简单的实现示例:

#include <iostream>
#include <thread>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

// 相机采图线程
void captureThread(Mat& img) {
    VideoCapture cap(0);
    if (!cap.isOpened()) {
        cerr << "Failed to open camera!" << endl;
        return;
    }

    while (true) {
        cap >> img;
        if (img.empty()) {
            cerr << "Failed to capture image!" << endl;
            break;
        }
    }
}

// 实时显示图像线程
void displayThread(const Mat& img) {
    namedWindow("Display", WINDOW_NORMAL);

    while (true) {
        imshow("Display", img);
        waitKey(1);
    }
}

// 特征点匹配线程
void matchThread(const Mat& img) {
    Mat gray;
    vector<KeyPoint> keypoints;
    Mat descriptors;

    Ptr<FeatureDetector> detector = ORB::create();
    Ptr<DescriptorExtractor> extractor = ORB::create();

    while (true) {
        cvtColor(img, gray, COLOR_BGR2GRAY);

        detector->detect(gray, keypoints);
        extractor->compute(gray, keypoints, descriptors);

        // 计算速度
        double speed = keypoints.size() * descriptors.cols;

        // 将速度传递给主线程
        // ...

        this_thread::sleep_for(chrono::milliseconds(100));
    }
}

int main() {
    Mat img;
    thread capture(captureThread, ref(img));
    thread display(displayThread, cref(img));
    thread match(matchThread, cref(img));

    // 主线程实时显示速度并实时绘制速度曲线
    // ...

    capture.join();
    display.join();
    match.join();

    return 0;
}

需要注意的是,由于多线程涉及到共享变量的读写,需要使用refcref来传递引用,避免出现数据竞争的问题。同时,需要使用mutex等同步机制来保证共享变量的安全访问。具体实现还需要根据实际情况进行进一步的完善和调试。

c++多线程 三个子线程一个子线程调用相机采图一个子线程实时显示图像一个子线程利用opencv特征点匹配计算速度主线程运用实时显示速度并实时绘制速度曲线

原文地址: https://www.cveoy.top/t/topic/brZJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录