Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不使用imrotate的Matlab图像旋转_Matlab_Image Processing_Interpolation - Fatal编程技术网

不使用imrotate的Matlab图像旋转

不使用imrotate的Matlab图像旋转,matlab,image-processing,interpolation,Matlab,Image Processing,Interpolation,我正在尝试使用Matlab旋转图像,而不使用imrotate函数。我实际上是用变换矩阵做的。但这还不够好。问题是,旋转的图像是“滑动的”。让我用图片告诉你 这是我要旋转的图像: 但当我旋转它时,例如45度,它变成: 我在问为什么会发生这种情况。这是我的代码,是否有任何数学或编程错误 image=torso; %image padding [Rows, Cols] = size(image); Diagonal = sqrt(Rows^2 + Cols^2); RowPad = ceil

我正在尝试使用Matlab旋转图像,而不使用imrotate函数。我实际上是用变换矩阵做的。但这还不够好。问题是,旋转的图像是“滑动的”。让我用图片告诉你

这是我要旋转的图像:

但当我旋转它时,例如45度,它变成:

我在问为什么会发生这种情况。这是我的代码,是否有任何数学或编程错误

image=torso;

%image padding
[Rows, Cols] = size(image); 
Diagonal = sqrt(Rows^2 + Cols^2); 
RowPad = ceil(Diagonal - Rows) + 2;
ColPad = ceil(Diagonal - Cols) + 2;
imagepad = zeros(Rows+RowPad, Cols+ColPad);
imagepad(ceil(RowPad/2):(ceil(RowPad/2)+Rows-1),ceil(ColPad/2):(ceil(ColPad/2)+Cols-1)) = image;

degree=45;

%midpoints
midx=ceil((size(imagepad,1)+1)/2);
midy=ceil((size(imagepad,2)+1)/2);

imagerot=zeros(size(imagepad));

%rotation
for i=1:size(imagepad,1)
    for j=1:size(imagepad,2)

         x=(i-midx)*cos(degree)-(j-midy)*sin(degree);
         y=(i-midx)*sin(degree)+(j-midy)*cos(degree);
         x=round(x)+midx;
         y=round(y)+midy;

         if (x>=1 && y>=1)
              imagerot(x,y)=imagepad(i,j); % k degrees rotated image         
         end

    end
end

 figure,imagesc(imagerot);
 colormap(gray(256));

图像中有洞的原因是,您正在计算
imagerot
中每个像素在
imagepad
中的位置。你需要用另一种方法来计算。也就是说,对于
imagerot
中的每个像素,在
imagerpad
中插值。要做到这一点,您只需要应用逆变换,在旋转矩阵的情况下,逆变换只是矩阵的转置(只需更改每个
sin
上的符号,并以另一种方式进行转换)

imagerot
中循环像素:

imagerot=zeros(size(imagepad)); % midx and midy same for both

for i=1:size(imagerot,1)
    for j=1:size(imagerot,2)

         x= (i-midx)*cos(rads)+(j-midy)*sin(rads);
         y=-(i-midx)*sin(rads)+(j-midy)*cos(rads);
         x=round(x)+midx;
         y=round(y)+midy;

         if (x>=1 && y>=1 && x<=size(imagepad,2) && y<=size(imagepad,1))
              imagerot(i,j)=imagepad(x,y); % k degrees rotated image         
         end

    end
end
要将上述内容修改为线性插值,请计算
XYt
中每个坐标的4个相邻像素,并使用分数分量乘积作为权重执行加权和。我将把它留作练习,因为它只会使我的答案超出你问题的范围

您使用的方法(通过采样旋转)是最快、最简单的,但也是最不准确的

下面给出的区域映射旋转(这是一个很好的参考)在保留颜色方面更好

但是:请注意,这只适用于灰度/RGB图像,而不适用于您似乎正在使用的彩色映射图像

image = imread('peppers.png');

figure(1), clf, hold on
subplot(1,2,1)
imshow(image);

degree = 45;

switch mod(degree, 360)
    % Special cases
    case 0
        imagerot = image;
    case 90
        imagerot = rot90(image);
    case 180
        imagerot = image(end:-1:1, end:-1:1);
    case 270
        imagerot = rot90(image(end:-1:1, end:-1:1));

    % General rotations
    otherwise

        % Convert to radians and create transformation matrix
        a = degree*pi/180;
        R = [+cos(a) +sin(a); -sin(a) +cos(a)];

        % Figure out the size of the transformed image
        [m,n,p] = size(image);
        dest = round( [1 1; 1 n; m 1; m n]*R );
        dest = bsxfun(@minus, dest, min(dest)) + 1;
        imagerot = zeros([max(dest) p],class(image));

        % Map all pixels of the transformed image to the original image
        for ii = 1:size(imagerot,1)
            for jj = 1:size(imagerot,2)
                source = ([ii jj]-dest(1,:))*R.';
                if all(source >= 1) && all(source <= [m n])

                    % Get all 4 surrounding pixels
                    C = ceil(source);
                    F = floor(source);

                    % Compute the relative areas
                    A = [...
                        ((C(2)-source(2))*(C(1)-source(1))),...
                        ((source(2)-F(2))*(source(1)-F(1)));
                        ((C(2)-source(2))*(source(1)-F(1))),...
                        ((source(2)-F(2))*(C(1)-source(1)))];

                    % Extract colors and re-scale them relative to area
                    cols = bsxfun(@times, A, double(image(F(1):C(1),F(2):C(2),:)));

                    % Assign                     
                    imagerot(ii,jj,:) = sum(sum(cols),2);

                end
            end
        end        
end

subplot(1,2,2)
imshow(imagerot);
image=imread('peppers.png');
图(1),clf,保持
子地块(1,2,1)
imshow(图像);
度=45;
开关模式(360度)
%特例
案例0
imagerot=图像;
案例90
imagerot=rot90(图像);
案例180
imagerot=图像(结束:-1:1,结束:-1:1);
案例270
imagerot=rot90(图像(结束:-1:1,结束:-1:1));
%一般轮换
否则
%转换为弧度并创建变换矩阵
a=度*pi/180;
R=[+cos(a)+sin(a);-sin(a)+cos(a)];
%计算变换图像的大小
[m,n,p]=尺寸(图像);
dest=圆形([11;1n;m1;mn]*R);
dest=bsxfun(@负,dest,min(dest))+1;
imagerot=零([max(dest)p],类别(图像));
%将变换图像的所有像素映射到原始图像
对于ii=1:尺寸(imagerot,1)
对于jj=1:大小(imagerot,2)
来源=([ii jj]-dest(1,:)*R;
如果all(source>=1)和&all(source检查此项

这是你能做的最快的方法。


在matlab中,根据用户给定的角度旋转彩色图像,无需对图像进行任何裁剪

该程序的输出类似于内置命令“imrotate”的输出.该程序根据用户输入的角度动态创建背景。通过使用旋转矩阵和原点偏移,我们得到初始图像和最终图像的坐标关系。使用初始图像和最终图像的坐标关系,我们现在映射每个像素的强度值

img=imread('img.jpg'); 

[rowsi,colsi,z]= size(img); 

angle=45;

rads=2*pi*angle/360;  

%calculating array dimesions such that  rotated image gets fit in it exactly.
% we are using absolute so that we get  positve value in any case ie.,any quadrant.

rowsf=ceil(rowsi*abs(cos(rads))+colsi*abs(sin(rads)));                      
colsf=ceil(rowsi*abs(sin(rads))+colsi*abs(cos(rads)));                     

% define an array withcalculated dimensionsand fill the array  with zeros ie.,black
C=uint8(zeros([rowsf colsf 3 ]));

%calculating center of original and final image
xo=ceil(rowsi/2);                                                            
yo=ceil(colsi/2);

midx=ceil((size(C,1))/2);
midy=ceil((size(C,2))/2);

% in this loop we calculate corresponding coordinates of pixel of A 
% for each pixel of C, and its intensity will be  assigned after checking
% weather it lie in the bound of A (original image)
for i=1:size(C,1)
    for j=1:size(C,2)                                                       

         x= (i-midx)*cos(rads)+(j-midy)*sin(rads);                                       
         y= -(i-midx)*sin(rads)+(j-midy)*cos(rads);                             
         x=round(x)+xo;
         y=round(y)+yo;

         if (x>=1 && y>=1 && x<=size(img,1) &&  y<=size(img,2) ) 
              C(i,j,:)=img(x,y,:);  
         end

    end
end

imshow(C);
img=imread('img.jpg');
[rowsi,colsi,z]=尺寸(img);
角度=45;
rads=2*pi*角度/360;
%计算数组尺寸,使旋转的图像精确地适合它。
%我们使用的是绝对值,所以在任何情况下,即任何象限,我们都可以得到正值。
rowsf=ceil(rowsi*abs(cos(rads))+colsi*abs(sin(rads));
colsf=ceil(rowsi*abs(sin(rads))+colsi*abs(cos(rads));
%使用计算尺寸定义数组,并用零(即黑色)填充数组
C=uint8(零([rowsf colsf 3]);
%计算原始图像和最终图像的中心
xo=ceil(rowsi/2);
yo=ceil(colsi/2);
midx=ceil((大小(C,1))/2);
midy=细胞((大小(C,2))/2);
%在这个循环中,我们计算一个图像像素的对应坐标
%对于C的每个像素,其强度将在检查后指定
%它是否位于(原始图像)的边界内
对于i=1:尺寸(C,1)
对于j=1:尺寸(C,2)
x=(i-midx)*cos(rads)+(j-midy)*sin(rads);
y=-(i-midx)*sin(rads)+(j-midy)*cos(rads);
x=圆形(x)+xo;
y=圆形(y)+yo;

如果(x>=1&&y>=1&&x你试过用弧度代替度吗?事实上我试过了。但是没有任何改变。旋转后的图片是正确的,只是它的坐标。正如@Junuxx指出的,
cos
sin
使用弧度,而不是度。如果你想使用度,你应该使用
cosd
sind
。谢谢但我也试过了。还是一样。我的方程中有数学错误吗?@Moondra我想在图像上多加1左1右,1上1下填充。如果我没记错的话,你不必使用它。谢谢,这真的很有效。所以我试着找出错误的(x,y)我可以再问一件事吗?在找到x和y之后,为什么我们要加上midx,midy?我对这方面的数学很感兴趣rotation@Zapdos-若要围绕图像中心旋转,必须减去质心,变换,然后将其添加回。对于特殊情况处理,这是一个好主意。请注意
rot90(A,K)
将同时进行
K
90次旋转,使用与您相同的方法进行180次旋转,然后进行转置和翻转270次。嗯,重点是在不使用变换函数的情况下学习和执行此操作,无论是
imtransform
还是
imrotate
。但是的,这是您实际操作的方式,因此+1有助于实现这一点使用图像处理工具箱。重点是在没有它的情况下执行此操作。此答案也被弃用为
maketform<
img = imread('Koala.jpg');

theta = pi/10;
rmat = [
cos(theta) sin(theta) 0
-sin(theta) cos(theta) 0
0           0          1];

mx = size(img,2);
my = size(img,1);
corners = [
    0  0  1
    mx 0  1
    0  my 1
    mx my 1];
new_c = corners*rmat;

T = maketform('affine', rmat);   %# represents translation
img2 = imtransform(img, T, ...
    'XData',[min(new_c(:,1)) max(new_c(:,1))],...
    'YData',[min(new_c(:,2)) max(new_c(:,2))]);
subplot(121), imshow(img);
subplot(122), imshow(img2);
img=imread('img.jpg'); 

[rowsi,colsi,z]= size(img); 

angle=45;

rads=2*pi*angle/360;  

%calculating array dimesions such that  rotated image gets fit in it exactly.
% we are using absolute so that we get  positve value in any case ie.,any quadrant.

rowsf=ceil(rowsi*abs(cos(rads))+colsi*abs(sin(rads)));                      
colsf=ceil(rowsi*abs(sin(rads))+colsi*abs(cos(rads)));                     

% define an array withcalculated dimensionsand fill the array  with zeros ie.,black
C=uint8(zeros([rowsf colsf 3 ]));

%calculating center of original and final image
xo=ceil(rowsi/2);                                                            
yo=ceil(colsi/2);

midx=ceil((size(C,1))/2);
midy=ceil((size(C,2))/2);

% in this loop we calculate corresponding coordinates of pixel of A 
% for each pixel of C, and its intensity will be  assigned after checking
% weather it lie in the bound of A (original image)
for i=1:size(C,1)
    for j=1:size(C,2)                                                       

         x= (i-midx)*cos(rads)+(j-midy)*sin(rads);                                       
         y= -(i-midx)*sin(rads)+(j-midy)*cos(rads);                             
         x=round(x)+xo;
         y=round(y)+yo;

         if (x>=1 && y>=1 && x<=size(img,1) &&  y<=size(img,2) ) 
              C(i,j,:)=img(x,y,:);  
         end

    end
end

imshow(C);