matrix operation

1 次查看(过去 30 天)
Vinh
Vinh 2012-2-6
Hi,
I am trying to avoid using loop for this operation. Basically, my job is to replace original image individual pixel by average value of a grid size i (any odd number bigger or equal to 3).
My code is somewhat like this:
for n=1:L1(1)
for m=1:L1(2)
h = 1:i;
window = double(InputImage(h+n-1,h+m-1));
output(n,m)= (sum(window(:,j+1)) + sum(window(j+1,:)) - window(j+1,j+1))/(i*2 - 1);
end
end
which was reasonably ok, in which I use arrar/matrix operation at the smallest scoop instead of another loop.
However, I want to improve its speed. I am trying to do something like
n=1:L1(1);
m=1:L1(2);
%create an array to move from one location to the other
h = 1:i;
output(n,m)= sum(InputImage(h+n-1,h+m-1))/(i*2 - 1);
Of course, it couldn't work since h and n do not match the dimensions. Is there some how I can "tell" Matlab that at keep m-vector and n-vector fixed while moving cursor along h-vector to do the average-operator for every single point of the image along n-m-vectors?

回答(1 个)

Andrei Bobrov
Andrei Bobrov 2012-2-6
out = conv2(InputImage,ones(i1)/i1^2,'same')

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by