Skip to content

Instantly share code, notes, and snippets.

@barisdemiroz
Created April 4, 2016 12:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barisdemiroz/3d916c0371acaee5a54c77c2d0f415d8 to your computer and use it in GitHub Desktop.
Save barisdemiroz/3d916c0371acaee5a54c77c2d0f415d8 to your computer and use it in GitHub Desktop.
OpenCV parallel_for_() example.
/*
* parallel_for_() example.
* Paints 8 stripes to different colors in a 80x80 image in parallel.
* @OpenCVTip on Twitter
**/
#include <opencv2/opencv.hpp>
using namespace cv;
class RandomFillBody : public ParallelLoopBody
{
Mat3b img;
public:
RandomFillBody(Mat3b& img) : img(img)
{ }
void operator()(const Range& range) const override
{
theRNG().state = getTickCount();
Vec3b color{ theRNG(), theRNG(), theRNG() };
for (int i = range.start; i < range.end; ++i)
{
int x = i % 8, y = i / 8;
img(Range{ y * 10, (y + 1) * 10 }, Range{ x * 10, (x + 1) * 10 }) = color;
}
}
};
int main()
{
Mat3b img = Mat3b::zeros(80,80);
parallel_for_(Range{ 0, 64 }, RandomFillBody{ img }, 8);
imshow("img", img);
waitKey();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment