How to using my own code to resize image

조회 수: 9 (최근 30일)
Andy
Andy 2013년 10월 28일
댓글: Andy 2013년 10월 29일
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)

채택된 답변

Image Analyst
Image Analyst 2013년 10월 28일
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
Andy
Andy 2013년 10월 29일
Thank you so much,this problem has tortured me for a week.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by