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.

Now we get labelmatrix
bw = im2bw(img, graythresh(img)); cc = bwconncomp(bw); lblMatrix = labelmatrix(cc); imshow(lblMatrix)
lblMatrix as follows

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)

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.
abcd
source share