Ask Your Question
0

multithreading in c++, capturing thread stops after some loops

asked 2019-03-20 05:30:38 -0600

nobot gravatar image
  • opencv 3.4.2
  • OS ubuntu 16.04
  • g++ 7.4.0

    I am trying capture and process image on different threads.

    In Mcapture(), the image is captured; and after that if the processing threads completed processing, then the captured image is divided into 2 parts and moved to global variables, else it goes on capturing.

    In the other 2 threads , firstly the loop for saving the frame into global variables is paused, and after that a dummy processing is done, and after that , 2 half processed image is combined to a global UMat, to get the full image.

The code is running indefinitely, but after some 9-10 iterations , In Mcapture() it is going through the capture loop but it is failing to enter the loop (which divides the image and moves it to a global variable). And also the output of the combined processed images are not in sync with each other

Here is the link for my source_code, op_log, and the op_image. It will be very helpful, to get some help, I am getting no clue why that loop is stopping in between.

edit retag flag offensive close merge delete

Comments

please put (the relevant part of) your code into the question, not on an external dropbox, thank you.

berak gravatar imageberak ( 2019-03-20 06:32:38 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-03-20 09:58:50 -0600

kbarni gravatar image

Well, your code is quite complicated, I didn't test it, and I don't understand all of it either. This is more a comment (but it's too long, so I have to put it as an answer). So a few ideas:

  • keep your code simple, it's much easier to find a bug.
  • use opencv routines to copy an image (frm.copyTo()) instead of std::move.
  • capturing from a webcam doesn't take much time, so it isn't necessary to parallelize it*.

It will be much easier to capture the image, then do the processing in parallel, and when it's ready, capture the next frame. Here's a pseudocode:

Rect roi[10];
Mat frm(480,640,CV_8UC3);
Mat subfrm[10];
for(int i=0;i<10;i++)subfrm[i]=frm(roi[i]);  //define 10 ROIs on the image
while(1){
    cap>>frm;   //get the image
    for(int i=0;i<10;i++)thread[i].run(subfrm[i]);  //launch a thread for each ROI
    for(int i=0;i<10;i++)thread[i].join();  //wait for all the threads to finish
    imshow("Result",frm); //display the result
}

This code solves the problem of combining the frames, and saves some time because it won't make copies of the image. You won't need global variables or concurrency check (mutex).

*Of course, this isn't true anymore if you want to capture a large quantity of data (e.g. high resolution RAW image with high framerate) in a time-critical application.

edit flag offensive delete link more

Comments

Thank you for the suggestion, but i am using the threads for these exact purposes, my input is 1920*1080 of 60 fps, and it has to be done in a little endian ARM processor odroid xu4

And I do not want to do these for(int i=0;i<10;i++)thread[i].run(subfrm[i]); for(int i=0;i<10;i++)thread[i].join(); as my loop will be infinite, and so it will increase the overall thread cost, I just want it to wait for next frame.

I am getting the output but the 2 threads are not in sync with each other even, I used the lock so the particular frame will be only divided among the threads.

mut_frames.lock(); block1= std::move(frm(roic1)); block2= std::move(frm(roic2)); mut_frames.unlock();

nobot gravatar imagenobot ( 2019-03-21 13:28:34 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-03-20 05:30:38 -0600

Seen: 511 times

Last updated: Mar 20 '19