C++ 如何结合opencv和多线程?

C++ 如何结合opencv和多线程?,c++,multithreading,opencv,C++,Multithreading,Opencv,我是一名新的软件开发人员。我使用OPENCV开发了一个OCR项目。我想拆分图像,并考虑作为一个孤立的形象形象埃弗里的一部分。我想到了一个主意,为什么不使用多线程来最小化和优化执行时间。 谁有一个链接或例子,在OpenCV和多线程之间的C++结合。 非常感谢 您可以使用。以下是一个例子: #include "tbb/parallel_for.h" #include "tbb/blocked_range.h" using namespace tbb; class TaskPool { Ma

我是一名新的软件开发人员。我使用OPENCV开发了一个OCR项目。我想拆分图像,并考虑作为一个孤立的形象形象埃弗里的一部分。我想到了一个主意,为什么不使用多线程来最小化和优化执行时间。 谁有一个链接或例子,在OpenCV和多线程之间的C++结合。 非常感谢

您可以使用。以下是一个例子:

#include "tbb/parallel_for.h"
#include "tbb/blocked_range.h"

using namespace tbb;
class TaskPool {
    Mat image; /**< Image to process  */
    Task** t_vector;

public:
    // This empty constructor with an initialization list is used to setup calls to the function
    TaskPool(cv::Mat frame, Task** current_tasks)
    {
        image = frame;
        t_vector = current_tasks;
    }

  /*----------------------------------------------------------+
   | Here is the actual body, that will be called in parallel |
   | by the TBB runtime. You MUST put this code inside the    |
   | class definition, since the compiler will be expanding   |
   | and inlining this code as part of the template process.  |
   |                                                          |
   | The blocked_range<int> is something like a list of       |
   | indexes corresponding to each invocation of the function |
   +----------------------------------------------------------*/
    void operator() ( const blocked_range<int>& r ) const
    {
        for ( int i = r.begin(); i != r.end(); i++ )
        { // iterates over the entire chunk
            t_vector[i]->run(image);
        }

    }
};

// Here is the call to the parallelized code
void RunTasks(const Mat&image)
{
    // Each Task is a class containing a run(const Mat&) method which process some region of the image (e.g. calculates the histogram or whatever)
    vector<Task*> taskVector;
    // ... create/initialize the tasks here

    /// Do the TBB parallel stuff
    int k = trackVector.size();
    parallel_for(blocked_range<int>(0,k),
                 TaskPool(image,&(taskVector[0])));

}
#包括“tbb/parallel_for.h”
#包括“tbb/blocked_range.h”
使用名称空间tbb;
类任务池{
Mat图像;/**<要处理的图像*/
任务**t_向量;
公众:
//这个带有初始化列表的空构造函数用于设置对函数的调用
任务池(cv::Mat帧,任务**当前任务)
{
图像=帧;
t_向量=当前_任务;
}
/*----------------------------------------------------------+
|这是实际的身体,它将被并行调用|
|您必须将此代码放入|
|类定义,因为编译器将进行扩展|
|并将此代码内联为模板过程的一部分|
|                                                          |
|阻塞的_范围类似于|
|对应于函数每次调用的索引|
+----------------------------------------------------------*/
void运算符()(常数阻塞\u范围和r)常数
{
for(int i=r.begin();i!=r.end();i++)
{//迭代整个块
t_向量[i]->run(图像);
}
}
};
//下面是对并行化代码的调用
无效运行任务(常数矩阵和图像)
{
//每个任务都是一个包含run(const Mat&)方法的类,该方法处理图像的某个区域(例如,计算直方图或其他)
向量任务向量;
//…在此处创建/初始化任务
///做TBB并行的东西
int k=trackVector.size();
并联_(阻塞_范围(0,k),
任务池(图像和任务向量[0]);
}
正如您所看到的,您拥有处理任务类中每个图像区域的代码。然后,当调用
parallel_for
传递
TaskPool
构造函数时,它将处理多线程的内容

另一个选项包括,它甚至更易于使用(它还包括
parallel for
),但我在尝试将它用于某些版本的GCC编译器时遇到了一些问题。

您可以使用它。以下是一个例子:

#include "tbb/parallel_for.h"
#include "tbb/blocked_range.h"

using namespace tbb;
class TaskPool {
    Mat image; /**< Image to process  */
    Task** t_vector;

public:
    // This empty constructor with an initialization list is used to setup calls to the function
    TaskPool(cv::Mat frame, Task** current_tasks)
    {
        image = frame;
        t_vector = current_tasks;
    }

  /*----------------------------------------------------------+
   | Here is the actual body, that will be called in parallel |
   | by the TBB runtime. You MUST put this code inside the    |
   | class definition, since the compiler will be expanding   |
   | and inlining this code as part of the template process.  |
   |                                                          |
   | The blocked_range<int> is something like a list of       |
   | indexes corresponding to each invocation of the function |
   +----------------------------------------------------------*/
    void operator() ( const blocked_range<int>& r ) const
    {
        for ( int i = r.begin(); i != r.end(); i++ )
        { // iterates over the entire chunk
            t_vector[i]->run(image);
        }

    }
};

// Here is the call to the parallelized code
void RunTasks(const Mat&image)
{
    // Each Task is a class containing a run(const Mat&) method which process some region of the image (e.g. calculates the histogram or whatever)
    vector<Task*> taskVector;
    // ... create/initialize the tasks here

    /// Do the TBB parallel stuff
    int k = trackVector.size();
    parallel_for(blocked_range<int>(0,k),
                 TaskPool(image,&(taskVector[0])));

}
#包括“tbb/parallel_for.h”
#包括“tbb/blocked_range.h”
使用名称空间tbb;
类任务池{
Mat图像;/**<要处理的图像*/
任务**t_向量;
公众:
//这个带有初始化列表的空构造函数用于设置对函数的调用
任务池(cv::Mat帧,任务**当前任务)
{
图像=帧;
t_向量=当前_任务;
}
/*----------------------------------------------------------+
|这是实际的身体,它将被并行调用|
|您必须将此代码放入|
|类定义,因为编译器将进行扩展|
|并将此代码内联为模板过程的一部分|
|                                                          |
|阻塞的_范围类似于|
|对应于函数每次调用的索引|
+----------------------------------------------------------*/
void运算符()(常数阻塞\u范围和r)常数
{
for(int i=r.begin();i!=r.end();i++)
{//迭代整个块
t_向量[i]->run(图像);
}
}
};
//下面是对并行化代码的调用
无效运行任务(常数矩阵和图像)
{
//每个任务都是一个包含run(const Mat&)方法的类,该方法处理图像的某个区域(例如,计算直方图或其他)
向量任务向量;
//…在此处创建/初始化任务
///做TBB并行的东西
int k=trackVector.size();
并联_(阻塞_范围(0,k),
任务池(图像和任务向量[0]);
}
正如您所看到的,您拥有处理任务类中每个图像区域的代码。然后,当调用
parallel_for
传递
TaskPool
构造函数时,它将处理多线程的内容

另一个选项包括,它甚至更易于使用(它还包括
parallel for
),但我在尝试将它用于某些版本的GCC编译器时遇到了一些问题。

您可以使用它。以下是一个例子:

#include "tbb/parallel_for.h"
#include "tbb/blocked_range.h"

using namespace tbb;
class TaskPool {
    Mat image; /**< Image to process  */
    Task** t_vector;

public:
    // This empty constructor with an initialization list is used to setup calls to the function
    TaskPool(cv::Mat frame, Task** current_tasks)
    {
        image = frame;
        t_vector = current_tasks;
    }

  /*----------------------------------------------------------+
   | Here is the actual body, that will be called in parallel |
   | by the TBB runtime. You MUST put this code inside the    |
   | class definition, since the compiler will be expanding   |
   | and inlining this code as part of the template process.  |
   |                                                          |
   | The blocked_range<int> is something like a list of       |
   | indexes corresponding to each invocation of the function |
   +----------------------------------------------------------*/
    void operator() ( const blocked_range<int>& r ) const
    {
        for ( int i = r.begin(); i != r.end(); i++ )
        { // iterates over the entire chunk
            t_vector[i]->run(image);
        }

    }
};

// Here is the call to the parallelized code
void RunTasks(const Mat&image)
{
    // Each Task is a class containing a run(const Mat&) method which process some region of the image (e.g. calculates the histogram or whatever)
    vector<Task*> taskVector;
    // ... create/initialize the tasks here

    /// Do the TBB parallel stuff
    int k = trackVector.size();
    parallel_for(blocked_range<int>(0,k),
                 TaskPool(image,&(taskVector[0])));

}
#包括“tbb/parallel_for.h”
#包括“tbb/blocked_range.h”
使用名称空间tbb;
类任务池{
Mat图像;/**<要处理的图像*/
任务**t_向量;
公众:
//这个带有初始化列表的空构造函数用于设置对函数的调用
任务池(cv::Mat帧,任务**当前任务)
{
图像=帧;
t_向量=当前_任务;
}
/*----------------------------------------------------------+
|这是实际的身体,它将被并行调用|
|您必须将此代码放入|
|类定义,因为编译器将进行扩展|
|并将此代码内联为模板过程的一部分|
|                                                          |
|阻塞的_范围类似于|
|对应于函数每次调用的索引|
+----------------------------------------------------------*/
void运算符()(常数阻塞\u范围和r)常数
{
for(int i=r.begin();i!=r.end();i++)
{//迭代整个块
t_向量[i]->run(图像);
}
}
};
//下面是对并行化代码的调用
无效运行任务(常数矩阵和图像)
{
//每个任务都是一个包含run(const Mat&)方法的类,该方法处理任务的某个区域