ForEach and parallel_for_

asked 2016-10-26 07:21:52 -0600

LBerger gravatar image

updated 2016-10-26 07:22:36 -0600

Hi,

I want to understand how works is split between thread. I wrote a little program which write in each pixel thread number :

#include <opencv2/opencv.hpp> 
using namespace std;
using namespace cv;

class FiltreLigneParallel : public ParallelLoopBody
{

private:
    bool verbose;
    Mat imgSrc;

public:
    FiltreLigneParallel(Mat& img) :
        imgSrc(img),
        verbose(false)
    {}
    void Verbose(bool b) { verbose = b; }
    virtual void operator()(const Range& range) const
    {

        int tNum = getThreadNum()*16; 
        if (verbose)
            cout << getThreadNum() << "# :Start from row " << range.start << " to " << range.end - 1 << " (" << range.end - range.start << " loops)" << endl;
        for (int y = range.start; y < range.end; y++)
        {
            uchar *vDst = (uchar*)imgSrc.ptr(y);
            for (int x = 0; x < imgSrc.cols; x++, vDst++) 
                *vDst= static_cast<uchar>(tNum);
        }

    }
    FiltreLigneParallel& operator=(const FiltreLigneParallel &) {
        return *this;
    };
};

class FiltreLigne {
   uchar *pData;
public:
    FiltreLigne(uchar *p) {  pData=p;};
    void operator ()(uchar &pixel, const int * position) const {
        int tNum = getThreadNum()*16;
        pixel = static_cast<uchar>(tNum);;
    }
};

void main(void)
{
    Mat img(512,512,CV_8UC1,Scalar(255));
    img.forEach<uchar>(FiltreLigne(img.ptr(0)));
    imwrite("forEach.png",img); 
    Mat img2(512, 512, CV_8UC1, Scalar(255));
    FiltreLigneParallel x(img2);
    parallel_for_(Range(0, img2.rows), x, getNumThreads());
    imwrite("parallel_for.png", img2);
    waitKey();
}

Results are : forEach image description and parallel_for image description

Why foreach.png is not regular like parallel_for_?

Configuration windows 10, VS 2015, and concurrency for thread

edit retag flag offensive close merge delete