Masking
- ultrasound_processing.masking.compute_threshold(image: ndarray, top_percent: float) int[source]
Compute the intensity threshold such that the top top_percent fraction of pixel values in the grayscale image will be set to white in the binary mask.
- Parameters:
image – 2D numpy array of grayscale pixel intensities (0-255).
top_percent – Number between 0 and 1 that determines the fraction of pixels to be set to white.
- Returns:
An integer threshold value in the range [0, 255].
- ultrasound_processing.masking.dilate_contour(contour: ndarray, kernel_size: Tuple[int, int] = (2, 2), iterations: int = 1) ndarray[source]
Thicken the one- pixel-wide contour by applying dilation.
- Parameters:
contour – Binary contour image (0 or 255) with single-pixel lines.
kernel_size – Size of the rectangular structuring element.
iterations – Number of dilation iterations.
- Returns:
The dilated contour mask.
- ultrasound_processing.masking.do_closing(mask: ndarray, kernel_size: Tuple[int, int] = (2, 2)) ndarray[source]
Apply a morphological closing operation (dilation followed by erosion) to fill small holes and connect nearby white regions in the binary mask.
- Parameters:
mask – Binary mask (0 or 255) to be processed.
kernel_size – Size of the structuring element used for closing.
- Returns:
The mask after morphological closing.
- ultrasound_processing.masking.extract_top_contour(mask: ndarray) ndarray[source]
Extract the top contour of the white regions in the binary mask by keeping only the first white pixel in each column.
- Parameters:
mask – Cleaned binary mask (0 or 255).
- Returns:
A binary image where each column has at most one white pixel.
- ultrasound_processing.masking.mask(image: ndarray, top_percent: float = 0.93, top_margin: int = 5, apply_closing_flag: bool = True) ndarray[source]
Complete pipeline that generates a masked ultrasound image that keeps only the contour of a certain object.
- Parameters:
image – Input 2D grayscale image.
top_percent – Fraction of pixels to threshold as white (default 0.93).
top_margin – Pixel row margin to remove top noise components (default 5).
apply_closing_flag – Whether to perform morphological closing (default True).
- Returns:
A grayscale image where only the contour region retains its original intensities.
- ultrasound_processing.masking.original_intensity_mask(image: ndarray, smooth: ndarray) ndarray[source]
Preserve the original grayscale intensities.
- Parameters:
image – Original 2D grayscale image.
smooth – Smooth mask with values in [0, 255].
- Returns:
Masked image where the contour regions preserve their original intensities.
- ultrasound_processing.masking.remove_small_components(mask: ndarray, top_margin: int) ndarray[source]
Remove white components (noise) from the top part of the image.
- Parameters:
mask – Binary mask (0 or 255) (after closing).
top_margin – Number of pixels from the top; any component starting above this line will be removed.
- Returns:
The mask with small top components removed.
- ultrasound_processing.masking.smooth_mask(dilated: ndarray, blur_ksize: Tuple[int, int] = (5, 5)) ndarray[source]
Apply a Gaussian blur to the dilated contour mask.
- Parameters:
dilated – Dilated binary contour mask.
blur_ksize – Kernel size for Gaussian blur.
- Returns:
A smooth mask with values in [0, 255].
- ultrasound_processing.masking.threshold_mask(image: ndarray, threshold_value: int) ndarray[source]
Create a binary mask by thresholding the image: pixels with intensity >= threshold_value become 255 (white), others become 0 (black).
- Parameters:
image – 2D numpy array of grayscale pixel intensities.
threshold_value – Intensity cutoff for binarization.
- Returns:
A binary mask as a 2D numpy array of 0s and 255s.
- ultrasound_processing.masking.mask(image: ndarray, top_percent: float = 0.93, top_margin: int = 5, apply_closing_flag: bool = True) ndarray[source]
Complete pipeline that generates a masked ultrasound image that keeps only the contour of a certain object.
- Parameters:
image – Input 2D grayscale image.
top_percent – Fraction of pixels to threshold as white (default 0.93).
top_margin – Pixel row margin to remove top noise components (default 5).
apply_closing_flag – Whether to perform morphological closing (default True).
- Returns:
A grayscale image where only the contour region retains its original intensities.
- ultrasound_processing.masking.compute_threshold(image: ndarray, top_percent: float) int[source]
Compute the intensity threshold such that the top top_percent fraction of pixel values in the grayscale image will be set to white in the binary mask.
- Parameters:
image – 2D numpy array of grayscale pixel intensities (0-255).
top_percent – Number between 0 and 1 that determines the fraction of pixels to be set to white.
- Returns:
An integer threshold value in the range [0, 255].
- ultrasound_processing.masking.threshold_mask(image: ndarray, threshold_value: int) ndarray[source]
Create a binary mask by thresholding the image: pixels with intensity >= threshold_value become 255 (white), others become 0 (black).
- Parameters:
image – 2D numpy array of grayscale pixel intensities.
threshold_value – Intensity cutoff for binarization.
- Returns:
A binary mask as a 2D numpy array of 0s and 255s.
- ultrasound_processing.masking.do_closing(mask: ndarray, kernel_size: Tuple[int, int] = (2, 2)) ndarray[source]
Apply a morphological closing operation (dilation followed by erosion) to fill small holes and connect nearby white regions in the binary mask.
- Parameters:
mask – Binary mask (0 or 255) to be processed.
kernel_size – Size of the structuring element used for closing.
- Returns:
The mask after morphological closing.
- ultrasound_processing.masking.remove_small_components(mask: ndarray, top_margin: int) ndarray[source]
Remove white components (noise) from the top part of the image.
- Parameters:
mask – Binary mask (0 or 255) (after closing).
top_margin – Number of pixels from the top; any component starting above this line will be removed.
- Returns:
The mask with small top components removed.
- ultrasound_processing.masking.extract_top_contour(mask: ndarray) ndarray[source]
Extract the top contour of the white regions in the binary mask by keeping only the first white pixel in each column.
- Parameters:
mask – Cleaned binary mask (0 or 255).
- Returns:
A binary image where each column has at most one white pixel.
- ultrasound_processing.masking.dilate_contour(contour: ndarray, kernel_size: Tuple[int, int] = (2, 2), iterations: int = 1) ndarray[source]
Thicken the one- pixel-wide contour by applying dilation.
- Parameters:
contour – Binary contour image (0 or 255) with single-pixel lines.
kernel_size – Size of the rectangular structuring element.
iterations – Number of dilation iterations.
- Returns:
The dilated contour mask.
- ultrasound_processing.masking.smooth_mask(dilated: ndarray, blur_ksize: Tuple[int, int] = (5, 5)) ndarray[source]
Apply a Gaussian blur to the dilated contour mask.
- Parameters:
dilated – Dilated binary contour mask.
blur_ksize – Kernel size for Gaussian blur.
- Returns:
A smooth mask with values in [0, 255].
- ultrasound_processing.masking.original_intensity_mask(image: ndarray, smooth: ndarray) ndarray[source]
Preserve the original grayscale intensities.
- Parameters:
image – Original 2D grayscale image.
smooth – Smooth mask with values in [0, 255].
- Returns:
Masked image where the contour regions preserve their original intensities.