I want to ask how can I use a non void function as function to run with thread.
I mean, a function such as this:
void example(Mat& in, Mat& out)
How I can use this function for a thread with beginthreadx?
Paste the code that I've to trasform in multithreads code:
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <stdio.h>
#include <windows.h>
#include <process.h>
using namespace std;
using namespace cv;
//filling array
void acquisisci (Mat in[]){
in[0]=imread("C:/OPENCV/Test/imgtest/bird1.jpg",1);
in[1]=imread("C:/OPENCV/Test/imgtest/bird2.jpg",1);
in[2]=imread("C:/OPENCV/Test/imgtest/bird3.jpg",1);
in[3]=imread("C:/OPENCV/Test/imgtest/pig1.jpg",1);
in[4]=imread("C:/OPENCV/Test/imgtest/pig2.jpg",1);
in[5]=imread("C:/OPENCV/Test/imgtest/pig3.jpg",1);
}
//grey function
void elabora (Mat& in, Mat& out){
if (in.channels()==3){
cvtColor(in,out,CV_BGR2GRAY); //passa al grigio
}
}
//threshold function
void sogliata(Mat& in, Mat& out){
threshold(in,out,128,255,THRESH_BINARY);//fa la soglia
}
//view
void view (Mat& o){
imshow("Immagine",o);
waitKey(600);
}
int main(){
Mat in[6],ou[6],out[6];
acquisisci(in);
for (int i=0;i<=5;i++){
elabora(in[i],ou[i]);
}
for (int i=0;i<=5;i++){
sogliata(ou[i],out[i]);
}
for (int i=0;i<=5;i++){
view(out[i]);
}
return 0;
}
can I do this with parallel threads??
_beginthreadex requires a specific signature for a thread function.
void(*thread_func)(void *);
To use a function with a different signature, you normally just create a "thunk" -- a small function that does nothing but call the function you really want called:
struct params {
Mat ∈
Mat &out;
};
void thread_func(void *input) {
params *p = (params *)input;
example(input->in, input->out);
}
You'll probably also need to include something like a Windows Event to signal when the data is ready in the output -- you don't want to try to read it before the function in the thread has had a chance to write the data:
struct params {
Mat ∈
Mat &out;
HANDLE event;
params() : event(CreateEvent(NULL, 0, 0, NULL)) {}
~params() { CloseHandle(event); }
};
void thread_func(void *input) {
params *p = (params *)input;
example(input->in, input->out);
SetEvent(input->event);
}
Then the calling function with start thread_func, and when it needs the result, do something like a WaitForSingleObject or WaitForMultipleObjects on the event handle, so it can continue processing when it has the data it needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With