Question regarding OpenMP + OpenCV (frame processing) [closed]

asked 2016-03-26 04:44:28 -0600

Nbb gravatar image

updated 2016-03-28 13:01:31 -0600

Hello

I was hoping someone with knowledge in OpenMP would be able to assist me. I am trying to read in a frame using a single thread then have 3 separate threads doing different operations on the frame. I would then merge and display the results on a single frame. The problem is that I am getting weird results for eg the flip operation does not get called.

Mat frame
omp_set_num_threads(3);
#pragma omp parallel
{
    int TID = omp_get_thread_num();

    while(true)
    {
        vector<Object> objects;
        #pragma omp single
        {
              // read in frame
              flip(frame, frame, 1);
        } 

        if(TID == 0)
        {
            // process frame
            // Store result in vector<Object>
        }

        else
        {
            // process frame with a different operation
            // Store result in vector<Object>
        }

        #pragma omp barrier
        if(TID == 0) // #pragma omp single causes program to hang
        {
              // merge results
              // do some drawing on frame based on results
              imshow(frame);
              waitKey(1);
        } 
    }
}
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-12-10 05:28:55.980739

Comments

1

Try moving the declaration of Mat frame inside the loop. If you only have one, the memory is shared between the three threads and gets over-written with each read().

Tetragramm gravatar imageTetragramm ( 2016-03-27 07:50:23 -0600 )edit

Thanks ! That would mean I do not have to include shared (frame) right ? What about drawing the results on a single frame ? #pragma omp single has an implicit flush right ? that way that section of the code would be able to access the objects from all the different threads ?

Nbb gravatar imageNbb ( 2016-03-27 12:35:51 -0600 )edit

Yes. As for drawing, you probably want to use a named window, which will overwrite the image with the latest and will allow you to draw to the same window. I believe that you can just move the declaration of frame inside the while loop and it will work as is. Need to try though. Honestly I only barely use OpenMP, so I can't really help.

Tetragramm gravatar imageTetragramm ( 2016-03-27 14:03:07 -0600 )edit

I have moved Mat frame inside the loop but it now crashes. Thread 0 sees an empty Mat. Its able to show the first frame before it crashes meaning its able to run through the while loop once ?

Nbb gravatar imageNbb ( 2016-03-28 04:15:51 -0600 )edit

"have 3 separate threads doing different operations on the frame" When you flip (frame,frame) memory used by other thread need to access to frame so something coulb be wrong. In parallelize scheme you cannot change source data while thread are running.

example :

Mat frame;....

first thread flip(frame,frame2)

second thread sobel(frame,frame3

...

LBerger gravatar imageLBerger ( 2016-03-28 05:21:38 -0600 )edit

I realized that the code I posted earlier was slightly wrong. #pragma omp single causes the program to hang. Also, I realized that the code above works sometimes i.e. the output image is the flipped image and sometimes it does not, the output image randomly shows the flipped / non flipped image. I have read through http://openmp.org/mp-documents/ntu-va... and tried various methods like flushing before drawing and flushing right after flipping the image but I am still not getting the right results. I hope someone can help me on this

Nbb gravatar imageNbb ( 2016-03-28 08:39:27 -0600 )edit

My first program using openmp : load a color image split plane and 3 threads calculate gradient for each plane

int main(int argc, char* argv[])
{


    Mat img=imread("f:/lib/opencv/samples/data/graf1.png"),imgRes;
    vector<Mat> plan;
    omp_set_num_threads(img.channels());
    split(img, plan);
#pragma omp parallel 
    {
        int nb = omp_get_thread_num();
        Sobel(plan[nb], plan[nb], CV_8UC1, 1, 0);
#pragma omp critical 
        cout << nb << "\n";
    }
#pragma omp barrier    
    merge (plan,imgRes);
    imshow("Result",imgRes);
    waitKey();


   return 0;
}

No parameters are checked

LBerger gravatar imageLBerger ( 2016-03-28 09:28:18 -0600 )edit

Hello berger, thanks for your continued help. I think the reason why im facing these problems is that my code above never leaves the parallel region because its in a while loop. Though im not sure why displaying the image in the single section causes it to hang... I have thought of a few ways to debug to figure out what is going on though I will only try it in 8 hours time. Gotta get some rest. I guess ill edit my post with the changes I have in my code. Thanks !

Nbb gravatar imageNbb ( 2016-03-28 13:06:30 -0600 )edit

You can find good example in this blog http://berenger.eu/blog/c-openmp-exam...

LBerger gravatar imageLBerger ( 2016-03-28 14:54:48 -0600 )edit