Related Articles

Related Articles

Mahotas – Distance from binary image
  • Last Updated : 30 Jun, 2020

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing required libraries
import mahotas as mh
import numpy as np
from pylab import imshow, show
   
# creating region
# numpy.ndarray
regions = np.zeros((10, 10), bool)
   
# setting 1 value to the region
regions[:3, :3] = 1
regions[6:, 6:] = 1
   
# getting labeled function
labeled, nr_objects = mh.label(regions)
   
# showing the image with interpolation = 'nearest'
print("Image")
imshow(labeled, interpolation ='nearest')
show()
  
# getting distance map
dmap = mahotas.distance(labeled)
  
# showing image
print("Distance Map")
imshow(dmap)
show()

chevron_right


Output :

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing required libraries
import numpy as np
import mahotas
from pylab import imshow, show
   
# loading iamge
img = mahotas.imread('dog_image.png')
     
# filtering the imagwe
img = img[:, :, 0]
      
# setting gaussian filter
gaussian = mahotas.gaussian_filter(img, 15)
   
# setting threshold value
gaussian = (gaussian > gaussian.mean())
   
# creating a labeled image
labeled, n_nucleus = mahotas.label(gaussian)
   
  
print("Image")
# showing the gaussian filter
imshow(labeled)
show()
  
  
# getting distance map
dmap = mahotas.distance(labeled)
  
# showing image
print("Distance Map")
imshow(dmap)
show()

chevron_right


Output :

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :