Chr*_*ris 2 c++ opencv image-processing
我有一个非常大的 Mat,它实际上是另一个 Mat 的 ROI(通过 otherMat(cv::Rect(x,y,w,h)) 获得)。我想遍历 Mat 的所有像素,进行一些像素计算并使用指针将结果写入另一个 Mat。
到目前为止,遍历所有像素(包括 ROI 之外的像素)工作正常,但我想知道跳过 ROI 之外的像素的最快方法是什么。我希望缓存未命中尽可能少,而且我也不希望出现低效的分支预测。解决这个问题的最佳方法是什么?
编辑:我对获取特定感兴趣区域的子矩阵不感兴趣。我感兴趣的是以最有效的方式通过指针迭代像素,而无需访问子矩阵区域之外的数据。
只需使用子矩阵:
cv::Mat largeMat
cv::Rect roi(yourROI);
cv::Mat submatrix = largeMat(roi);
// now iterate over all the pixels of submatrix
Run Code Online (Sandbox Code Playgroud)
每行末尾都会有缓存未命中
这是实际的代码示例,显示子垫外部的像素被跳过(您将在每行末尾获得额外的缓存未命中,但这应该是全部)。
int main(int argc, char* argv[])
{
cv::Mat input = cv::imread("C:/StackOverflow/Input/Lenna.png");
cv::Rect roi(128, 128, 256, 256);
cv::Mat submat = input(roi);
cv::MatIterator_<cv::Vec3b> it; // = src_it.begin<cv::Vec3b>();
for (it = submat.begin<cv::Vec3b>(); it != submat.end<cv::Vec3b>(); ++it)
{
(*it)[0] = 0;
(*it)[1] = 0;
}
cv::imshow("input", input);
cv::imwrite("C:/StackOverflow/Output/submatIter.png", input);
cv::waitKey(0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给出这个结果:
如果你想要快一点,你可以使用行指针:http://docs.opencv.org/2.4/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html
请注意,在链接中他们比较了调试模式运行速度,这就是随机访问如此慢的原因。在发布模式下,它应该与迭代器版本一样快(或者可能更快)。但这里是 row-Ptr 版本(无需计算每个像素访问的行偏移),它给出相同的结果,并且应该是最快的方法(如果 openCV 的 LUT 函数不能用于您的任务):
int main(int argc, char* argv[])
{
cv::Mat input = cv::imread("C:/StackOverflow/Input/Lenna.png");
cv::Rect roi(128, 128, 256, 256);
cv::Mat submat = input(roi);
cv::Vec3b * currentRow;
for (int j = 0; j < submat.rows; ++j)
{
currentRow = submat.ptr<cv::Vec3b>(j);
for (int i = 0; i < submat.cols; ++i)
{
currentRow[i][0] = 0;
currentRow[i][1] = 0;
}
}
cv::imshow("input", input);
cv::imwrite("C:/StackOverflow/Output/submatIter.png", input);
cv::waitKey(0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)