Created
April 4, 2016 12:54
-
-
Save barisdemiroz/3d916c0371acaee5a54c77c2d0f415d8 to your computer and use it in GitHub Desktop.
OpenCV parallel_for_() example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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