maad.rois.select_rois
- maad.rois.select_rois(im_bin, min_roi=None, max_roi=None, verbose=False, display=False, savefig=None, **kwargs)[source]
Select regions of interest based on its dimensions.
The input is a binary mask, and the output is an image with labelled pixels.
- Parameters:
- im2d ndarray of scalars
Spectrogram (or image)
- min_roi, max_roiscalars, optional, defaultNone
Define the minimum and the maximum area possible for an ROI. If None, the minimum ROI area is 1 pixel and the maximum ROI area is the area of the image
- 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 plt.plot and savefig functions
- savefilenamestr, optional, default :’_spectro_after_noise_subtraction.png’
Postfix of the figure filename
- figsizetuple of integers, optional, default: (4,10)
width, height in inches.
- titlestring, optional, default‘Spectrogram’
title of the figure
- xlabelstring, optional, default‘Time [s]’
label of the horizontal axis
- ylabelstring, optional, default‘Amplitude [AU]’
label of the vertical axis
- cmapstring or Colormap object, optional, default is ‘gray’
See https://matplotlib.org/examples/color/colormaps_reference.html in order to get all the existing colormaps examples: ‘hsv’, ‘hot’, ‘bone’, ‘tab20c’, ‘jet’, ‘seismic’, ‘viridis’…
- vmin, vmaxscalar, optional, default: None
vmin and vmax are used in conjunction with norm to normalize luminance data. Note if you pass a norm instance, your settings for vmin and vmax will be ignored.
- extentscalars (left, right, bottom, top), optional, default: None
The location, in data-coordinates, of the lower-left and upper-right corners. If None, the image is positioned such that the pixel centers fall on zero-based (row, column) indices.
- dpiinteger, optional, default is 96
Dot per inch. For printed version, choose high dpi (i.e. dpi=300) => slow For screen version, choose low dpi (i.e. dpi=96) => fast
- formatstring, optional, default is ‘png’
Format to save the figure
… and more, see matplotlib
- Returns:
- im_rois: 2d ndarray
image with labels as values
- rois: pandas DataFrame
Regions of interest with future descriptors will be computed. Array have column names:
labelID
,label
,min_y
,min_x
,max_y
,max_x
, Use the functionmaad.util.format_features
before using centroid_features to format of therois
DataFrame correctly.
Examples
>>> import maad
Load audio recording compute the spectrogram in dB.
>>> s, fs = maad.sound.load('../data/cold_forest_daylight.wav') >>> Sxx,tn,fn,ext = maad.sound.spectrogram (s, fs, flims=(0,20000), display=True) >>> Sxx_dB = maad.util.power2dB(Sxx) +96
Smooth the spectrogram
>>> Sxx_dB_blurred = maad.sound.smooth(Sxx_dB)
Using image binarization, detect isolated region in the time-frequency domain with high density of energy, i.e. regions of interest (ROIs).
>>> im_bin = maad.rois.create_mask(Sxx_dB_blurred, bin_std=1.5, bin_per=0.5, mode='relative')
Select ROIs from the binary mask.
>>> im_rois, df_rois = maad.rois.select_rois(im_bin, display=True) >>> print('Without subtracted the background noise, only {} ROIs have been segmented'.format(len(df_rois))) Without subtracted the background noise, only 13 ROIs have been segmented >>> df_rois.iloc[-1] labelID 13 label unknown min_y 162 min_x 1105 max_y 193 max_x 1127 Name: 12, dtype: object
We detected the background noise as a ROI, and that multiple ROIs are mixed in a single region. To have better results, it is adviced to preprocess the spectrogram to remove the background noise before creating the mask.
>>> Sxx_noNoise = maad.sound.median_equalizer(Sxx) >>> Sxx_noNoise_dB = maad.util.power2dB(Sxx_noNoise) >>> Sxx_noNoise_dB_blurred = maad.sound.smooth(Sxx_noNoise_dB) >>> im_bin2 = maad.rois.create_mask(Sxx_noNoise_dB_blurred, bin_std=6, bin_per=0.5, mode='relative') >>> im_rois2, df_rois2 = maad.rois.select_rois(im_bin2, display=True) >>> print('{} ROIs have been segmented'.format(len(df_rois2))) 274 ROIs have been segmented >>> df_rois2.iloc[-1] labelID 274 label unknown min_y 214 min_x 4485 max_y 230 max_x 4494 Name: 273, dtype: object