我有一个强度图像,其中我用 impoly 函数标记了一个感兴趣的区域。我有一个代码,可以让我返回仍然包含我的多边形的最小矩形的顶点。矩形大部分时间不与轴对齐。我想将矩形内的数据接收为矩阵形式。我一直在尝试找出矩形最大边与 x 轴之间的角度,然后旋转图像,使矩形与轴对齐.我将非常感谢有关如何将矩形内的值提取到矩阵中的任何帮助或新想法。这是我的代码的一部分:
filename = uigetfile; %get the file name
obj = VideoReader(filename);
nFrames=obj.NumberOfFrames;
thisfig = figure();
for k = 1 : nFrames
this_frame = read(obj, k);
thisax = axes('Parent', thisfig);
image(this_frame, 'Parent', thisax);
if k==nFrames
title(thisax, sprintf('Frame #%d', k));
end
if k==1
result=input('How many polygons would you like to draw? ');
for i=1:result
handle=impoly;
accepted_pos = wait(handle);
BW = createMask(handle);
sparse_image=sparse(BW);
[XX, YY] = find(sparse_image);
[rectx,recty]=minboundrect(XX,YY); %the function that returns the vertices of the rectangle
points=[rectx(1),rectx(2),rectx(3),rectx(4);recty(1),recty(2),recty(3),recty(4)]
distance1=((points(1,2)-points(1,1))^2+(points(2,2)-points(2,1))^2)^0.5;
distance2=((points(1,3)-points(1,2))^2+(points(2,3)-points(2,2))^2)^0.5;
if(distance1>distance2) %which side of the rectangle is the largest
vector=[points(1,2)-points(1,1),points(2,2)-points(2,1)];
else
vector=[points(1,3)-points(1,2),points(2,3)-points(2,2)];
end
angleInDegrees = atan2(vector(2), vector(1)) * 180 / pi; %supposed angle between the largest side and the x axis
end
end
需要明确的是:我得到一个视频并将其分割成帧,我需要在视频的所有帧中跟踪某个区域。我正在处理一个视频,但我将它分成帧,所以我真的在处理图像。我得到的矩形大部分时间都是倾斜的,即与我的图像的轴不对齐。