How To Rotate Image By Nearest Neighbor Interpolation Using Matlab
Asked Answered
U

4

12

My Plain Code without interpolation:

im1 = imread('lena.jpg');imshow(im1);    
[m,n,p]=size(im1);
thet = rand(1);
m1=m*cos(thet)+n*sin(thet);
n1=m*sin(thet)+n*cos(thet);    

for i=1:m
    for j=1:n
       t = uint16((i-m/2)*cos(thet)-(j-n/2)*sin(thet)+m1/2);
       s = uint16((i-m/2)*sin(thet)+(j-n/2)*cos(thet)+n1/2);
       if t~=0 && s~=0           
        im2(t,s,:)=im1(i,j,:);
       end
    end
end
figure;
imshow(im2);

This code creates black spot, the problem is how to do interpolation? Thank you all for any illumination. P.S. Not asking for build-in function: imrotate(im1,1/thet,'nearest');

Unassuming answered 28/11, 2009 at 2:43 Comment(2)
Are you trying to do a feature rotation or a global rotation of the entire image?Chafin
it's a global rotation of the entire imgUnassuming
H
11

To rotate the image without the black spots, you need to go in the reverse direction.

The inverse of the rotation matrix is the transpose of it. Also, the rotated image is always bigger with maximum being 45 degree rotation. Hence, the sqrt(2) factor

im1 = imread('lena.jpg');imshow(im1);  
[m,n,p]=size(im1);
thet = rand(1);
mm = m*sqrt(2);
nn = n*sqrt(2);
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
figure;
imshow(im2);
Hereford answered 3/12, 2009 at 23:50 Comment(2)
So the common solving of nearest neighbor interpolation is implicit? I thought it would detect the left most visible pixel and the right most, and then interpolate line by line.Unassuming
What you describe would be a bilinear interpolation. Near neighbor just takes the closest pixel valueHereford
H
6

I remember a previous question on SO that had a similar problem.

The idea I had was to map the pixels in the opposite direction; for each pixel in the rotated image, find the pixel(s) that maps to it in the original image, then the problem becomes much simpler.

I don't have access to MATLAB at this moment, but I think it is doable. The difficulty here is looping over the rotated image pixels..

Hern answered 28/11, 2009 at 3:18 Comment(3)
Thanks, Amro. I did check your previous post before I was asking. The mapping is indeed in the opposite direction, which makes it simpler. And you just get my difficulty.Unassuming
This is how rotation is usually performed since it avoids processing pixels which are not visible in the final result anyway.Stannum
it's been quite a while but has someone come up with a faster implementation to the rotation problem, similar to the one made in this link?Tyrolienne
P
0

Once you have all the transformed pixels, you can fill in the black spots with griddata which takes in a non-uniform spatial distribution of pixels (your rotated pixels) and interpolates the required pixels (your black spots) using linear, cubic or nearest-neighbor.

Pogonia answered 28/11, 2009 at 7:6 Comment(1)
It's still built-in feature to do interpolation, though. Thank you anyway, Jacob.Unassuming
A
0

Black Spots can be removed by these lines, while the rest of the code remains same:

im2= zeros(500,500);
im2(:)=1;
Affable answered 13/1, 2013 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.