如何在图像内旋转图像?

时间:2018-07-22 21:32:53

标签: matlab image-processing

我正在尝试开发一个程序,该程序会将100x100像素的图像叠加到200x200像素的背景上。将提示用户移动较小的图像(向左,向右,向上,向下)和/或将CCW / CW旋转任意角度。我的问题很简单,“如何在较大的图像中旋转较小的图像?”。我尝试在较小的图像上使用imrotate,而较大的则等于较小的val。

谢谢

a = zeros(15);
b = a(7:9,7:9);

b(:) = 1; %initialize b matrix to ones
a(7:9,7:9) = b; %center matrix

    n = 1;

while n ~= 0

    n = input('PLLRAFM Aligner\n Please enter a command to align image.\n 8: up\n 2: down\n 4: left\n 6: right\n 7: rotate CCW\n 9: rotate CW\n 0: save image\n');

    switch n

    case 8 %up
     index = sub2ind(size(a),find(a == 1));
     [row, col] = ind2sub(size(a),index);
     a = zeros(15);
     row = row - 1;
     a(row,col) = 1;
     figure(2)
     imagesc(a)
    case 2 %down
     index = sub2ind(size(a),find(a == 1));
     [row, col] = ind2sub(size(a),index);
     a = zeros(15);
     row = row + 1;
     a(row,col) = 1;
     figure(2)
     imagesc(a)
    case 4 %left
     index = sub2ind(size(a),find(a == 1));
     [row, col] = ind2sub(size(a),index);
     a = zeros(15);
     col = col - 1;
     a(row,col) = 1; 
     figure(2)
     imagesc(a)
    case 6 %right
     index = sub2ind(size(a),find(a == 1));
     [row, col] = ind2sub(size(a),index);
     a = zeros(15);
     col = col + 1;
     a(row,col) = 1;    
     figure(2)
     imagesc(a)
    case 7 %rotate CCW
     index = sub2ind(size(a),find(a == 1));
     theta = 45; %temporary rotation of 1 degree
     imrotate(b,theta);
     a(b) = 1;
     figure(2)
     imagesc(a)
    case 9 %rotate CW
%      index = sub2ind(size(a),find(a == 1));
%      [row, col] = ind2sub(size(a),index);
%      theta = 45; %temporary rotation of 1 degree
%      b = imrotate(a(row,col),theta);
%      figure(2)
%      imagesc(a)
    otherwise
     fprintf('Please try again.');
    end
end

I would like to rotate this yellow block by 45 degrees for testing.

1 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,这应该可以做您想要的事情:

a=zeros(15);
b=ones(3);
b=imrotate(b,45);
a(ceil(length(a)/2)-floor(length(b)/2):ceil(length(a)/2)+floor(length(b)/2),...
    ceil(length(a)/2)-floor(length(b)/2):ceil(length(a)/2)+floor(length(b)/2))=b;
imagesc(a);