11

Maximally Stable Extremal Regions (MSERs) are found from an image in Matlab using detectMSERFeatures.

Is there any patch or method to get the hierarchical MSER component tree from Matlab?

This tree is anyways being generated when Matlab calculates the regions - it returns only the most "stable" component from each region's tree. Since this tree is already present, I am searching for ways to expose this to user code from the Matlab libraries, which keeps this part hidden and provides only the final "maximally stable" regions.

Anything would be acceptable - modifications of the Matlab inbuilt code, patches, hacks whatever. (I realize OpenCV has such a patch, however I am trying to avoid porting to OpenCV as most of the other procedures are written in Matlab).

EDIT: (from the original hierarchical MSER paper)

detected MSERs MSER tree

Detected MSERs(left), MSER Tree(right)

Dima
  • 38,860
  • 14
  • 75
  • 115
AruniRC
  • 5,070
  • 7
  • 43
  • 73
  • 1
    You could try using OpenCV Mex functions and simply call the OpenCV functions from inside Matlab. Several Mex implementations of OpenCV for Matab are available. However, it is unlikely that the patch for the default OpenCV MSER function has a Mex. You could try compiling one yourself in that case. – Riddhiman Dasgupta Jan 28 '13 at 06:42
  • You could also try looking into the implementation of [`vl_mser`](http://www.vlfeat.org/mdoc/vl_mser.html) in the [VLFeat toolbox](http://www.vlfeat.org/)... – Eitan T Jan 30 '13 at 12:04
  • You could try looking to the Matlab code `detectMSERFeatures.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 – phyrox Jan 27 '14 at 17:10
  • `detectMSERFeatures.m` is just calling the mex function `ocvExtractMSER`, 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 Feb 28 '14 at 21:19
  • Check out this link, someone made a patch. https://code.ros.org/trac/opencv/ticket/1577 – James Harper Mar 12 '14 at 02:48

1 Answers1

2

"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
Tokkot
  • 1,215
  • 7
  • 22