首页 > 解决方案 > 如何在图像中旋转图像?

问题描述

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

谢谢

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

我想将这个黄色块旋转 45 度进行测试。

标签: matlabimage-processing

解决方案


如果我理解正确的话,这应该做你想做的事:

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

推荐阅读