matlab - 如何在图像中旋转图像?
问题描述
我正在尝试开发一个将 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
解决方案
如果我理解正确的话,这应该做你想做的事:
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);
推荐阅读
- airflow - AWS Airflow 上的作业失败
- python - Google Coral Boards 是否有 TensorFlowServing 等价物?
- statistics - 获得平均 CPU 和内存使用率很热
- javascript - 为这个漂亮的六边形文字滚轮添加超过 6 个面部内容
- python - 使用 sparql 删除/清除名为图的海王星的所有内容
- python - 用于过滤列值的多个输入
- c# - 如何使这个 JSON 加载方法通用
- webpack - Webpack、html-loader、file-loader 和 extract-loader
- vue.js - 子组件更新时如何防止 props 重置?
- android - 无法在 android studio 中构建或运行