在一个相关的问题(Huge broadcast variable, optimizing code without parfor?)中,我正在努力优化代码,以检测连接的组件(彼此相邻的具有相同值的元素组)的数量和大小。
代码的运行速度比以前更快,并显示如下:
FRAGMENTSIZESCLASS = cell(NumberOfClasses,1);
for class=1:NumberOfClasses
%-First we create a binary image for each class-%
BWclass = Raster==class;
%-Second we calculate the number of connected components (fragments) and the size of each fragment-%
CC = bwconncomp(BWclass);
numPixels = cellfun(@numel,CC.PixelIdxList);
FRAGMENTSIZESCLASS{class}=numPixels;
end
问题在于,现在我有了一组坐标(矩阵索引),我需要获取与每个坐标对关联的组件大小。如果Index = [1 1];和Raster = [5 5 8 2; 5 3 6 5];然后,我应该知道类“ 5”具有两个大小分别为3和1的簇(这将由上面的代码给出),并且坐标(1,1)对应于一个大小为3的簇。
在以前的代码版本中,您可以看到上面的链接,但是我在这里复制了它:
FRAGMENTSIZESCLASS = struct([]); %We store the data in a structure
for class=1:NumberOfClasses
%-First we create a binary image for each class-%
BWclass = Raster==class;
%-Second we calculate the number of connected components (fragments)-%
L = bwlabeln(BWclass); %returns a label matrix, L, containing labels for the connected components in BWclass
clear BWclass
NumberFragments=max(max(L));
%-Third we calculate the size of each fragment-%
FragmentSize=zeros(NumberFragments,1);
for f=1:NumberFragments % potential improvement: using parfor while saring the memory between workers
FragmentSize(f,1) = sum(L(:) == f);
end
FRAGMENTSIZESCLASS{class}=FragmentSize;
clear L
end
仅需查看L(1,1)= 1并且L中存在三个“ 1”值,就可以从矩阵“ L”获得信息,这意味着这些坐标的分量大小为3。
此版本代码的问题(如我在更新中上传的图片所示)是,它比新版本慢得多,以至于不可能在40000之前运行它80000个矩阵和数百万个坐标对。
有什么想法吗?