Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient segment boundary marking after segmentation of an image

One can mark the boundary of a binary image by bwboundaries function of MATLAB.

What should be done for obtaining boundaries of all segments as a binary image?

I have segmented an image and want to know if there is a way to mark boundaries between each neighbouring segment without applying morphological operations on each segment.

I have added images to illustrate what i want to do. Actually i want to obtain a binary image that keeps pink boundary marker pixels between all segments. Thus, I can overlay them with original image by the help of imoverlay function of Steve Eddins.

Random colored labeling of segmentation result:

enter image description here

Roughly-marked pink boundaries between segments:

enter image description here

like image 994
Gulcan Avatar asked Jan 17 '23 15:01

Gulcan


2 Answers

You can find the region boundaries using a range filter, which finds the intensity range within each pixel's neighborhood. This takes advantage of the fact that the label matrix only has non-zero range at the region boundaries.

im = imread('http://i.stack.imgur.com/qPiA3.png');
boundaries = rangefilt(im,ones(3)) > 0;
imoverlay(label2rgb(im),boundaries,[0 0 0]);

These edges are also two pixels wide. Actually, I think the edges have to be two pixels wide; otherwise the regions will "lose" pixels to the border non-uniformly.

like image 100
reve_etrange Avatar answered Feb 24 '23 13:02

reve_etrange


Since erosion and dilation work on non-binary images as well, you can write

img = imread('http://i.stack.imgur.com/qPiA3.png');
ei = imerode(img,ones(3));
di = imdilate(img,ones(3));
boundaries = ei~=img | di~=img;

This results in a bw image that has a boundary at the edge of each colored region (thus, the boundary line will be two pixels wide).

enter image description here

Note that this will not return an ordered list of pixels as bwboundaries, but rather a logical mask like bwperim, which is what imoverlay needs as input.

like image 28
Jonas Avatar answered Feb 24 '23 14:02

Jonas