使用Matlab 2018。
我有一组图像,其中绘制了大量ROI。对于图片,还定义了一个位掩码。我想找到位掩码为1的每个ROI的部分(分数或百分比)。以下代码可以工作,但是速度很慢。 (对于8张图像大约需要1分钟,每张图像的投资回报率为80)。必须有更聪明的方法来做到这一点。
数据是这样提供的:我感兴趣的图像部分位于两个“环”之间。这些环是手动绘制的。对于每个环,依次记录80点,顺时针旋转。
内圈的数据存储为InnerX(n,Im),InnerY(n,Im),其中n表示点的编号,Im是图像编号(多张图像的数据在同一位置处理时间)。
OuterX和OuterY相似。每个ROI由内环中的两个连续点和外环中的两个相应点定义。
Score = zeros(size(InnerX));
%Loop over the different images
for i = 1:size(InnerX, 2)
%Loop over all points in this ring
%The points are ordered, so that consecutive points in Inner
%and Outer ring form a ROI
for n = 1:size(InnerX,1)
%if statement to take care of the last point
if(n == size(InnerX, 1))
X = [InnerX(n,i), InnerX(1,i) OuterX(1,i) OuterX(n,i)];
Y = [InnerY(n,i), InnerY(1,i) OuterY(1,i) OuterY(n,i)];
else
X = [InnerX(n,i), InnerX(n+1,i) OuterX(n+1,i) OuterX(n,i)];
Y = [InnerY(n,i), InnerY(n+1,i) OuterY(n+1,i) OuterY(n,i)];
end
%Calculate the integer numbers of the range of all pixels that could
%potentially overlap
xmin = floor(min(X));
ymin = floor(min(Y));
xmax = floor(max(X));
ymax = floor(max(Y));
%Create shape
TotalArea = polyshape(X, Y);
%Loop over all pixels within the range
%For each pixel calculate the overlap with the ROI
%Multiply with the bitmask score for that pixel (0 or 1)
for x = xmin:xmax
for y = ymin:ymax
ROI = polyshape([x x x+1 x+1], [y y+1 y+1 y]);
polyout = intersect(TotalArea, ROI);
Score(n,i) = Score(n,i) + Bitmap(y,x,i) .* polyout.area;
end
end
%Divide by the area of the ROI
Score(n,i) = Score(n,i) / TotalArea.area;
end
end
The results coming out of this seem accurate (correct shape, all numbers between 0 and 1), but as said, this code is rather slow.
Thanks in advance.