

NewSize = max(floor(scale.*oldSize(1:2)),1) %# Compute the new image size

OldSize = size(inputImage) %# Get the size of your image Scale = %# The resolution scale factors: Here’s how it would be applied to your problem: %# Initializations:
#NEAREST NEIGHBOR MATLAB 2012 CODE#
Jj = round( (j-1)*(c-1)/(scale(2)*c-1)+1 ) Ī 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. % map from output image location to input image location OutputI = zeros(scale(1)*r,scale(2)*c, class(inputI)) Scale = % you could scale each dimension differently Putting pieces together, we get the following code: % read a sample image Therefore substituting for each gives us: jj = (j-1)*(c-1)/(scaleC*c-1) + 1 In our case, x is the i/ j coordinate and y is the ii/ jj coordinate. Since this is a simple mapping we use the formula that maps a given x to y (given all the other params): x-minX y-minY 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. Note: I am using matrix notation (row/col), so: To illustrate, consider the following notation: 1 c 1 scaleC*c

What you should be doing is the opposite (from output to input). Consider an example where input image is all white and output initialized to black, we get the following: 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. In case you are trying to understand how it works, keep reading… I think gnovice‘s solution is best in that regard. Plot(Xout(end),Yout(end),'r.This answer is more explanatory than trying to be concise and efficient. Title()Įxample 3: continuous points, pathological caseĮxample 4: continuous points, distance limit applied This will, however, find the shortest path for points with a symmetric nearest neighbor matrix. Note that this will *not* necessarily form the shortest path between all the points - that is the NP-Hard Traveling Salesman Problem, for which there is no deterministic solution. The input points can be of any numerical class. Likewise, in cases where one point is far from its neighbors, it may be orphaned, and only connected into the path at the end, giving strange results.

Picking a different P will result in a different contour. any time there are more than 2 nearest neighbor points, or in situations where the nearest neighbor matrix is non-symmetric. There are many (Inf) situations where there is no unique mapping of points into a connected contour - e.g. The optional output 'indout' is the order of indices that produces Xin(indout)=Xout and Yin(indout)=Yout. The optional output 'orphans' gives the indices of the original (Xin,Yin) points that were not included in the contour. The optional input parameter 'dlim' sets a distance limit, if the distance between a point and all other points is greater than or equal to 'dlim', the point is left out of the contour. 'P' sets the point to begin looking for the contour from the original ordering of (Xin,Yin), and 'direction' sets the direction of the contour, with options 'cw' and 'ccw', specifying clockwise and counter-clockwise, respectively. The code has been written to handle square and hexagon grid points, as well as any non-grid arrangement of points. Given any list of 2D points (Xin,Yin), construct a singly connected nearest-neighbor path in either the 'cw' or 'ccw' directions. =points2contour(Xin,Yin,P,direction,dlim)
