Find contours / borders of image tags in MATLAB

I was wondering if theres an easy way to convert the label matrix to a matrix where you have lines where two marked areas meet and zeros elsewhere so that you can basically overlay the borders of the areas on the original image from which the labels were made created as another alternative to visualizing the popular label2rgb function.

The reason I'm asking is because Im is currently working on some super pixel code, so I have a lot of areas with labels (from 500 to 5000). I used rgblabel to convert lettering to colored areas by rotating and then displaying them on top of the original image with AlphaData turned off to make them translucent. However, with so many regions, it can be difficult to analyze visually, and I think that the simple boundaries of the regions will work better. Thanks.

[EDIT] @O_O: I attached a sample label matrix along with the target result, although now I am completely satisfied with Jonas second suggestion. He will try the method from user616736, as well as the next day. I also uploaded sample images in .mat format here if someone wants to experiment with them.

Tag Matrix:

Label Matrix

Desired Result:

Desired result

+8
image-processing matlab label
source share
3 answers

One way is to skip all the labels and exclude everything except the border like this (where lblImg is your shortcut 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 way to find borders is to use the gradient of the marked image

 [gx,gy] = gradient(lblImg); lblImg((gx.^2+gy.^2)==0) = 0; imshow(label2rgb(lblImg)) 
+7
source share

If you have access to the image processing toolbar (I assume you are doing this since you are dealing with matrix labels), you can use the edge function. Here is a simple example.

 img = imread('rice.png'); imshow(img) 

rice.png is a Matlab stock image, so you can run this code on your computer. The image looks like this.

enter image description here

Now we get labelmatrix

 bw = im2bw(img, graythresh(img)); cc = bwconncomp(bw); lblMatrix = labelmatrix(cc); imshow(lblMatrix) 

lblMatrix as follows

enter image description here

Now we get the edges of the label matrix. Here I used the Laplacian of the Gauss method, but you can choose any other algorithm (see Help for more)

 edgeMatrix=edges(lblMatrix,'log',0); imshow(edgeMatrix) 

enter image description here

Here all the edges are greater than 0 , which is what you need. Then you can manipulate this, but you want in your processing and overlay on top of other shapes. In practice, you need something a little above zero, so you won’t get these little circles (which are just due to noise) and only restore what you want. You can mess around and set the threshold for something else until you get it just right. Although this can be automated, I cannot say much without knowing the actual problem. In any case, this is only for you to start in the right direction.

+5
source share

Quick Track:

I also asked this question on the blog of Steve Eddins, and he suggested a very quick way to find the edges of the image tags:

 region_borders = imdilate(lblImg,ones(3,3)) > imerode(lblImg,ones(3,3)); 

I like this option because my main reason for finding borders is for visualization purposes, and it gives slightly thicker borders. In addition, its imoverlay function is a convenient way to view the borders that you found on top of the original image from which the tags were generated,

+1
source share

All Articles