Guest User

Untitled

a guest
Apr 26th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. function output = nearest(input)
  2. [x,y]=size(input);
  3. output = repmat(uint8(0),x*2,y*2);
  4. [newwidth,newheight]=size(output);
  5. for i=1:y
  6. for j=1:x
  7. xloc = round ((j * (newwidth+1)) / (x+1));
  8. yloc = round ((i * (newheight+1)) / (y+1));
  9. output(xloc,yloc) = input(j,i);
  10. end
  11. end
  12.  
  13. %# Initializations:
  14.  
  15. scale = [2 2]; %# The resolution scale factors: [rows columns]
  16. oldSize = size(inputImage); %# Get the size of your image
  17. newSize = max(floor(scale.*oldSize(1:2)),1); %# Compute the new image size
  18.  
  19. %# Compute an upsampled set of indices:
  20.  
  21. rowIndex = min(round(((1:newSize(1))-0.5)./scale(1)+0.5),oldSize(1));
  22. colIndex = min(round(((1:newSize(2))-0.5)./scale(2)+0.5),oldSize(2));
  23.  
  24. %# Index old image to get new image:
  25.  
  26. outputImage = inputImage(rowIndex,colIndex,:);
  27.  
  28. newSize = max(floor(scale.*oldSize(1:2)),1);
  29.  
  30. rowIndex = min(round(((1:newSize(1))-0.5)./scale(1)+0.5),oldSize(1));
  31. colIndex = min(round(((1:newSize(2))-0.5)./scale(2)+0.5),oldSize(2));
  32.  
  33. outputImage = inputImage(rowIndex,colIndex,:);
  34.  
  35. 1 c 1 scaleC*c
  36. +-----------+ 1 +----------------------+ 1
  37. | | | | | |
  38. |----o | <=== | | |
  39. | (ii,jj) | |--------o |
  40. +-----------+ r | (i,j) |
  41. inputImage | |
  42. | |
  43. +----------------------+ scaleR*r
  44. ouputImage
  45.  
  46. Note: I am using matrix notation (row/col), so:
  47. i ranges on [1,scaleR*r] , and j on [1,scaleC*c]
  48. and ii on [1,r], jj on [1,c]
  49.  
  50. x-minX y-minY
  51. --------- = ---------
  52. maxX-minX maxY-minY
  53.  
  54. jj = (j-1)*(c-1)/(scaleC*c-1) + 1
  55. ii = (i-1)*(r-1)/(scaleR*r-1) + 1
  56.  
  57. % read a sample image
  58. inputI = imread('coins.png');
  59. [r c] = size(inputI);
  60. scale = [2 2]; % you could scale each dimension differently
  61.  
  62. outputI = zeros(scale(1)*r,scale(2)*c, class(inputI));
  63.  
  64. for i=1:scale(1)*r
  65. for j=1:scale(2)*c
  66. % map from output image location to input image location
  67. ii = round( (i-1)*(r-1)/(scale(1)*r-1)+1 );
  68. jj = round( (j-1)*(c-1)/(scale(2)*c-1)+1 );
  69.  
  70. % assign value
  71. outputI(i,j) = inputI(ii,jj);
  72. end
  73. end
  74. figure(1), imshow(inputI)
  75. figure(2), imshow(outputI)
  76.  
  77. output = imresize(input,size(input)*2,'nearest');
  78.  
  79. output = imresize(input,2,'nearest');
  80.  
  81. xloc = (j * (newwidth+1)) / (x+1);
  82. yloc = (i * (newheight+1)) / (y+1);
Add Comment
Please, Sign In to add comment