计算结果数组的大小(矩阵变换)

时间:2017-10-25 15:41:40

标签: matlab matrix transformation

所以我有这个代码来旋转和扭曲图像。它适用于旋转,图像将完全适合画布。但是,如果我应用歪斜,则图像不再适合。有人可以解释如何为特定角度旋转和倾斜的结果图像计算正确的阵列尺寸吗?特别是,我使用这2行作为旋转图像(虽然我不完全理解它们但它仍然有效)。如何修改它们,即使在倾斜时,最终图像也适合?谢谢!

 export class HeroComponent implements OnInit {

  constructor(private sidenavService: SidenavService) {      
  }

  ngOnInit() {
    this.sidenavService.sideNav.close();
  }
}

完整代码

rows_new = ceil(rows_init_img * abs(cos(rads)) + cols_init_img * abs(sin(rads)));                      
cols_new = ceil(rows_init_img * abs(sin(rads)) + cols_init_img * abs(cos(rads)));

更新代码

clc;
clear;

%% init values

%loading initial image
init_img = imread('name2.png');

% define rows/cols dimension of original image pixel matrix
[rows_init_img, cols_init_img,z]= size(init_img); 

% skew angle in radians
sk_angle = 50;
sk_rads = 2*pi*sk_angle/360;

% rotation angle in radians
angle = 20;
rads = 2*pi*angle/360;  

%% calculate size of final_image
orig_corners = [ 1, 1; 1, rows_init_img; 1, cols_init_img; rows_init_img, cols_init_img];
new_corners = uint8(zeros(size(orig_corners)));

for i = 1:size(orig_corners, 1)
    for j = 1:size(orig_corners, 2)
        % translate
        a = i - final_origin_x;
        b = j - final_origin_y;

        % rotate
        x = a * cos(rads) - b * sin(rads);
        y = a * sin(rads) + b * cos(rads);

        % skew along x axis (AFTER rotation)
        x = x + sk_rads * y;

        % translate
        x = x + init_origin_x;
        y = y + init_origin_y;

        % round to turn values to positive integers
        x = round(x);
        y = round(y);

        if (x >= 1 && y >= 1 && x <= size(orig_corners, 1) && y <= size(orig_corners, 2) ) 
          new_corners(i, j) = init_img(x, y);  
        end
    end
end

% calculating array dimesions such that rotated image gets fit in it exactly.
% rows_new = ceil(rows_init_img * abs(cos(rads)) + cols_init_img * abs(sin(rads)));                      
% cols_new = ceil(rows_init_img * abs(sin(rads)) + cols_init_img * abs(cos(rads)));  

% define an array with calculated dimensions and fill the array  with zeros ie.,black
% uint8 is important. without it will show noise WHY?
final_img = uint8(zeros([rows_new cols_new 3 ]));

%calculating center of original image
init_origin_x = ceil(rows_init_img/2);                                                            
init_origin_y = ceil(cols_init_img/2);

%calculating center of final image
final_origin_x = ceil( size(final_img, 1)/2 );
final_origin_y = ceil( size(final_img, 2)/2 );

%% main loop

% apply transformation to each pixel of the image
for i = 1:size(final_img ,1)
    for j = 1:size(final_img, 2)                                                       

        % translate
        a = i - final_origin_x;
        b = j - final_origin_y;

        % rotate
        x = a * cos(rads) - b * sin(rads);
        y = a * sin(rads) + b * cos(rads);

        % skew along x axis
        x = x + sk_rads * y;

        % translate
        x = x + init_origin_x;
        y = y + init_origin_y;

        % round to turn values to positive integers
        x = round(x);
        y = round(y);

        % make sure values exists (are part of the initial image) and copy
        % them in the final image matrix
        if (x >= 1 && y >= 1 && x <= size(init_img, 1) && y <= size(init_img, 2) ) 
          final_img(i, j, :) = init_img(x, y, :);  
        end

    end
end

%% display images

% original image
% figure('name','Original Image','numbertitle','off');
% imshow(init_img);

% result image
figure('name','Manipulated Image','numbertitle','off');
imshow(final_img);

0 个答案:

没有答案