【发布时间】:2016-02-10 09:26:10
【问题描述】:
我正在计算 C++ 中图像内多个平方区域的平均值。因此,我在图像上移动了一个平方区域并使用 openCV“均值”函数计算均值,但将其替换为 std。平均计算(见下文),它出乎意料地更快。然而,在 Android 设备上它需要大约 8 毫秒,因为平均计算被调用了大约 400 次(每次平均计算需要大约 0.025 毫秒)
uchar rectSize = 10;
Rect roi(0,0,rectSize, rectSize);
int pxNumber = rectSize * rectSize;
uchar value;
//Shifting the region to the bottom
for(uchar y=0; y<NumberOfRectangles_Y; y++)
{
p = outBitMat.ptr<uchar>(y);
roi.x = rectSize;
//Shifting the region to the right
for(uchar x=0; x<NumberOfRectangles_X; x++, ++p)
{
meanCalc(normalized(roi),rectSize, pxNumber, value);
roi.x += rectSize;
}
roi.y += rectSize;
}
void meanCalc(const cv::Mat& normalized, uchar& rectSize, int& pxNumber, uchar& value)
{
for(uchar y=0; y < rectSize; y++)
{
p = normalized.ptr<uchar>(y);
for(uchar x=0; x < rectSize; x++, ++p)
{
sum += *p;
}
}
value = sum / (float)pxNumber;
}
有没有办法加快图像内每个矩形窗口的平均计算速度?我可以进行某种预像素排序以仅计算一次平均值并更快吗?
提前致谢
更新
根据用户 6502 的回答和总和表的使用,我得出以下结论:
Mat tab;
integral(image,tab);
int* input = (int*)(tab.data);
value = (input[yStart*tabWidth + xStart] + input[(yStart+rectSize)*tabWidth + xStart+rectSize]
- input[yStart*tabWidth + xStart+rectSize] - input[(yStart+rectSize)*tabWidth + xStart]) / (double)pxNumber;
因此这个函数需要几乎相同的时间。是不是只有在计算大量重叠区域时,总和表才有用。因为在我的情况下,我只触摸每个像素进行一次计算。
【问题讨论】:
-
你应该看看summed area tables
-
既然你标记了OpenCV,你可以使用cv::integral
标签: c++ performance opencv mean