任意の点を中心として画像を回転する方法

21 views (last 30 days)
A I
A I on 11 Oct 2022
Commented: A I on 16 Oct 2022
お世話になります。
I = readimage('images.jpg');
J = imrotate(I, 45, 'bilinear', 'crop');
figure
imshow(J)
title('Rotated Image')
のように、'imrotate'を使用して画像を回転できますが、画像の中心点を中心とした回転のみです。
任意の点を回転中心として、処理を行える方法があればご教授いただきたいです。
よろしくお願いいたします。
  2 Comments
Atsushi Ueno
Atsushi Ueno on 15 Oct 2022
画像サイズ'FollowOutput'のどこを切り出すかという視点で考えるとなんか難しいですね。
下記は直接の回答になってないのでコメントに残します。
変換行列の演算を自分で書くと自由にパラメータを動かす事が出来ます。(回転中心はcx,cy。0で画像左上が回転中心)
ただ、補間しないとimrotate同様にならず画像がギザギザになるので課題が残ります。
I = imread('onion.png'); sz = size(I)';
cx = sz(1)/2; cy = sz(2)/2; % cx,xyが回転中心、
th = pi / 10; % thは回転角度
c = [0, 0, cx; % 平行移動行列
0, 0, cy;
0, 0, 0];
rt = [cos(th), sin(th), 0; % 回転行列
-sin(th), cos(th), 0;
0, 0, 1];
tr = (eye(3) + c) * rt * (eye(3) - c); % 平行移動⇒回転⇒平行移動
for x = 1:sz(1)
for y = 1:sz(2)
org = round(tr * [x; y; 1]);
if any(org < [1; 1; 1] | org > sz)
J(x,y,:) = uint8([0 0 0]);
else
J(x,y,:) = I(org(1),org(2),:);
end
end
end
imshow(J);
A I
A I on 16 Oct 2022
Uenoさん、ありがとうございます。
コメントしていただいた内容を参考に、よく使用する' imrotate 'と' imtranslate 'で組んでみました。
2段階の処理になっていますが、' imrotate 'を使用していますので画質の低下が防げています。
また、平行移動によって端が切り取られていますが、回転後に行いたい処理には影響しませんので、現段階では妥協点ではないかと思っております。
I1 = imread('yasai.jpg');
sz = size(I1);
% --- 回転中心 ---
cx = 350; % sz(2)/2
cy = 150; % sz(1)/2
% --- 回転角度(度) ---
angle = 15;
% --- cx, cy回転後の座標 ---
rx = round((cx - sz(2)/2) * cosd(-angle) ... % +xが右向き, +yが上向きの座標系に当てはめ、
- (cy - sz(1)/2) * sind(-angle) ... % angleはマイナス
+ sz(2)/2);
ry = round((cx - sz(2)/2) * sind(-angle) ...
+ (cy - sz(1)/2) * cosd(-angle) ...
+ sz(1)/2);
% --- 回転 ---
I2 = imrotate(I1,angle,'bilinear','crop');
% --- 平行移動 ---
j = (cx - rx); % (回転中心 - 回転後座標)
i = (cy - ry);
I3 = imtranslate(I2,[j, i],'FillValues',[0, 0, 0]);
imshow(I3);
imwrite(I3, 'yasai_afin.jpg');

Sign in to comment.

Answers (0)

Categories

Find more on 幾何学的変換とイメージ レジストレーション in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!