Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain Hierarchical component tree of MSER in Matlab?

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 MSERsMSER tree

Detected MSERs(left), MSER Tree(right)

like image 311
AruniRC Avatar asked Jan 28 '13 04:01

AruniRC


1 Answers

"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
like image 175
Tokkot Avatar answered Nov 16 '22 03:11

Tokkot