Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find outlines/ borders of label image in MATLAB

I was wondering if there’s an easy way to convert a label matrix into a matrix where you have lines anywhere two labeled regions meet and zeros elsewhere so that you could basically superimpose the borders of regions on the original image from which the labels were generated as another visualization alternative to the popular label2rgb function.

The reason I ask is that I’m currently working on with some superpixel code, so I have many labeled regions (500 to 5,000). I’ve been using rgblabel to convert the superpixel labels to colored regions, turning hold on, then displaying them over the original image with 'AlphaData' turned down to make them semi-transparent. However, with so many regions, this can be hard to analyze visually and I think simple borders of the regions would work better. Thanks.

[EDIT] @O_O: I've attached a sample label matrix along with the target result although I'm now quite satisfied with Jonas's second suggestion. Will try method from user616736 as well in the next day. I've also uploaded the sample images in .mat format here in case anyone else wants to experiment with them.

Label Matrix:

Label Matrix

Desired Result:

Desired Result

like image 824
SSilk Avatar asked Mar 10 '11 20:03

SSilk


People also ask

How does Bwlabel work in Matlab?

Description. L = bwlabel( BW ) returns the label matrix L that contains labels for the 8-connected objects found in BW . L = bwlabel( BW , conn ) returns a label matrix, where conn specifies the connectivity.

What is label matrix in Matlab?

A label matrix labels objects or connected components in a binary image with unique integer values. Use a label matrix to visualize distinct objects or connected components. example. L = labelmatrix( CC ) creates a label matrix, L , from the connected components structure CC returned by bwconncomp .


1 Answers

One way is to loop through all labels and eliminate all but the border, like this (where lblImg is your label matrix)

nLabels = max(lblImg(:));
for lbl = 1:nLabels
    currenObject = lblImg == lbl; %# find pixels belonging to current label
    lblImg(imerode(currentObject,strel('disk',1))) = 0; %# mask all but the border
end

imshow(label2rgb(lblImg))

EDIT

A faster method to find borders, is to use the gradient of the labeled image

[gx,gy] = gradient(lblImg);
lblImg((gx.^2+gy.^2)==0) = 0;

imshow(label2rgb(lblImg))
like image 88
Jonas Avatar answered Oct 29 '22 16:10

Jonas