How to using my own code to resize image
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Andy
el 28 de Oct. de 2013
Comentada: Andy
el 29 de Oct. de 2013
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 comentarios
Respuesta aceptada
Image Analyst
el 28 de Oct. de 2013
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;
Más respuestas (0)
Ver también
Categorías
Más información sobre Image Processing Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!