KDOXG

redimensionar

Oct 26th, 2020 (edited)
830
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arquivo2 = redimensionar(arquivo1, tipo, num1, num2)
  2.   if !exist(arquivo1) || (tipo != 1 && tipo != 2) || (num1 == 0 || num2 == 0)
  3.     arquivo2 = "";
  4.     return;
  5.   endif
  6.  
  7.   I = imread(arquivo1);
  8.   xsize = round(columns(I)*num1);
  9.   ysize = round(rows(I)*num2);
  10.   tempI = zeros(ysize,xsize,size(I,3));
  11.   tempI = uint8 (tempI);
  12.   maxy = rows(tempI)-1;
  13.   maxx = columns(tempI)-1;
  14.  
  15.   if tipo == 1  #nearest neighbor
  16.     for i = 0:maxy
  17.       real_y = i/num2;
  18.       index_y = ifelse(mod(real_y,1) <= 0.5,floor(real_y),ceil(real_y));
  19.       index_y(index_y >= rows(I)) = index_y-1;
  20.       for j = 0:maxx
  21.         real_x = j/num1;
  22.         index_x = ifelse(mod(real_x,1) <= 0.5,floor(real_x),ceil(real_x));
  23.         index_x(index_x >= columns(I)) = index_x-1;
  24.         tempI(i+1,j+1,:) = I(index_y+1,index_x+1,:);
  25.       endfor
  26.     endfor
  27.   else  #bilinear
  28.     ratio_x = columns(I)/columns(tempI);
  29.     ratio_y = rows(I)/rows(tempI);
  30.     for i = 1:ysize
  31.       real_y = (ratio_y * i) + (0.5 * (1 - 1/num2));
  32.       for j = 1:xsize
  33.         real_x = (ratio_x * j) + (0.5 * (1 - 1/num1));
  34.        
  35.         real_x(real_x < 1) = 1;
  36.         real_x(real_x > rows(I) - 0.001) = rows(I) - 0.001;
  37.         x1 = floor(real_x);
  38.         x2 = x1 + 1;
  39.         real_y(real_y < 1) = 1;
  40.         real_y(real_y > columns(I) - 0.001) = columns(I) - 0.001;
  41.         y1 = floor(real_y);
  42.         y2 = y1 + 1;
  43.        
  44.         prox1 = I(y1,x1,:);
  45.         prox2 = I(y1,x2,:);
  46.         prox3 = I(y2,x1,:);
  47.         prox4 = I(y2,x2,:);
  48.        
  49.         a1 = (y2-real_y)*(x2-real_x);
  50.         a2 = (y2-real_y)*(real_x-x1);
  51.         a3 = (x2-real_x)*(real_y-y1);
  52.         a4 = (real_y-y1)*(real_x-x1);
  53.        
  54.         tempI(i,j,:) = a1 * prox1 + a2 * prox2 + a3 * prox3 + a4 * prox4;
  55.       endfor
  56.     endfor
  57.   endif
  58.   imwrite(tempI,"output.png");
  59.   arquivo2 = "output.png";
  60.   return;
  61. endfunction
  62.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×