Matlab 如何在基于移动窗口的操作中减少for循环?

Matlab 如何在基于移动窗口的操作中减少for循环?,matlab,image-processing,Matlab,Image Processing,如何在基于移动窗口的操作中减少for循环?我在两幅图像上使用一个15x15的窗口,并执行乘法以获得每像素的平均值 我可能在你的循环中迷失了方向,我无法准确地阅读公式(有点模糊),但我认为这就是你想要的: g = rand(5); %sample img1 edg = rand(5); %sample img2 windowsize = 3; %set this to 15 for real images A = g.*edg; % multiply each element beforeha

如何在基于移动窗口的操作中减少for循环?我在两幅图像上使用一个15x15的窗口,并执行乘法以获得每像素的平均值


我可能在你的循环中迷失了方向,我无法准确地阅读公式(有点模糊),但我认为这就是你想要的:

g = rand(5); %sample img1
edg = rand(5); %sample img2
windowsize = 3; %set this to 15 for real images

A = g.*edg; % multiply each element beforehand, corresponds to mu*sigma in formula
B = movsum(movsum(A,windowsize,2),windowsize,1); % get moving window sum of A, corresponds to numerator in formula
C = movsum(movsum(edg,windowsize,2),windowsize,1); % get moving window sum of edg, corresponds to denominator in formula
E = B./C; %hopefully what you wanted
Ps:您需要2016a或更新版本才能运行此功能

g = rand(5); %sample img1
edg = rand(5); %sample img2
windowsize = 3; %set this to 15 for real images

A = g.*edg; % multiply each element beforehand, corresponds to mu*sigma in formula
B = movsum(movsum(A,windowsize,2),windowsize,1); % get moving window sum of A, corresponds to numerator in formula
C = movsum(movsum(edg,windowsize,2),windowsize,1); % get moving window sum of edg, corresponds to denominator in formula
E = B./C; %hopefully what you wanted