"Hierarchical MSER component tree" is a confusing phrase, because (1) the component tree is already hierarchical (2) if you want the whole tree, then you don't want only the Maximally Stable Extremal Regions (MSER), but instead you want all the extremal regions, and (3) extremal regions and components are the same thing in this context.
So let's say you want the extremal region tree. As noted in comments, you can't have exactly the one MATLAB uses because detectMSERFeatures.m
calls a mex function that we don't have source code for (though, based on its inputs and name, it is likely very similar to the openCV MSER function). But you can still compute your own extremal region tree. Basically what this code does is finds connected components in the image at various levels of thresholding. Those CCs are the extremal regions. The trickiest part of the code is recording the parent relations. This should get you started:
% 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
vl_mser
in the VLFeat toolbox... – Eitan TdetectMSERFeatures.m
. Additionally, you can check that Matlab uses a lot OpenCV code, so maybe Matlab hasn't got access to the intermediate info from OpenCV – phyroxdetectMSERFeatures.m
is just calling the mex functionocvExtractMSER
, which means that you aren't going to be able to do this just with MATLAB code. I agree with @RiddhimanDasgupta that looking for (or compiling yourself) a Mex version of the original OpenCV function with the output you want exposed is a good approach. – Tokkot