imageresize.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. %====================================================%
  2. %===========ECEN 5793: DIP===========================%
  3. %===========PROJECT 1================================%
  4. %====================================================%
  5. function A3 = imageresize(B3,p,m)
  6. % Zoom or shrink a image
  7. % Input:image B3: gray-image or RGB image
  8. % p: ratio, zooming (p>1), shrink(p<1)
  9. % m: mode; =0:(default) nearest neighbor interpolation
  10. % =1: Bilinear interpolation
  11. % =2: Bicubic interpolation
  12. % Output: image A3
  13. % @Trung Duong, trungd@okstate.edu
  14. % Jan 25, 2009
  15. %====================================================%
  16. % Input checking and default values
  17. error(nargchk(2, 3, nargin, 'struct'));
  18. if nargin < 3, m = 0; end
  19. dimB = length(size(B3));
  20. if dimB== 2 % GRAY-IMAGE INPUT:
  21. A3 = gray_resize(B3,p,m);
  22. elseif dimB== 3 % COLOR RGB-IMAGE INPUT:
  23. AR = gray_resize(B3(:,:,1),p,m);
  24. AG = gray_resize(B3(:,:,2),p,m);
  25. AB = gray_resize(B3(:,:,3),p,m);
  26. A3 = zeros([size(AR) 3]);
  27. A3(:,:,1) = AR;
  28. A3(:,:,2) = AG;
  29. A3(:,:,3) = AB;
  30. else
  31. error('Improper input image');
  32. end
  33. %**************************************************%
  34. %==================================================%
  35. % Sub-Function: Resize a gray-image
  36. % Same input argument with imageresize()
  37. function A = gray_resize(B,p,m)
  38. % Initialize new-grid and output
  39. [N,M] = size(B);
  40. xp = 1:1/p:N+1/p; yp = 1:1/p:M+1/p;
  41. A = zeros(length(xp),length(yp));
  42. % Symmetric Padding
  43. npad = 3;
  44. B = sym_pad(B,npad);
  45. switch m
  46. case 0 % Nearest neighbor interpolation
  47. U = round(xp); V = round(yp);
  48. U(find(U<1)) = 1; V(find(V<1)) = 1;
  49. U(find(U>N)) = N; V(find(V>M)) = M;
  50. A = B(U+npad,V+npad);
  51. case 1 % Bilinear interpolation
  52. % Floor of (xp,yp)
  53. xf = floor(xp); yf = floor(yp);
  54. % Distance to top-left neighbors
  55. [XF,YF] = ndgrid(xf,yf);
  56. [XP,YP] = ndgrid(xp,yp);
  57. u = XP - XF; v = YP - YF;
  58. % Change xf, yf for new padding image
  59. xf = xf + npad; yf = yf + npad;
  60. % Interpolation
  61. A = (1-u).*(1-v).*B(xf,yf) + ...
  62. (1-u).*v .*B(xf,yf+1) + ...
  63. u .*(1-v) .*B(xf+1,yf) + ...
  64. u .*v .*B(xf+1,yf+1);
  65. case 2
  66. % Floor of (xp,yp)
  67. xf = floor(xp);yf = floor(yp);
  68. % Distance to top-left neighbors
  69. [XF,YF] = ndgrid(xf,yf);
  70. [XP,YP] = ndgrid(xp,yp);
  71. u = XP - XF; v = YP - YF;
  72. % Change xf, yf for new padding image
  73. xf = xf + npad; yf = yf + npad;
  74. % Interpolation: 16 neighbors
  75. for i = -1:2
  76. for j = -1:2
  77. if i==-1, sgi = -1; else sgi = 1; end
  78. if j==-1, sgj = -1; else sgj = 1; end
  79. A = A + mex_hat(sgi*(i-u)).*mex_hat(sgj*(j-v)).*B(xf+i,yf+j);
  80. end
  81. end
  82. otherwise
  83. error('Undefined Interpolation method');
  84. end
  85. %**************************************************%
  86. %==================================================%
  87. % Sub-Function: Mexican-hat kernel
  88. function hx = mex_hat(x)
  89. hx = zeros(size(x));
  90. x = abs(x);
  91. ind1 = find(x<=1); ind2 = find(x>1 & x<=2);
  92. hx(ind1) = 1 - 2*x(ind1).^2 + x(ind1).^3;
  93. hx(ind2) = 4 - 8*x(ind2) + 5*x(ind2).^2 - x(ind2).^3;
  94. % END of sub-function
  95. %==================================================%
  96. % Sub-Function: symmetric padding, by default 2 pixels
  97. % Input: gray image
  98. function Bp = sym_pad(B,n)
  99. Bp = zeros(size(B)+2*n);
  100. Bp(n+1:end-n,n+1:end-n) = B;
  101. % Padding symmetrically 4 boundaries
  102. Bp(n:-1:1,n+1:end-n) = B(1:n,:);
  103. Bp(n+1:end-n,n:-1:1) = B(:,1:n);
  104. Bp(end-n+1:end,n+1:end-n) = B(end:-1:end-n+1,:);
  105. Bp(n+1:end-n,end-n+1:end) = B(:,end:-1:end-n+1);
  106. % END of sub-function
  107. %==================================================%