0

The function ProcessFrames takes in one quadrant of the image, and applies a Canny Filter to it. But the thread created does not give out a Canny detected image. instead, I get the color quadrant of the image in imshow().

void ProcessFrames(Mat &image)
{
    Mat test = image;
    Canny(test, image, 5, 60, 3);
}

void main()
{
    Mat frame;
    Mat top_left, top_right, bot_left, bot_right;
    String filename = "C:\\Users\\operator\\Downloads\\MODFAM1080I.mpg";
    VideoCapture capture(filename);
    if (!capture.isOpened())
        throw "Error when reading video file!!";
    while (1)
    {
        capture >> frame;
        if (frame.empty())
        break;
        top_left = frame(Range(0, frame.rows / 2 - 1), Range(0, frame.cols / 2 - 1));
        top_right = frame(Range(0, frame.rows / 2 - 1), Range(frame.cols / 2, frame.cols - 1));
        bot_left = frame(Range(frame.rows / 2, frame.rows - 1), Range(0, frame.cols / 2 - 1));
        bot_right = frame(Range(frame.rows / 2, frame.rows - 1), Range(frame.cols / 2, frame.cols - 1));
        //Cropping the image into four quadrants to process it separately. 
        thread t1(ProcessFrames,top_left); //invoking a thread and passing first quadrant.
        t1.join(); //joing the thread with the main function
        imshow("Canny", top_left); // still shows color image of the first quadrant. Canny not reflected.
        cvWaitKey(10);
    }
}
Siddharth
  • 1
  • 5
  • You should read about [std::async](http://en.cppreference.com/w/cpp/thread/async) if you are just trying to run functions asynchronously. – François Andrieux Jun 19 '17 at 15:12
  • Why do you create a thread at all if you join it immediately after? – Aconcagua Jun 19 '17 at 15:13
  • Are you using MSVS? – NathanOliver Jun 19 '17 at 15:13
  • Duplicate? https://stackoverflow.com/questions/21048906/stdthread-pass-by-reference-calls-copy-constructor `std::thread` takes it's arguments by value, even if the function takes them by reference. – François Andrieux Jun 19 '17 at 15:13
  • 1
    @FrançoisAndrieux It doesn't take them by value. Unfortunately what it does do is copy them into the state of the thread. – NathanOliver Jun 19 '17 at 15:15
  • @Aconcagua I just want to test it for a single quadrant and then repeat the same process for the other parts of the picture. – Siddharth Jun 19 '17 at 15:37
  • @FrançoisAndrieux Thanks for the tip! I didn't know that threads just take a copy of the parameter rather than pass it by reference. – Siddharth Jun 19 '17 at 15:38
  • The only thing the thread achieves in this piece of code is adding some overhead (along with some opportunities to shoot yourself in the foot, I suppose). Usually the intent is to be able to do multiple things at the same time. All your main thread does is sit there waiting, while the worker thread does the calculations. | See [this answer](https://stackoverflow.com/a/37146523/3962537) for some inspiration. – Dan Mašek Jun 19 '17 at 16:00

1 Answers1

2

std::thread constructor copies everything. If you want to pass data pointer and this data to be changed in the thread you must wrap it with std::ref().

Petar Velev
  • 2,305
  • 12
  • 24