Matlab在二维空间中围绕任意点旋转图像点

Matlab在二维空间中围绕任意点旋转图像点,matlab,rotation,2d,point,Matlab,Rotation,2d,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)

我想使用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;
但是在
旋转变量中,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)

对于调试-在每次转换后跟踪点的位置:绘制转换到原点后的位置
first*original
。然后在围绕原点旋转后:
second*first*original
并最终翻译回
third*second*first*original
。哪个阶段导致坐标中的错误?原始=649.3293,278.3167,1.0000,我的角度是:-3.1150,第一个*原始:ans=-0.6707,-649.6833,1.0000,看看它已经走了多远…两个值你的图像大小是多少?我的眼睛在图像的下部,对吗?1300*1856。眼睛位于图像的较高部分我希望我的答案能有所帮助,但我不得不猜测“im”是一个二维点数组(如果是三维同质数组,则变化不大),并且您希望边界框的中心(切换到CoM很小)。如果我错了,请告诉我,我会修正我的答案。im只是一个图像矩阵(1300*1856)。所以为了得到图像的中心,我只需要对行进行大小(im,2)/2(列数除以2)和模拟。对不对?因为用你的方法,我得到m=117和n=118,这不能是图像的中心,如果图像是1300*1856,是二维二进制数组,还是其他?这取决于数据是什么,以及您到底想要什么。如果您想要图像边界的中心(假设为矩形图像),那么只需[right-left,top-bottom]/2Mmmm它只是一个矩阵:/但是我在您的代码中得到一个错误:im_-hom=[im-ones(长度(im),1)];你是说im*ones…?如果你想旋转一个2D图像矩阵,就像一张普通的图片,恐怕这对你来说不行。这就是你想要做的吗?好吧,我的代码对我来说仍然有效,所以不确定你那边出了什么问题,但我现在得到了1.0e+003*[0.6475 1.6242]作为新的点;与你先前的回答类似。但这似乎是正确的。