我想使用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值总是远离我预期的那么远......
答案 0 :(得分:1)
被修改
我的第一个答案是基于对这个问题的误解。我已经修改了代码。它现在看起来更像你的原创。我得到了结果
转化点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)