【问题标题】:How do I rotate image within an image?如何在图像中旋转图像?
【发布时间】:2018-07-23 07:19:55
【问题描述】:

我正在尝试开发一个程序,将 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

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

【问题讨论】:

    标签: matlab image-processing


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 2017-04-14
      • 2016-02-04
      • 2015-04-04
      • 2011-06-25
      相关资源
      最近更新 更多