我有以下代码,我尝试使用cv::Mat
成员方法利用对OpenCV forEach()
类的元素的多线程访问。但是当我在valgrind --tool=helgrind
中运行此代码时,我会收到多个竞争条件报告。它也不能按预期工作,即用另一个图像掩盖图像。这种行为的原因是什么?
#include <opencv2/opencv.hpp>
int main(int argc, char** argv)
{
cv::Mat1f im(21, 21, 0.0f);
im(cv::Rect(7, 7, 7, 7)) = 0.5;
cv::Mat1b mask(21, 21, 0.0);
mask(cv::Rect(9, 9, 3, 3)) = 1;
cv::Mat1f result(21, 21, 0.0f);
mask.forEach([&result, &im] (uchar val, const int* pos)
{
if (val == 0)
result(pos[1], pos[0]) = 0;
else
result(pos[1], pos[0]) = im(pos[1], pos[0]);
});
return 0;
}