Replace picture's each 2*2 pixel values into their average value

Hi, I have a problem. I want to go with a 2*2 pixels moving window on a picture and take a snapshot then replace these 2*2 pixels' values with their average value -> move to next 2*2 until the whole picture is replaced. Can you guys write a brief code for me? Thx!!

Antworten (2)

Image Analyst
Image Analyst am 26 Jan. 2013
Bearbeitet: Image Analyst am 27 Jan. 2013
blurredImage = conv2(double(grayImage), [.25,.25;.25,.25], 'same');
hi Jackson, This technique however does not produce good filtering, but here is standard code :
=================================================================
function ZZ=averaging_2x2(I)
ZZ=zeros(size(I));
I=im2double(I);
M=size(I);
for x=1:M(1)-1
for y=1:M(2)-1
ZZ(x,y)=I(x,y)+I(x,y+1)+I(x+1,y)+I(x+1,y+1);
ZZ(x,y)=ZZ(x,y)/4;
end
end
========================================================================
Note that this code does not give the same result as that given by Image Analyst , lets take an example :
===============================================
I=im2double(imread('circuit.tif'));
I=imnoise(I,'Gaussian');
X1=averaging_2x2(I);
X2=conv2(double(I),[.5,.5;.5,.5], 'same');
subplot(121), imshow(X1), subplot(122), imshow(X2) =================================================
try to manipulate the two codes and conclude.
KH.Youssef

1 Kommentar

I mistakenly had 0.5 as the kernel elements initially, instead of 1/4. If you change to 0.25 they give the same values. conv2() is a lot more efficient, more so as the window size gets larger. You could also use imfilter().

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 26 Jan. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by