- Mahotas Tutorial
- Mahotas - Home
- Mahotas - Introduction
- Mahotas - Computer Vision
- Mahotas - History
- Mahotas - Features
- Mahotas - Installation
- Mahotas Handling Images
- Mahotas - Handling Images
- Mahotas - Loading an Image
- Mahotas - Loading Image as Grey
- Mahotas - Displaying an Image
- Mahotas - Displaying Shape of an Image
- Mahotas - Saving an Image
- Mahotas - Centre of Mass of an Image
- Mahotas - Convolution of Image
- Mahotas - Creating RGB Image
- Mahotas - Euler Number of an Image
- Mahotas - Fraction of Zeros in an Image
- Mahotas - Getting Image Moments
- Mahotas - Local Maxima in an Image
- Mahotas - Image Ellipse Axes
- Mahotas - Image Stretch RGB
- Mahotas Color-Space Conversion
- Mahotas - Color-Space Conversion
- Mahotas - RGB to Gray Conversion
- Mahotas - RGB to LAB Conversion
- Mahotas - RGB to Sepia
- Mahotas - RGB to XYZ Conversion
- Mahotas - XYZ to LAB Conversion
- Mahotas - XYZ to RGB Conversion
- Mahotas - Increase Gamma Correction
- Mahotas - Stretching Gamma Correction
- Mahotas Labeled Image Functions
- Mahotas - Labeled Image Functions
- Mahotas - Labeling Images
- Mahotas - Filtering Regions
- Mahotas - Border Pixels
- Mahotas - Morphological Operations
- Mahotas - Morphological Operators
- Mahotas - Finding Image Mean
- Mahotas - Cropping an Image
- Mahotas - Eccentricity of an Image
- Mahotas - Overlaying Image
- Mahotas - Roundness of Image
- Mahotas - Resizing an Image
- Mahotas - Histogram of Image
- Mahotas - Dilating an Image
- Mahotas - Eroding Image
- Mahotas - Watershed
- Mahotas - Opening Process on Image
- Mahotas - Closing Process on Image
- Mahotas - Closing Holes in an Image
- Mahotas - Conditional Dilating Image
- Mahotas - Conditional Eroding Image
- Mahotas - Conditional Watershed of Image
- Mahotas - Local Minima in Image
- Mahotas - Regional Maxima of Image
- Mahotas - Regional Minima of Image
- Mahotas - Advanced Concepts
- Mahotas - Image Thresholding
- Mahotas - Setting Threshold
- Mahotas - Soft Threshold
- Mahotas - Bernsen Local Thresholding
- Mahotas - Wavelet Transforms
- Making Image Wavelet Center
- Mahotas - Distance Transform
- Mahotas - Polygon Utilities
- Mahotas - Local Binary Patterns
- Threshold Adjacency Statistics
- Mahotas - Haralic Features
- Weight of Labeled Region
- Mahotas - Zernike Features
- Mahotas - Zernike Moments
- Mahotas - Rank Filter
- Mahotas - 2D Laplacian Filter
- Mahotas - Majority Filter
- Mahotas - Mean Filter
- Mahotas - Median Filter
- Mahotas - Otsu's Method
- Mahotas - Gaussian Filtering
- Mahotas - Hit & Miss Transform
- Mahotas - Labeled Max Array
- Mahotas - Mean Value of Image
- Mahotas - SURF Dense Points
- Mahotas - SURF Integral
- Mahotas - Haar Transform
- Highlighting Image Maxima
- Computing Linear Binary Patterns
- Getting Border of Labels
- Reversing Haar Transform
- Riddler-Calvard Method
- Sizes of Labelled Region
- Mahotas - Template Matching
- Speeded-Up Robust Features
- Removing Bordered Labelled
- Mahotas - Daubechies Wavelet
- Mahotas - Sobel Edge Detection
Mahotas - Conditional Dilating Image
In our previous chapter, we explored the concept of image dilation, an operation used to expand all the pixels to the boundaries of regions in an image. Conditional dilation on the other hand, expands the pixels of certain specific regions, based on some conditions.
The condition can be based on the intensity values of the image pixels or some specific pattern in the image.
For example, let's consider a grayscale image. Instead of dilating all foreground pixels, you could conditionally dilate only those pixels that meet a certain intensity threshold.
If a pixel's intensity is above a predefined threshold, then dilation is applied; otherwise, the pixel remains unchanged.
Conditional Dilating Image in Mahotas
In mahotas, conditional dilation is an extension of the traditional dilation operation that comprises a condition based on a second image, often referred to as the "marker image."
It allows you to control the dilation process such that dilation only occurs at locations where the marker image has specific pixel values.
We can perform the conditional dilation on an image in mahotas using the cdilate() function. This function restricts the dilation process to specific regions based on the marker image's pixel values.
The mahotas.cdilate() function
The cdilate() function in Mahotas takes two inputs − the input image and a mask (condition) array. It performs conditional dilation on the input image based on the provided condition, and it returns the resulting dilated image.
Masks are used to identify specific regions within an image. They act as a filter that highlights certain areas while disregarding others.
Syntax
Following is the basic syntax of the cdilate() function −
mahotas.cdilate(f, g, Bc={3x3 cross}, n=1)
Where,
f − It is the input image on which the conditional dilation is to be performed.
g − It is the mask to be applied during conditional dilation.
Bc = {3×3 cross} (optional) − It is a structuring element used for dilation. Default is {3×3 cross}.
n − It is the number of iterations of the dilation operation. By default, it is set to 1, indicating a single iteration.
Example
In the following example, we are performing the conditional dilation on an image by scaling up its pixel intensity −
import mahotas as mh import numpy as np import matplotlib.pyplot as plt image= mh.imread('nature.jpeg') g = image * 2 conditional_dilated_image = mh.cdilate(image, g) # Create a figure with subplots fig, axes = plt.subplots(1, 2, figsize=(7,5 )) # Display the original image axes[0].imshow(image) axes[0].set_title('Original Image') axes[0].axis('off') # Display the conditional dilated image axes[1].imshow(conditional_dilated_image, cmap='gray') axes[1].set_title('Conditional Dilated Image') axes[1].axis('off') # Adjust the layout and display the plot plt.tight_layout() plt.show()
Output
After executing the above code, we get the following output −
Using Structuring Element
To perform conditional dilation using structuring element in Mahotas, first, we need to define the condition based on which the dilation will be applied. For example, you can specify a condition based on pixel intensity or provide a binary mask.
Next, choose a structuring element that defines the neighborhood for dilation. Mahotas provides several pre−defined structuring elements, such as disks and squares, which you can select based on the desired shape and size.
Finally, retrieve the resulting image, which will contain the dilation effects only where the condition is satisfied.
Example
In here, we are trying to perform conditional dilation on an image using structuring elements −
import mahotas as mh import numpy as np import matplotlib.pyplot as plt image= mh.imread('nature.jpeg', as_grey = True).astype(np.uint8) # Define the condition based on pixel intensity condition = image > 100 # Define a structuring element for dilation structuring_element = mh.disk(5) conditional_dilated_image = mh.cdilate(image, condition, structuring_element) # Create a figure with subplots fig, axes = plt.subplots(1, 2, figsize=(7,5 )) # Display the original image axes[0].imshow(image) axes[0].set_title('Original Image') axes[0].axis('off') # Display the conditional dilated image axes[1].imshow(conditional_dilated_image, cmap='gray') axes[1].set_title('Conditional Dilated Image') axes[1].axis('off') # Adjust the layout and display the plot plt.tight_layout() plt.show()
Output
The output obtained is as shown below −
Using a Custom Condition Function
We can also perform conditional dilation on an image using a custom condition function.
To achieve this, we first define a custom condition function that determines which pixels should undergo dilation.
Next, choose a structuring element to define the shape and size of dilation operation.
Finally, perform the conditional dilation and retrieve the dilated image.
Example
Now, we are dilating an image using a custom condition function −
import mahotas as mh import numpy as np import matplotlib.pyplot as plt # Load image image = mh.imread('sea.bmp', as_grey=True).astype(np.uint8) # Define a custom condition function def custom_condition(pixel_value): return pixel_value > 100 # Define a structuring element structuring_element = mh.disk(5) # Create a binary mask based on the custom condition function condition = custom_condition(image) # Perform conditional dilation conditional_dilated_image = mh.cdilate(image, condition, structuring_element) # Create a figure with subplots fig, axes = plt.subplots(1, 2, figsize=(7,5 )) # Display the original image axes[0].imshow(image) axes[0].set_title('Original Image') axes[0].axis('off') # Display the conditional dilated image axes[1].imshow(conditional_dilated_image, cmap='gray') axes[1].set_title('Conditional Dilated Image') axes[1].axis('off') # Adjust the layout and display the plot plt.tight_layout() plt.show()
Output
Following is the output of the above code −