function nimg = nma_zoom_image(img,k) %zoom a gray image by factor and return new image %EXAMPLE USE: %--------------------------- %X= imread('logo.tif'); %size(X) % %ans = % 107 122 % %nimg = nma_zoom_image(X,4); %size(nimg) % %ans = % 428 488 %-------------------------- %INPUT: % img: 2D image only (single channel) % k: an integer to zoom img with % %OUTPUT: % nimg: image which is k times as large as img %----------------------------- %Algorithm: nearest neighbor The nearest neighbor method where the value %of each new pixel added to the new image is obtained from the value of %the pixel in the original image by finding the closest pixel in the %original image to the new pixel in the new image. % %ISSUES: %no error checking on img data % %copy rights Nasser M. Abbasi %free to use provided copyright notice is kept in place % if not(isnumeric(k)) error('zoom factor must be numeric'); end if round(k) ~= k error('zoom factor must be integer'); end if k <= 1 error('zoom factor must be larger than one'); end if not(ismatrix(img)) error('image must be matrix'); end [M,N] = size(img); newM = M*k; newN = N*k; nimg = zeros(newM,newN); deltay = (M-1)/(newM-1); deltax = (N-1)/(newN-1); for i = 1:newM y = round(i*deltay)+1; for j = 1:newN x = round(j*deltax)+1; nimg(i,j) = img(y,x); end end end