I am trying to write my own function for scaling up an input image by using the Nearest-neighbor interpolation algorithm. The bad part is I am able to see how it works but cannot find the algorithm itself. I will be grateful for any help.
Here's what I tried for scaling up the input image by a factor of 2:
function output = nearest(input)
[x,y]=size(input);
output = repmat(uint8(0),x*2,y*2);
[newwidth,newheight]=size(output);
for i=1:y
for j=1:x
xloc = round ((j * (newwidth+1)) / (x+1));
yloc = round ((i * (newheight+1)) / (y+1));
output(xloc,yloc) = input(j,i);
end
end
Here is the output after Mark's suggestion
A while back I went through the code of the
imresize
function in the MATLAB Image Processing Toolbox to create a simplified version for just nearest neighbor interpolation of images. Here's how it would be applied to your problem:Another option would be to use the built-in
interp2
function, although you mentioned not wanting to use built-in functions in one of your comments.EDIT: EXPLANATION
In case anyone is interested, I thought I'd explain how the solution above works...
First, to get the new row and column sizes the old row and column sizes are multiplied by the scale factor. This result is rounded down to the nearest integer with
floor
. If the scale factor is less than 1 you could end up with a weird case of one of the size values being 0, which is why the call tomax
is there to replace anything less than 1 with 1.Next, a new set of indices is computed for both the rows and columns. First, a set of indices for the upsampled image is computed:
1:newSize(...)
. Each image pixel is considered as having a given width, such that pixel 1 spans from 0 to 1, pixel 2 spans from 1 to 2, etc.. The "coordinate" of the pixel is thus treated as the center, which is why 0.5 is subtracted from the indices. These coordinates are then divided by the scale factor to give a set of pixel-center coordinates for the original image, which then have 0.5 added to them and are rounded off to get a set of integer indices for the original image. The call tomin
ensures that none of these indices are larger than the original image sizeoldSize(...)
.Finally, the new upsampled image is created by simply indexing into the original image.
MATLAB has already done it for you. Use imresize:
or if you want to scale both x & y equally,
This answer is more explanatory than trying to be concise and efficient. I think gnovice's solution is best in that regard. In case you are trying to understand how it works, keep reading...
Now the problem with your code is that you are mapping locations from the input image to the output image, which is why you are getting the spotty output. Consider an example where input image is all white and output initialized to black, we get the following:
What you should be doing is the opposite (from output to input). To illustrate, consider the following notation:
The idea is that for each location
(i,j)
in the output image, we want to map it to the "nearest" location in the input image coordinates. Since this is a simple mapping we use the formula that maps a givenx
toy
(given all the other params):in our case,
x
is thei
/j
coordinate andy
is theii
/jj
coordinate. Therefore substituting for each gives us:Putting pieces together, we get the following code:
You just need a more generalized formula for calculating xloc and yloc.
This assumes your variables have enough range for the multiplication results.