如何使用Matlab通过最近邻插值旋转图像

如何使用Matlab通过最近邻插值旋转图像

本文介绍了如何使用Matlab通过最近邻插值旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的普通代码没有插值:

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);

此代码创建黑点,问题是如何进行插值?谢谢大家的任何照明。
P.S.不要求内置函数:imrotate(im1,1 / thet,'nearest');

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');

推荐答案

旋转图像没有黑点,你需要反方向。

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

旋转矩阵的倒数是它的转置。此外,旋转的图像总是更大,最大是45度旋转。因此, sqrt(2)因子

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);

这篇关于如何使用Matlab通过最近邻插值旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:14