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 m开发者_开发问答ake 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:
Desired Result:
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))
If you have access to the image processing toolbox (I assume you do, since you're dealing with label matrices), you can use the edge
function. Here's a simple example
img = imread('rice.png');
imshow(img)
rice.png
is Matlab's stock image, so you can run this code on your machine. The image looks like this.
Now get labelmatrix
bw = im2bw(img, graythresh(img));
cc = bwconncomp(bw);
lblMatrix = labelmatrix(cc);
imshow(lblMatrix)
lblMatrix
looks like this
Now we get the edges of the label matrix. Here I've used the Laplacian of Gaussian method, but you can choose any other algorithm (see the help for more)
edgeMatrix=edges(lblMatrix,'log',0);
imshow(edgeMatrix)
This finds all edges that are larger than 0
, which is what you need. You can then manipulate this however you want in your processing and overlay on top of other figures. In practice, you need something slightly higher than zero, so that you don't get those little circles (which are just due to noise), and only recover what you want. You can tinker and adjust the threshold to something else, till you get it just right. Although this can be automated, I can't say much without knowing the actual problem. Anyway, this is just to get you started in the right direction.
Quick follow up:
I also posed this question on Steve Eddins' blog, and he offered a very quick way to find the edges of the label image:
region_borders = imdilate(lblImg,ones(3,3)) > imerode(lblImg,ones(3,3));
I like this option because my main reason for looking for the borders is for visualization purposes, and this gives somewhat thicker borders. Also, his function imoverlay
is a handy way to view the borders you've found on top of the original image that the labels were generated from.
精彩评论