How to using my own code to resize image

teacher assigned a home work to resize the image by computing the average of 4 neighbouring pixels to get one pixel. following are my code, but it is wrong,and I cant figure out why.Here are images before and after reszing, the size changed as well as color imageshack.us/img834/2379/tb7w.jpg I prefer to point out my mistake directly, rather than give me a new code. And I have another question, sometimes my matrix of image is in double type, and it's works, but sometimes it's not, and I need to change it to uint8 type, am I correct? Thanks
a=imread('c:\black and white.jpg');
[rows,columns,layers]=size(a);
i=1;j=1;k=1;
c=zeros(rows/2,columns/2,layers);
c=uint8(c);
for x=1:2:rows-1;
for y=1:2:columns-1;
for z=1:layers;
c(i,j,k)=1/4*(a(x,y,z)+a(x,y+1,z)+a(x+1,y,z)+a(x+1,y+1,z));
k=k+1;
end
j=j+1;
k=1;
end
i=i+1;
j=1;
k=1;
end
figure, imshow(a)
figure, imshow(c)

 Akzeptierte Antwort

Image Analyst
Image Analyst am 28 Okt. 2013

1 Stimme

If you add uint8 images and the sum goes past 255, it will clip to 255, so that's why your colors look funny. You can cast to double, then cast back to uint8 after the division by 4.
a=imread('peppers.png');
[rows,columns,layers]=size(a)
i=1;j=1;k=1;
c=zeros(rows/2,columns/2,layers);
c=uint8(c);
figure
imshow(a) % Display BEFORE casting to double.
a = double(a);
for x=1:2:rows-1;
for y=1:2:columns-1;
for z=1:layers;
c(i,j,k)=1/4*(a(x,y,z)+a(x,y+1,z)+a(x+1,y,z)+a(x+1,y+1,z));
k=k+1;
end
j=j+1;
k=1;
end
i=i+1;
j=1;
k=1;
end
axis on;
figure,
imshow(c)
axis on;

1 Kommentar

Andy
Andy am 29 Okt. 2013
Thank you so much,this problem has tortured me for a week.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Gefragt:

am 28 Okt. 2013

Kommentiert:

am 29 Okt. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by