Mahotas – Distance from binary image

In this article we will see how we can obtain distance map of binary image in mahotas. A distance transform, also known as distance map or distance field, is a derived representation of a digital image. The choice of the term depends on the point of view on the object in question: whether the initial image is transformed into another representation, or it is simply endowed with an additional map or field. 
In order to do this we will use mahotas.distance method 
 
Syntax : mahotas.distance(img)
Argument : It takes image object which should be binary as argument
Return : It returns image object
Note : Input image should be binary image it can be labeled as well, image should be filtered or should be loaded as grey to make it binary
In order to filter the image we will take the image object which is numpy.ndarray and filter it with the help of indexing, below is the command to do this
 
image = image[:, :, 0]
Example 1: 
 
Python3
| # importing required librariesimportmahotas as mhimportnumpy as npfrompylab importimshow, show # creating region# numpy.ndarrayregions =np.zeros((10, 10), bool) # setting 1 value to the regionregions[:3, :3] =1regions[6:, 6:] =1 # getting labeled functionlabeled, nr_objects =mh.label(regions) # showing the image with interpolation = 'nearest'print("Image")imshow(labeled, interpolation ='nearest')show()# getting distance mapdmap =mahotas.distance(labeled)# showing imageprint("Distance Map")imshow(dmap)show() | 
Output : 
 
Example 2: 
 
Python3
| # importing required librariesimportnumpy as npimportmahotasfrompylab importimshow, show # loading imageimg =mahotas.imread('dog_image.png')   # filtering the imageimg =img[:, :, 0]    # setting gaussian filtergaussian =mahotas.gaussian_filter(img, 15) # setting threshold valuegaussian =(gaussian > gaussian.mean()) # creating a labeled imagelabeled, n_nucleus =mahotas.label(gaussian) print("Image")# showing the gaussian filterimshow(labeled)show()# getting distance mapdmap =mahotas.distance(labeled)# showing imageprint("Distance Map")imshow(dmap)show() | 
Output : 
 
 
				 
					



