从分割输出构建训练图像的最有效方法是什么



我想从分水岭变换输出中生成训练图像,以便在每个单元中都有一个图像片段。我怎样才能以最有效的方式做到这一点?

更多详细信息:假设L是分水岭分割的输出:

L =
 1     1     2
 1     0     2
 1     2     2
 0     0     2
  • 0是背景
  • 1是段编号1,2是段编号2,依此类推

我想构建一个有两个图像的细胞,每个图像都包含一个片段:

cell1=
1     1 
1     0 
1     0 
cell2=
0     2 
0     2 
2     2
0     2 

我知道我可以用一些for循环和if条件来做这件事,但我需要有一个计算成本最高的解决方案。也许Matlab有一个内置的函数来完成这个任务?

可以使用以下一个linner;-)

U = regionprops(L, 'Image')

解决方案之间的比较(L是1200x1600像素的图像):

>> tic;
for index=1:100
U = regionprops(L, 'Image');
end
toc;

运行时间为20.138794秒。

>>tic;
for index=1:100
N = max(L(:)); %//number of segments
C = cell(N,1); %//create Cell Array
[height, width] = size(L); %//get dimensions of image
for target=1:N %//for each segment..
    %//search column-wise to get first and last column index
    col_start = ceil(find(L==target,1)/height);
    col_end = ceil(find(L==target,1,'last')/height);
    %//search row-wise to get first and last row index
    row_start = ceil(find(L.'==target,1)/width);
    row_end = ceil(find(L.'==target,1,'last')/width);
    T = L(row_start:row_end , col_start:col_end); %//image segment of bounding box
    T(T~=target) = 0; %//set non-targets to 0
    C{target} = T; %//add to cell array
end;
end
toc;

运行时间为300.744868秒。

>> tic;
for index=1:100
u = unique(L(:));
B = arrayfun(@(x) removePadding(L, x)*2, u(2:end), 'UniformOutput', false);
end
toc;

运行时间为182.193148秒。

我在这里写了一个干净/简短的解决方案,但我不知道它是比Lincoln的更快还是更慢。只需尝试使用tic/toc

function A = removePadding(L, x) 
  A = (L==x); 
  A(all(A == 0, 2), :)=[]; 
  A(:, all(A == 0, 1))=[]; 
end
L = [1 1 2;1 0 2; 1 2 2; 0 0 2];
u = unique(L(:))
arrayfun(@(x) removePadding(L, x)*2, u(2:end), 'UniformOutput', false)

将输出:

ans =
{
  [1,1] =
     1   1
     1   0
     1   0
  [2,1] =
     0   2
     0   2
     2   2
     0   2
}

注意:函数removePadding将删除所有只包含零的行/列。这意味着,如果一个区域不连接,它将不起作用,因为中间行/列也将被删除。但我认为在您的情况下不会发生这种情况,因为如果区域完全连接,分水岭(IMO)只会返回相同的区域索引(例如,区域1为1)。


快速测试:首先,定义了L和我的函数。现在测试:

>> tic; 
for i = 1:1000
  u = unique(L(:));
  B = arrayfun(@(x) removePadding(L, x)*2, u(2:end), 'UniformOutput', false);
end
>> toc
Elapsed time is 4.89563 seconds.

现在,您可以复制这个测试片段并对其进行修改,以检查Lincolns计算的速度。

编辑2:我将Lincolns解决方案定义为C = myFun(L),然后再次运行速度测试:

>> tic;
>> for i = 1:1000
  B = myFun(L);
end
>> toc
Elapsed time is 1.01026 seconds.

看起来要快得多:-)尽管使用了for循环。

既然您要求一种有效的方法,我认为下面的解决方案应该可以很好地工作。尽管它使用1作为循环,但它只循环N次,其中N是分水岭变换输出中的number of segments,这对于图像分割来说通常非常低(对于您的示例,N=2)。

N = max(L(:)); %//number of segments
C = cell(N,1); %//create Cell Array
[height, width] = size(L); %//get dimensions of image
for target=1:N %//for each segment..
    %//search column-wise to get first and last column index
    col_start = ceil(find(L==target,1)/height);
    col_end = ceil(find(L==target,1,'last')/height);
    %//search row-wise to get first and last row index
    row_start = ceil(find(L.'==target,1)/width);
    row_end = ceil(find(L.'==target,1,'last')/width);
    T = L(row_start:row_end , col_start:col_end); %//image segment of bounding box
    T(T~=target) = 0; %//set non-targets to 0
    C{target} = T; %//add to cell array
end

最新更新