Back Forum Reply New

Mutlimedia & MATLAB

MATLAB is a very useful tool, especially for multimedia use (such as image),
since every image is actually a 2D pixels matrix, notice MATLAB is very good tool
on matrix.

For example, some times we want change the size (pixel size) of an image,
yet, we can do that by Paint, but then we don't understand how does it
actually work, now look at this MATLAB code writen by myself (Kevin),..
it might be good start...

Q: You have an 1024 x 1024 (pixels) image,
you want reduce it to 1/4 (which is 256 x 256 pixels),
this procedure called pixel replication (down-sampling),
show step by step and display the image,
Also, after you reduce to 256 x 256, up-sampling back to 1024x1024
and compare the two, explain the difference.


The following code write by Kevin Tang,
you may used them, but don't copy the entire code,
try to understand the concepts~ thanks~
  1. function change_pixel ( picture_file )

  2. % by Kevin Tang

  3. % (1) Notice: This function shrink the image from 1024 x 1024
  4. % to 256 x 256 then, zoom out again, compare the differnce,
  5. % and is done by pixel replication,
  6. % e.g. from 4->8, from ABCD --> AABBCCDD
  7. %      from 8->4, from ABCDEFGH --> ACEG

  8. % (2) WARRNING: The input image MUST be in pixel of 1024 x 1024
  9. %           otherwise, it will NOT WORK
  10.    
  11. pic = imread(picture_file); %Read the image

  12. % Show the orignal image
  13. figure(1), imshow(pic);
  14. title('Original Picture [1024x1024]');  %Title of image

  15. % Down Sampling to 256 x 256.
  16. % The key point is to operate in both row/column,
  17. % which take 3 pixel out on every 4 pixel, reduce 75% of infromation
  18. % from 1024 --> 256 (factor of 1/4)

  19. shrink_pic=zeros(256,256); %make empty img, set matrix to zeros
  20. for i=1:256 %for loop on both row/column
  21.     for j=1:256
  22.         c= 4*(i-1)+1; %take every "4"th sample.
  23.         d= 4*(j-1)+1;
  24.         shrink_pic(i,j)=pic(c,d); %Image reduce to 1/4
  25.     end
  26. end

  27. % Up Sampling back to 1024 x 1024.
  28. % The key point is to copy the pixel back,
  29. % every one pixel copy three times, and replace nearst one.
  30. % result cause from 256 --> 1024 (factor of 4)
  31. zoom_pic=zeros(256,256);
  32. for i=1:256 % Make the repelica on both row and column from 1 to 4.
  33.     for j=1:256
  34.         c=4*(i-1)+1;
  35.         d=4*(j-1)+1;
  36.         zoom_pic(c:c+3,d:d+3)=shrink_pic(i,j); %Copy 1 pix to other 4.
  37.     end
  38. end
  39.    
  40. % Display the shrink image
  41. figure(2),imshow(shrink_pic,[]); %the open [] ignore zero pix part
  42. title('Shrink Image [256x256]');

  43. % Display the zooming image.
  44. figure(3), imshow(zoom_pic,[]);
  45. title('Zoom Image back [1024x1024]');
  46. end
  47.   
Copy Code
Any question, feel free to asked ^^
Back Forum