I am trying to understand the implementation of nearest neighbor algorithm for image rotation. The conventional nearest neighbor algorithms I know, calculate some explicit euclidean distances between different points and take the point with the lowest euclidean distance as the best point. But in image interpolation, I do not find any explicit euclidean distance in the implementation. I am referring to this answer to another similar question. The given solution perfectly rotates an input image by the given angle. But I have many questions about the code (I do not understand what they are doing).
1.) Why does the author multiply by sqrt(2)
for the new indices (lines 4 and 5) ?
2.) What is the author doing in the following lines of code (to be precise, I understand he is multiplying the indices by rotation matrix. But why does he have the extra terms like m/2
, n/2
, t-mm/2
and s-nn/2
? What is he doing with if i>0 && j>0 && i<=m && j<=n
?) ? :
for t=1:mm
for s=1:nn
i = uint16((t-mm/2)*cos(thet)+(s-nn/2)*sin(thet)+m/2);
j = uint16(-(t-mm/2)*sin(thet)+(s-nn/2)*cos(thet)+n/2);
if i>0 && j>0 && i<=m && j<=n
im2(t,s,:)=im1(i,j,:);
end
end
end
Any help will be much appreciated !
sqrt(2)
factor in the answer, "the rotated image is always bigger with maximum being 45 degree rotation. Hence, the sqrt(2) factor". As for theif
condition, all that's doing is making sure that the points are within the range of the original imageim1
. – beakersqrt(2)
for the multiplication ? What is the significance ofsqrt(2)
? In theif
condition, I see that he is literally copying data from the source image instead of doing any nearest neighbor interpolation (estimating the value of pixels for missing indices when we truncate floating point values ofcos(theta)
andsin(theta)
). There is no b-spline function or any other nearest neighbor interpolation being used... What is he doing here ? – Arun Kumarround(...)
, instead it truncates. It's not the same, but just leads to a half-pixel shift in both directions, which you would likely not notice. – Cris Luengo