[英]Matlab rotating image-point in 2D around arbitrary point
我想使用Matlab在2D上圍繞任意點旋轉一個點。 我正在實現以下等式:
我有以下代碼。 在我的實施中
original = [eye_left(1); eye_left(2);1];
m= size(im,2)/2 % width/2 (I want to rotate around the center of the image)
n= size(im,1)/2 % height/2
first = [1,0,-(size(im,2)/2); 0, 1,-(size(im,1)/2);0, 0, 1];
second = [cos(angle), -sin(angle),0; sin(angle), cos(angle), 0;0, 0, 1];
third = [1,0,size(im,2)/2; 0, 1,size(im,1)/2;0, 0, 1];
rotated = third* second* first * original;
但是在rotated
變量中,y值總是離我期望的位置太遠...
編輯我的第一個答案是基於對該問題的誤解。 我已經包含了修改后的代碼。 現在,它看起來更像您的原始照片了。 得到轉換點的結果1.0e+003 * [0.6475 1.6242]
。 繪制時,這似乎(如預期的那樣)幾乎完美地反射了整個中心。
工作代碼
clear all
close all
%% Points and angles (angle in radians)
angle = -3.1150
mypoint = [634 232] % Near top, middle
%% Sample data
load('mandrill', 'X', 'map');
im = uint8(X);
% Using Image Processing Toolkit to create an image 1300 wide by 1856 high
im = imresize(im, [1856 1300]);
%% Calculate 'center' from extrema
m = size(im,2)/2 % width/2 (I want to rotate around the center of the image)
n = size(im,1)/2 % height/2
%% Tx matrices
first = [...
1 0 -m;
0 1 -n;
0 0 1];
third = [...
1 0 m;
0 1 n;
0 0 1];
second = [...
cos(angle) -sin(angle) 0;
sin(angle) cos(angle) 0;
0 0 1];
%% Use homogenous coords
mp_hom = [mypoint 1]
%% Calculate (note because we premultiply,
rotated_hom = third* second* first* mp_hom';
rotated = rotated_hom'
%% Show it!
imshow(im)
hold on
plot(mypoint(:,1), mypoint(:,2), 'g+', 'MarkerSize', 12, 'LineWidth', 3)
plot (m, n, 'b*', 'MarkerSize', 12, 'LineWidth', 3)
plot(rotated(:,1), rotated(:,2), 'gx', 'MarkerSize', 12, 'LineWidth', 3)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.