Question

I want to rotate a point in 2D around an arbitrary point using Matlab. I am implementing following equation: enter image description here

I have the following code. In my implementation

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;

But in the rotated variable the y-values is always soo far away from where I expect it to be...

Était-ce utile?

La solution

Edited My first answer was based on a misunderstanding of the question. I have included revised code. It now appears to be much more like your original. I get the result 1.0e+003 * [0.6475 1.6242] for the transformed point. When plotted, this appears (as expected) almost perfectly reflected across the center.

Working code

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)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top