简体   繁体   English

在Visual C ++上,如何处理带有线程的图像数组(并行性)

[英]On visual C++ how can I works on an array of images with threads (parallelism)

I want to ask how can use multiple threads to works on array of OpenCV Mat images... 我想问一下如何使用多个线程来处理OpenCV Mat图像数组...

In simple words: using suggestion give me from user of this site, I have packed two array of six images in a struct to pass it at thread: 简而言之:使用本站点用户的建议,我将两个阵列的六个图像打包在一个结构中以在线程中传递它:

struct Args
{
     Mat in[6];
     Mat out[6];
};

In main code I populate the input "in" array with six images, with this code, and assing it to struct in array: 在主代码中,使用此代码用六个图像填充输入“ in”数组,并将其赋值为数组中的struct:

    Mat inn[6],ou[6];

    inn[0]=imread("C:/OPENCV/Test/imgtest/bird1.jpg",1);
inn[1]=imread("C:/OPENCV/Test/imgtest/bird2.jpg",1);
inn[2]=imread("C:/OPENCV/Test/imgtest/bird3.jpg",1);
inn[3]=imread("C:/OPENCV/Test/imgtest/pig1.jpg",1);
inn[4]=imread("C:/OPENCV/Test/imgtest/pig2.jpg",1);
inn[5]=imread("C:/OPENCV/Test/imgtest/pig3.jpg",1);

Args dati;
*dati.in = *inn;
*dati.out = *ou;

Now I want to use multiple threads to process this images...all six images, and store them in output array to visualize them. 现在,我想使用多个线程来处理此图像...所有六个图像,并将它们存储在输出数组中以使其可视化。

the functions are: 功能是:

 //greyscale funct
 void grey (void *param){
while (TRUE)
{
WaitForSingleObject(mutex,INFINITE);
Args* arg = (Args*)param;
cvtColor(*arg->in,*arg->out,CV_BGR2GRAY);
ReleaseMutex(mutex);
}
_endthread();
 }
  //threshold funct
 void soglia(void *param){
while (TRUE)
{
WaitForSingleObject(mutex,INFINITE);
Args* arg = (Args*)param;
threshold(*arg->out,*arg->out,128,255,THRESH_BINARY);
ReleaseMutex(mutex);
}
_endthread();
 }
 //output
void visualizza(void *param){
while (TRUE)
{
WaitForSingleObject(mutex,INFINITE);
Args* arg = (Args*)param;
imshow("Immagine",*arg->out);
waitKey(10);
ReleaseMutex(mutex);
}
//_endthread();
  }

using a mutex object to make them thread safe. 使用互斥对象使它们线程安全。 These functions using cast to make conversion from void to Args...but, if I want to process all 6 images of input array, and I think to use for cicle, how can I modify these functions to accept and use array with "i" position? 这些函数使用强制转换来实现从void到Args的转换...但是,如果我想处理输入数组的所有6张图像,并且我想将其用于cicle,如何修改这些函数以接受并使用带有“ i”的数组位置? Because I use for cicle without use threads and works...but with threads and for have a parallelism, how can I modify these functions for elaborate every images of input array? 因为我使用cicle时不使用线程并且可以工作...但是使用线程并具有并行性,我如何修改这些函数以详细说明输入数组的每个图像?

I mean: while second thread soglia works on first image of array, the first thread grey start works on secondo images..and so on.... 我的意思是:第二个线程soglia在数组的第一个图像上工作,而第一个线程灰色开始在第二个图像上工作..依此类推....

How can I do this? 我怎样才能做到这一点?

Thanks in advance for your attention and time. 在此先感谢您的关注和时间。

I am assuming that the windows threading code you have shown isn't set in stone. 我假设您显示的Windows线程代码不是一成不变的。 I couldn't work out what you are trying to do with them. 我无法弄清楚您要如何处理他们。 SInce you only have one mutex, (at least that what it looks like), all your threads will be blocked most of the time. 因为只有一个互斥锁(至少是它的样子),所以大多数时间所有线程都将被阻塞。

To quote from Intel TBB run a function in a parallel thread? 引用Intel TBB的说法,在并行线程中运行一个函数? :

From the TBB tutorial 来自TBB教程

Intel® Threading Building Blocks targets threading for performance. 英特尔®线程构建模块以线程为目标以提高性能。 Most general-purpose threading packages support many different kinds of threading, such as threading for asynchronous events in graphical user interfaces. 大多数通用线程包都支持许多不同类型的线程,例如图形用户界面中异步事件的线程。 As a result, general-purpose packages tend to be low-level tools that provide a foundation, not a solution. 结果,通用软件包往往是提供基础而非解决方案的低级工具。 Instead, Intel® Threading Building Blocks focuses on the particular goal of parallelizing computationally intensive work, delivering higher-level, simpler solutions. 取而代之的是,英特尔®线程构建模块专注于并行化计算密集型工作,提供更高级别,更简单解决方案的特定目标。

OpenCV uses TBB internally (if you build it with TBB) so you look at the source to see how things are done. OpenCV在内部使用TBB(如果使用TBB进行构建),因此您可以查看源代码以了解事情的完成方式。 Since 2.4.3 OpenCV has an inbuilt parallel_for_ construct (which uses TBB). 从2.4.3开始,OpenCV具有内置的parallel_for_构造(使用TBB)。 You can find out some more about it at http://answers.opencv.org/question/3730/how-to-use-parallel_for/ 您可以在http://answers.opencv.org/question/3730/how-to-use-parallel_for/中找到有关它的更多信息。

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

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