图像中的移动像素统计
Moving Pixel Statistics in an Image
我想使用大小为 3x3
像素的滑动 window 以逐个像素的方式遍历图像,并在滑动的每个位置 window我想计算最小值、最大值、平均值和标准差 的像素值。
你能告诉我如何完成这个吗?另外,执行此操作的最快方法是什么?
非常感谢!
您应该尽可能使用内置函数。
nlfilter
is recommended for sliding window operations. colfilt
是相同的,但通常具有更好的内存局部性,您应该使用它。
neigh = [3,3];
I_mean = colfilt(I, neigh, 'sliding', @mean);
I_max = colfilt(I, neigh, 'sliding', @max);
I_min = colfilt(I, neigh, 'sliding', @min);
可以使用 stdfilt
计算标准偏差。 colfilt(... @std)
由于某种原因需要数据类型转换,并且在我的机器上慢了 ~4 倍。
I_std = stdfilt(I);
Returns 使用 3x3 滑动 window 制作的标准偏差图像。
如果通过公平比较,您的意思是比较速度,请注意 colfilt
和 stdfilt
是完全不同的。
I_std = colfilt(double(I), neigh, 'sliding', @std);
您还可以通过imfilter
计算平均图像。快了一个数量级,但边界像素输出有点不同
tic;
meanh = fspecial('average', neigh);
I_mean = imfilter(I, meanh);
toc
Elapsed time is 0.024311 seconds.
对比
tic;
I_mean2 = colfilt(I, neigh, 'sliding', @mean);
toc
Elapsed time is 0.649545 seconds.
这是差异的说明 (double(I_mean)-double(I_mean2)
)。只有边框像素不同:
邻域越大速度差异越大
我想使用大小为 3x3
像素的滑动 window 以逐个像素的方式遍历图像,并在滑动的每个位置 window我想计算最小值、最大值、平均值和标准差 的像素值。
你能告诉我如何完成这个吗?另外,执行此操作的最快方法是什么?
非常感谢!
您应该尽可能使用内置函数。
nlfilter
is recommended for sliding window operations. colfilt
是相同的,但通常具有更好的内存局部性,您应该使用它。
neigh = [3,3];
I_mean = colfilt(I, neigh, 'sliding', @mean);
I_max = colfilt(I, neigh, 'sliding', @max);
I_min = colfilt(I, neigh, 'sliding', @min);
可以使用 stdfilt
计算标准偏差。 colfilt(... @std)
由于某种原因需要数据类型转换,并且在我的机器上慢了 ~4 倍。
I_std = stdfilt(I);
Returns 使用 3x3 滑动 window 制作的标准偏差图像。
如果通过公平比较,您的意思是比较速度,请注意 colfilt
和 stdfilt
是完全不同的。
I_std = colfilt(double(I), neigh, 'sliding', @std);
您还可以通过imfilter
计算平均图像。快了一个数量级,但边界像素输出有点不同
tic;
meanh = fspecial('average', neigh);
I_mean = imfilter(I, meanh);
toc
Elapsed time is 0.024311 seconds.
对比
tic;
I_mean2 = colfilt(I, neigh, 'sliding', @mean);
toc
Elapsed time is 0.649545 seconds.
这是差异的说明 (double(I_mean)-double(I_mean2)
)。只有边框像素不同:
邻域越大速度差异越大