1

I want to ask how can use multiple threads to works on array of OpenCV Mat images...

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:

    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? 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?

I mean: while second thread soglia works on first image of array, the first thread grey start works on secondo images..and so on....

How can I do this?

Thanks in advance for your attention and time.

4

1 回答 1

2

我假设您显示的 Windows 线程代码不是一成不变的。我无法弄清楚你想对他们做什么。因为你只有一个互斥体,(至少它看起来像这样),所以你所有的线程大部分时间都会被阻塞。

引用Intel TBB 在并行线程中运行函数?

来自TBB 教程

英特尔® 线程构建模块以线程为目标,以提高性能。大多数通用线程包支持许多不同类型的线程,例如图形用户界面中的异步事件线程。因此,通用包往往是提供基础的低级工具,而不是解决方案。相反,英特尔® 线程构建模块专注于并行化计算密集型工作的特定目标,提供更高级别、更简单的解决方案。

OpenCV 在内部使用 TBB(如果您使用 TBB 构建它),因此您查看源代码以了解事情是如何完成的。从 2.4.3 开始,OpenCV 有一个内置的 parallel_for_ 构造(使用 TBB)。您可以在http://answers.opencv.org/question/3730/how-to-use-parallel_for/找到更多相关信息

于 2013-05-28T10:40:26.333 回答