“分层 MSER 组件树”是一个令人困惑的短语,因为 (1) 组件树已经是分层的 (2) 如果您想要整个树,那么您不仅想要最大稳定极值区域 (MSER),而是您想要所有的极值区域,并且(3)极值区域和组件在这种情况下是相同的。
因此,假设您想要极值区域树。如评论中所述,您不能完全使用 MATLAB,因为它detectMSERFeatures.m
调用了一个我们没有源代码的 mex 函数(不过,根据它的输入和名称,它可能与 openCV MSER 函数非常相似) . 但是您仍然可以计算自己的极值区域树。基本上,这段代码所做的是在图像中以各种阈值级别查找连接的组件。这些CC是极值区域。代码中最棘手的部分是记录父关系。这应该让你开始:
% input image, included with MATLAB
x = imread('rice.png');
pixelList = {};
parents = [];
oldERsLabeled = zeros(size(x));
oldPixelList = {};
regionLabelOffset = 0;
for i = 255:-10:1 % the stride here is important, smaller will be slower and give more regions
newERs = bwlabel(x > i);
newERsLabeled = zeros(size(newERs));
newERsLabeled(newERs > 0) = newERs(newERs > 0) + regionLabelOffset;
regionLabelOffset = max(newERsLabeled(:));
% update the list of regions
props = regionprops(newERs, 'pixelList');
newPixelList = {props(:).PixelList};
pixelList = [pixelList newPixelList];
% figure out parents
newParents = cellfun(@(c)(newERsLabeled( sub2ind(size(x), c(1,2), c(1,1)))), oldPixelList);
parents = [parents; newParents'];
oldPixelList = newPixelList;
oldERsLabeled = newERsLabeled;
end
parents(end+1 : length(pixelList)) = -1; % top level regions have no parents
pixelListInt = cellfun(@int32, pixelList, 'UniformOutput', false);
regions = MSERRegions(pixelListInt');
% plot the first 300 regions
figure
imshow(x)
hold on
plot(regions(1:300), 'showEllipses', false, 'showPixelList', true);
% show all parents of a region ("close all" command might be useful after)
curRegion = 102;
while curRegion ~= -1
figure
imshow(x)
hold on
plot(regions(curRegion), 'showEllipses', false, 'showPixelList', true);
curRegion = parents(curRegion);
end