Ask Your Question
0

Opencv and TBB in image processing

asked 2015-10-07 17:14:14 -0600

anfedres.86 gravatar image

Hello everybody. I'd like to know if there is any example of TBB and Opencv. I'm hoping to run, let's say applying two filters to the same image, and each of the filters running in different cores? Is it possible?

Thank you.

edit retag flag offensive close merge delete

Comments

Parallel processing is used very offen in OpenCV particulary in filter processing. If you want to use TBB for filtering it's always used in different way. Image is divided in strip and multi-core is used for each strip.

I think If you want to use TBB for each image you have to disable TBB in Opencv and activate TBB in your program.

There is no example for TBB because TBB is encapsulated like OpenMP COncurrency cstripes and GCD.

(what is GCD and CStripes?)

LBerger gravatar imageLBerger ( 2015-10-08 01:51:21 -0600 )edit

There is an interesting article "The Foundations for Scalable Multi-core Software in Intel® Threading Building Blocks" http://citeseerx.ist.psu.edu/viewdoc/... with fibonacci number example similar as is in example code within TBB package. See page 54. It's long article but contains precious examples, explaining, diagrams and benchmarks. Link to LBerger's note: http://answers.opencv.org/question/15...

VanGog gravatar imageVanGog ( 2016-06-28 05:45:46 -0600 )edit

2 answers

Sort by » oldest newest most voted
3

answered 2015-10-08 03:29:16 -0600

Yes, you can do like this:

// A very raw example of using tbb's thread
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/video/video.hpp>
#include <opencv2/highgui.hpp>
#include <tbb/tbb.h>
#include <iostream>

using namespace std;
using namespace cv;
using namespace tbb;

int main(int argc, char* argv[])
{
    Mat im1 = imread(argv[1]);
    Mat imGray;
    if (im1.data == nullptr)
    {
        cout << "Error while reading file " << argv[1];
        return 1;
    }
    imshow("Input image", im1);
    cvtColor(im1, imGray, CV_RGB2GRAY);
    Mat im3, im4;
    tbb_thread th1([&imGray, &im3]() // in fact, you can do this with C++ thread
    {
        int windowSize = 5; // starting threshold value
        int constant = 5; // starting constant value

        adaptiveThreshold(imGray, im3, 255,
            CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,
            windowSize, constant);
    });
    tbb_thread th2([&imGray, &im4]()
    {
        cv::GaussianBlur(imGray, im4, cv::Size(3, 3), 5.0f);
    });
    th1.join();
    th2.join();

    imshow("Grayscale image", imGray);
    imshow("Adaptive thresholding", im3);
    imshow("Gaussian blur", im4);
    cvWaitKey(0);
    return 0;
}
edit flag offensive delete link more

Comments

how many threads have you got by core?

LBerger gravatar imageLBerger ( 2015-10-08 03:51:06 -0600 )edit
2

In fact, my machine CPU has only 2 cores. But if you want to set number of threads explicitly, the following statement will help:

int TBB_THREADS = 3;
task_scheduler_init init(TBB_THREADS);
tuannhtn gravatar imagetuannhtn ( 2015-10-08 04:08:14 -0600 )edit

As gaussianBlur use thread too I think you will have 2*getNumthreads() threads when you th2.join

It's speed is not important parameter (like sometimes in GUI where user interaction is important) that's not a problem.

LBerger gravatar imageLBerger ( 2015-10-08 04:34:21 -0600 )edit
2

@LBerger: I just want to illustrate how to exploit the multi-cores machine to do multiple tasks, and by a very raw way, since that is the question's aim.

tuannhtn gravatar imagetuannhtn ( 2015-10-08 05:24:50 -0600 )edit

Thanks for your answer, it's great to see example. But how can you detect that some thread has finished its job and should process next image?

VanGog gravatar imageVanGog ( 2016-06-27 01:38:15 -0600 )edit

AFAIK, all the statements after the join() call can deal with results obtained from previous threads (whose join() were called). So if you fed image (s) to one thread to process, after its join() finishes (join() statement in the program), you can be sure to cope with other images (or the ones produced from the thread).

tuannhtn gravatar imagetuannhtn ( 2016-06-27 05:22:45 -0600 )edit

What I look for is to create a loop with a listener function which will obtain a signal when some thread function have finished. I cannot imagine how it works. There should be some feeding function running in a loop in memory, which will create queue when it is needed or will pass the images from queue to the thread function. I just cannot imagine how to manage the feeding process. I will create question for this tomorrow because I just compiled OpenCV with TBB and I am completely new to this. So I first need to try your code and then I will ask.

VanGog gravatar imageVanGog ( 2016-06-27 14:43:40 -0600 )edit
0

answered 2017-03-28 06:26:31 -0600

sagiz gravatar image

You can use the TBB parallel pipeline with OpenCV to do just that.

I have a post about using it with OpenCV here - https://www.theimpossiblecode.com/blo...

TBB has advanced multi threading patterns and tools - some of them used internally by OpenCV (if built with it), and some can be used externally, regardless if OpenCV was built with TBB support.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2015-10-07 17:14:14 -0600

Seen: 6,828 times

Last updated: Oct 08 '15