I imrotate()一个图像,画两条线,将线向后旋转并在原始图像中绘制,但不要';在MATLAB中没有得到预期的结果? 我想做的是:

I imrotate()一个图像,画两条线,将线向后旋转并在原始图像中绘制,但不要';在MATLAB中没有得到预期的结果? 我想做的是:,matlab,rotation,translation,image-rotation,Matlab,Rotation,Translation,Image Rotation,假设我有一个图像I,我使用imrotate旋转-45°(我得到I\u R)。然后我画了两条线,分别是AB和CD(平行线)。最后,我将两条线向后旋转(45°),并在原始图像中绘制它们 我是怎么做到的## 我使用MATLAB函数旋转I I_R = imrotate(I,-45); 从Matlab帮助中,我得到:B=imrotate(A,angle)将图像A围绕其中心点逆时针旋转角度度 但是似乎imrotate为图像添加了一个翻译!我有 读了matlab内置函数的代码,似乎它使用了 调用函数getO

假设我有一个图像
I
,我使用
imrotate
旋转-45°(我得到
I\u R
)。然后我画了两条线,分别是AB和CD(平行线)。最后,我将两条线向后旋转(45°),并在原始图像中绘制它们

我是怎么做到的## 我使用MATLAB函数旋转
I

I_R = imrotate(I,-45);
从Matlab帮助中,我得到:B=imrotate(A,angle)将图像A围绕其中心点逆时针旋转角度度

但是似乎
imrotate
为图像添加了一个翻译!我有 读了matlab内置函数的代码,似乎它使用了 调用函数
getOutputBound
,检查旋转后的图像是否 适合这个数字。这是我要找的翻译

四个点
A、B、C、D
形成两条平行线
AB
&
CD

A = [x_A; u];
B = [x_B; u];

C = [x_A; d];
D = [x_B; d];
现在,我旋转这两行,我使用函数
rotateTwoPoints()
,只需调用以下两行:

[Af,Bf] = rotateTwoPoints(A,B,-45,O,true);
[Cf,Df] = rotateTwoPoints(C,D,-45,O,true);
其中
O
是旋转将围绕的原点

  • 我试过
    O=[0;0]
    我的意思是它是绘图的原点。没有成功
  • 因此,我使用
    regionprops(I,“质心”)
    选择图像的质心
    I
    。这是错误的,因为质心不是中心
  • 现在,我使用图像的中心
    O=floor(大小(I)/2+0.5)
    或使用
    ceil
但是当我在图像中画出结果线时,像这样:

plot([Af(1) Bf(1)],[Af(2) Bf(2)],'k');
plot([Cf(1) Df(1)],[Cf(2) Df(2)],'k'); 
我得到了一个不正确的结果

问题:在I\u R中,
AB
CD
包含我称之为蓝区的内容(参见图3)。但是旋转后的行
AfBf
CfDf
没有覆盖它

在图像中显示结果 这是旋转图像I\u R和绘制的两条线(中间的两条红线对应于
AB
CD
):

然后,我在原始图像I中绘制旋转线
AfBf
CfDf
(黑色粗体点对应于我旋转的中心):

图像更新

问题:您可以看到蓝区位于两行
AB
CD
内。但当向后旋转时,它变为外部,如下图所示(红色箭头指向蓝色区域):


更新添加了一个代码段 由于问题尚未解决,我选择了导致问题的代码,并将其添加为以下代码段(文件中存储了一个变量,您可以下载):

函数问题()
%在I中加载图像,图像可在下面的链接中联机获取
负荷I;
%使用imrotate将I旋转-45°
I_R=I旋转(I,-45);
%一些数据
x_A=3;
x_B=79;
u=24;
d=44;
%一些有意义的点:A、B、C和D形成两条线AB和CD
%相似
A=[x_A;u];
B=[x_B;u];
C=[x_A;d];
D=[x_B;D];
%图1包含两个子批次
图(1);
%绘制旋转图像I\u R
子批次(1,2,1),轴图像,图像C(I_R),保持;
%在旋转图像中用红色绘制两条AB和CD线
地块([A(1)B(1)],[A(2)B(2)],'r');
地块([C(1)D(1)],[C(2)D(2)],'r');
标题(“使用两行AB和CD旋转图像”);
%绘制原始图像I
子批次(1,2,2),轴图像,图像C(I),保持;
%计算图像I的中间
轴=轴;
中心=[平均值(轴(1:2)),平均值(轴(3:4))];
%以红色和黄色绘制中心作为点
地块(中心(1),中心(2),‘ro’;
%旋转两条线,结果是两条线AfBf和CfDf
[Af,Bf]=旋转点(A,B,-45,中心,真);
[Cf,Df]=旋转点(C,D,-45,中心,真);
%在原始图像I中绘制旋转后的直线
图(1);
子批次(1,2,2);
图([Af(1)Bf(1)],[Af(2)Bf(2)],'k');
图([Cf(1)Df(1)],[Cf(2)Df(2)],'k');
标题(“原始图像I,带有两行AfBf和CfDf”);
函数[Af,Bf]=旋转网络点(A,B,t,原点,isPlot)
%旋转矩阵的定义(围绕原点旋转)
R=[。。。
cosd(t)-sind(t)
sind(t)cosd(t)
];
%翻译
At=A-原点;
Bt=B-来源;
%A点和B点的旋转
Ar=R*At;
Br=R*Bt;
%翻译
Af=Ar+原点;
Bf=Br+原点;
如果isPlot==true
图(100)
%原线路图
地块(A(1),A(2),‘k*’,B(1),B(2),‘B*’;
行([A(1)B(1)],[A(2)B(2)],“颜色”和“r”);
网格化
等等
%绘制旋转将围绕的原点
绘图(原点(1)、原点(2)、‘k*’、‘线宽’、3);
%旋转线的绘图
图(Af(1),Af(2),'g*',Bf(1),Bf(2),'r*');
行([Af(1)Bf(1)],[Af(2)Bf(2)],'Color','b');
图例(‘A’、‘B’、‘AB线’、‘原点’、‘Af’、‘Bf’、‘AfBf线’、[‘角度:’、num2str(t)]、‘位置’、‘东北外’;
daspect([1])

end
可能无法修复,但请尝试将矩阵定义为:

R=[ ...
    cosd(t) -sind(t)
    -sind(t) -cosd(t)
    ];

在图像上打印时,需要注意y轴,y轴会自动从上到下移动。

正如您所说,旋转计算正确,如第一个打印所示。那么问题就在于结果的最终显示。当你这么做的时候

plot([Af(1) Bf(1)],[Af(2) Bf(2)],'k');
plot([Cf(1) Df(1)],[Cf(2) Bf(2)],'k'); 
您在第二行(第二个参数的第二个元素)有一个输入错误-您正在将
Bf(2)
绘制为第二行的结尾,而不是
Df(2)
。当我将其替换为
Df(2)% get an image
I = imread('cameraman.tif');
% get spatial referencing information
Rcb = imref2d(size(I));

% define some test points in 3space
pta = [10; 10; 0];
ptb = [ 50;  10; 0];
% construct a test line in 2space from our test points
testline = [pta(1:2) ptb(1:2)];

% overlay line on plot
figure(90); clf
imshow(I,Rcb); truesize; hold on;
plot(testline(1, :), testline(2, :), 'y', 'linewidth', 4)

% define our transforms
% rotation angle (deg)
t = 45;

% a transform suitable for images
Rimage=[ ...
    cosd(t) -sind(t) 0
    sind(t) cosd(t) 0
    0 0 1
    ];
% the same transform suitable for points
Rpoints=[ ...
    cosd(-t) -sind(-t) 0
    sind(-t) cosd(-t) 0
    0 0 1
    ];

% make tform object suitable for imwarp
tform = affine2d(Rimage);

% transform image and spatial referencing with tform
[Ir, Rr] = imwarp(I, tform);

% transform points directly using matrix multplication
ptar = Rpoints*pta;
ptbr = Rpoints*ptb;

% construct the rotated line for plotting
newline = [ptar(1:2) ptbr(1:2)];

% the results
figure(91); clf
imshow(Ir, Rr); truesize; 
hold on;
plot(testline(1, :), testline(2, :), 'y', 'linewidth', 4)
plot(newline(1, :), newline(2, :), 'g', 'linewidth', 4)
testpoint = [1; 5]; % x and y xoordinates only
trpoint = Rpoints*[testpoint; 1];
trpoint = trpoint(1:2);
trpoint = Rpoints(1:2, 1:2)*testpoint;
imshow(Ir, Rr);
xmax = size(I,2);
ymax = size(I,2);
corner1 = [xmax; 0; 0]+0.5;
corner2 = [xmax; ymax; 0]+0.5;
corner3 = [0; ymax; 0]+0.5;
corner4 = [0; 0; 0]+0.5;
tc1 = Rpoints*corner1;
tc2 = Rpoints*corner2;
tc3 = Rpoints*corner3;
tc4 = Rpoints*corner4;
corners = [tc1 tc2 tc3 tc4];
xlims = minmax(corners(1,:));
ylims = minmax(corners(2,:));
imshow(Ir, 'xdata', xlims, 'ydata', ylims);
XWorldLimits: [0.2264 363.2264]
YWorldLimits: [-181.5000 181.5000]
xlims: [0.7071  362.7458];
ylims: [-181.0193  181.0193];
I_R = imrotate(I,-45,'nearest','crop');