简体   繁体   English

OpenCV C ++多线程

[英]OpenCV C++ Multithreading

I have this opencv image processing function being called 4x on 4 diferent Mat objects. 我有这个opencv图像处理功能在4个不同的Mat对象上调用4x。

void processBinary(Mat& binaryMat) {
    //image processing
}

I want to multi-thread it so that all 4 method calls complete at the same time, but have the main thread wait until each thread is done. 我想多线程它,以便所有4个方法调用同时完成,但让主线程等到每个线程完成。

Ex: 例如:

int main() {

    Mat m1, m2, m3, m4;

    //perform each of these methods simultaneously, but have main thread wait for all processBinary() calls to finish
    processBinary(m1);
    processBinary(m2);
    processBinary(m3);
    processsBinary(m4);
}

What I hope to accomplish is to be able to call processBinary() as many times as I need and have the same efficiency as having the method called only once. 我希望完成的是能够根据需要多次调用processBinary(),并且具有与仅调用一次方法相同的效率。 I have looked up multithreading, but am a little confused on calling threads and then joining / detaching them. 我已经查找了多线程,但是在调用线程然后加入/分离它们时有点困惑。 I believe I need to instantiate each thread and then call join() on each thread so that the main thread waits for each to execute, but there doesn't seem to be a significant increase in execution time. 我相信我需要实例化每个线程,然后在每个线程上调用join(),以便主线程等待每个线程执行,但似乎没有显着增加执行时间。 Can anyone explain how I should go about multi-threading my program? 任何人都可以解释我应该如何多线程我的程序? Thanks! 谢谢!

EDIT : What I have tried: 编辑 :我尝试过:

//this does not significantly increase execution time. However, calling processBinary() only once does.4

    thread p1(&Detector::processBinary, *this, std::ref(m1));
    thread p2(&Detector::processBinary, *this, std::ref(m2));
    thread p3(&Detector::processBinary, *this, std::ref(m3));
    thread p4(&Detector::processBinary, *this, std::ref(m4));
    p1.join();
    p2.join();
    p3.join();
    p4.join();

The slick way to achieve this is not to do the thread housekeeping yourself but use a library that provides micro-parallelization. 实现这一目标的光滑方法不是自己进行线程管理,而是使用提供微并行化的库。

OpenCV itself uses Intel Thread Building Blocks (TBB) for exactly this task -- running loops in parallel. OpenCV本身使用英特尔线程构建模块 (TBB)来完成这项任务 - 并行运行循环。

In your case, your loop has just four iterations. 在您的情况下,您的循环只有四次迭代。 With C++11, you can write it down very easily using a lambda expression. 使用C ++ 11,您可以使用lambda表达式轻松地将其写下来。 In your example: 在你的例子中:

std::vector<cv::Mat> input = { m1, m2, m3, m4; }
tbb::parallel_for(size_t(0), input.size(), size_t(1), [=](size_t i) {
    processBinary(input[i]); 
});

For this example I took code from here . 在这个例子中,我从这里获取代码。

In case, you're using python language, then you can use my powerful open-source built-in multi-threaded vidgear OpenCV's wrapper python library available on GitHub and PyPI for achieving higher FPS. 如果您正在使用python语言,那么您可以使用我强大的开源内置多线程Vidgear OpenCV的GitHubPyPI上的包装python库来实现更高的FPS。

Project Insight: 项目洞察力:

VidGear is a lightweight python wrapper around OpenCV Video I/O module that contains powerful multi-thread modules(gears) to enable high-speed video frames capture functionality across various devices and platforms. VidGear是一个围绕OpenCV视频I / O模块的轻量级python包装器,它包含强大的多线程模块(齿轮),可以在各种设备和平台上实现高速视频帧捕获功能。

Features: 特征:

Key features which differentiate it from the other existing multi-threaded open source solutions are: 与其他现有多线程开源解决方案区别开来的主要功能包括:

  • Multi-Threaded high-speed OpenCV video-frame capturing( resulting in High FPS ) 多线程高速OpenCV视频帧捕获( 导致高FPS

  • Flexible Direct control over the video stream with easy manipulation ability 灵活直接控制视频流,操作简便

  • Lightweight 轻量级

  • Built-in Robust Error and frame synchronization Handling 内置鲁棒性错误和帧同步处理

  • Multi-Platform compatibility( Compatible with Raspberry Pi Camera also. ) 多平台兼容性(也兼容Raspberry Pi相机。

  • Full Support for Network Video Streams(Including Gstreamer Raw Video Capture Pipeline) 全面支持网络视频流(包括Gstreamer Raw Video Capture Pipeline)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM