How to using my own code to resize image
4 ビュー (過去 30 日間)
古いコメントを表示
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)
0 件のコメント
採用された回答
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;
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Image Processing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!