maad.rois.create_mask

maad.rois.create_mask(im, mode_bin='relative', verbose=False, display=False, savefig=None, **kwargs)[source]

Binarize an image based on a double threshold.

Parameters:
im2d ndarray of scalars

Spectrogram (or image)

mode_binstring in {‘relative’, ‘absolute’}, optional, default is ‘relative’

if ‘absolute’ [1] , a double threshold with absolute value is performed with two parameters (see kwargs section) if ‘relative’ [2], a relative double threshold is performed with two parameters (see kwargs section)

verboseboolean, optional, default is False

print messages

displayboolean, optional, default is False

Display the signal if True

savefigstring, optional, default is None

Root filename (with full path) is required to save the figures. Postfix is added to the root filename.

kwargs, optional. This parameter is used by the maad functions as well

as the plt.plot and savefig functions. All the input arguments required or optional in the signature of the functions above can be passed as kwargs :

if ‘absolute’ [1] - bin_h : scalar, optional, default is 0.7 Set the first threshold. Value higher than this value are set to 1, the others are set to 0. They are the seeds for the second step - bin_l: scalar, optional, defautl is 0.2 Set the second threshold. Value higher than this value and connected to the seeds or to other pixels connected to the seeds (6-connectivity) are set to 1, the other remains 0

if ‘relative’ [2] : - bin_std : scalar, optional, default is 6 bin_std is needed to compute the threshold1. This threshold is not an absolute value but depends on values that are similar to 75th percentile (pseudo_mean) and a sort of std value of the image. threshold1 = “pseudo_mean” + “std” * bin_std Value higher than threshold1 are set to 1, they are the seeds for the second step. The others are set to 0. - bin_per: scalar, optional, defautl is 0.5 Set how much the second threshold is lower than the first threshold value. From 0 to 1. ex: 0.1 = 10 %. threshold2 = threshold1 (1-bin_per) Value higher than threshold2 and connected (6-connectivity) to the seeds are set to 1, the other remains 0

… and more, see matplotlib

Returns:
im_bin: binary image

References

[1] (1,2)
  1. Canny. A computational approach to edge detection. IEEE Transactions on Pattern Analysis and Machine Intelligence. 1986; vol. 8, pp.679-698. DOI: 10.1109/TPAMI.1986.4767851

[2] (1,2)

from MATLAB: Threshold estimation (Oliveira et al, 2015)

Examples

>>> import maad

Load audio recording and convert it into spectrogram

>>> s, fs = maad.sound.load('../data/cold_forest_daylight.wav')
>>> Sxx,tn,fn,ext = maad.sound.spectrogram (s, fs, fcrop=(0,10000))   

Convert linear spectrogram into dB

>>> Sxx_dB = maad.util.power2dB(Sxx) +96

Smooth the spectrogram

>>> Sxx_dB_blurred = maad.sound.smooth(Sxx_dB)

Detection of the acoustic signature => creation of a mask

>>> im_bin = maad.rois.create_mask(Sxx_dB_blurred, bin_std=1.5, bin_per=0.25, mode='relative') 

Plot spectrograms

>>> import matplotlib.pyplot as plt 
>>> fig, (ax1, ax2) = plt.subplots(2, 1)
>>> ax1, fig = maad.util.plot2d(Sxx_dB, ax=ax1, extent=ext, title='original', vmin=10, vmax=70)
>>> ax2, fig = maad.util.plot2d(im_bin, ax=ax2, extent=ext, title='mask')
>>> fig.set_size_inches(13,8)
>>> fig.tight_layout()