Mahotas – Dilating an Image Dilating an image in image processing refers to expanding the pixels of an image. The dilation process will add pixels around the edges of an image. This happens because the algorithm looks at each pixel in the image and checks its neighboring pixels. If any of the neighboring pixels are part of an object, it adds those pixels to the object”s boundary. Dilating an Image in Mahotas By dilating an image in Mahotas, we refer to adding the number of pixels to the boundaries of regions in an image. This operation is commonly used to enhance or modify the shapes and structures in an image. We can dilate an image in mahotas using the dilate() function. It is used to expand an element A by using structuring element B. A structuring element is a small matrix or shape that defines the neighborhood around each pixel. It is used to determine which pixels should be considered during the dilation process. The mahotas.dilate() function The mahotas.dilate() function takes the input image and the structuring element as arguments, and returns a new Numpy array. The value of the output pixel is determined as the maximum value of all the pixels in the neighborhood. If any neighboring pixels have a value of 1, the output pixel is set to 1. The dilate() function scans the image, pixel by pixel, and checks the neighborhood defined by the structuring element. If any neighboring pixels are part of an object, the dilation operation adds those pixels to the object”s boundary, making it larger. Syntax Following is the basic syntax of the dilate() function in mahotas − mahotas.dilate(A, Bc=None, out=None, output=None) where, A − It is the input image on which dilation will be performed. It can be a 2D or 3D NumPy array representing grayscale or binary image data. Bc (optional) − It is the structuring element used for dilation. Default is None. out (deprecated) / output (optional) − It specifies the output array to store the result. If not provided, a new array is created and returned as the output. Example Following is the basic example to dilate an image in mahotas using the dilate() function − import mahotas as mh import matplotlib.pyplot as plt import numpy as np image = mh.imread(”nature.jpeg”, as_grey=True).astype(np.uint8) # Performing dilation with a square kernel of size 3×3 dilated_image = mh.dilate(image, Bc=mh.disk(3)) # 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 dilated image axes[1].imshow(dilated_image, cmap=”gray”) axes[1].set_title(”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 output as follows − Dilation with Varying Structuring Element Sizes We can also dilate an image using different structuring element sizes to enhance different aspects of the image. These elements are created with different sizes using the Mahotas disk() function. By choosing different radii or sizes for the structuring elements, we can achieve diverse effects. To begin, we perform the dilation using the largest structuring element. Then, we continue with additional dilations using smaller structuring elements. This approach allows us to modify the image in multiple ways, enhancing specific features and achieving the desired visual effects. Example In here, we are trying to dilate an image with varying structuring element sizes − import mahotas as mh import numpy as np from pylab import imshow, show image = mh.imread(”nature.jpeg”, as_grey=True).astype(np.uint8) # Performing dilation with the largest structuring element largest_se = mh.disk(8) dilated_image = mh.dilate(image, Bc=largest_se) # Performing additional dilations with smaller structuring elements smaller_se_1 = mh.disk(2) smaller_se_2 = mh.disk(5) dilated_image = mh.dilate(dilated_image, Bc=smaller_se_1) dilated_image = mh.dilate(dilated_image, Bc=smaller_se_2) # Displaying the dilated image imshow(dilated_image) show() Output The output obtained is as shown below − Dilation with a Circular-shaped Kernel To create a circular−shaped kernel, we can use the disk() function from Mahotas. By specifying the desired radius, this function generates a NumPy array representing the circular kernel. Once we have the image and circular kernel ready, we can proceed to perform dilation. This operation applies the circular kernel to each pixel of the image, expanding the white regions accordingly. In simpler terms, it enhances the bright areas in the image, making them larger. Example Now, we are dilating an image with a circular−shaped kernel − import mahotas as mh import numpy as np from pylab import imshow, show # Load image image = mh.imread(”sun.png”, as_grey=True).astype(np.uint8) # Circular kernel with radius 5 radius = 5 kernel = mh.disk(radius) dilated_image = mh.dilate(image, kernel) # Display the dilated image imshow(dilated_image) show() Output Following is the output of the above code −
Category: mahotas
Mahotas – Getting Image Moments Image moment is the statistical measure that is used to describe various properties of an image. It provides information about the shape, location, orientation of the objects and intensity of objects within an image. In general, image moments are calculated by summing up the product of pixel intensities and their corresponding spatial coordinates. These moments can be used to derive useful features such as centroid (mean position), area, orientation, and scale of objects in an image. Higher−order moments can capture more complex shape characteristics. Getting Image moments in Mahotas In the context of Mahotas, image moments are calculated using the mahotas.moments() function. This function takes an image as input and returns a set of moments that characterize the image. Mahotas provides various types of moments, including raw moments, central moments, normalized moments, and Hu moments. These moments can be useful for tasks such as object recognition, image alignment, and shape analysis. Using the mahotas.moments() Function The mahotas.moments() function is used to get the moments of an image or a region of interest (ROI). This function takes an image object as input and returns a numpy array containing the computed moments. A NumPy array is like a table of data where each value is arranged in a grid, and you can perform calculations on the entire grid or specific parts of it easily. Following is the basic syntax of the moments() function in Mahotas − mahotas.moments(image, p0, p1, cm=(0, 0), convert_to_float=True) where, p0 − It is the power of the first dimension (float) p1 − It is the power of the second dimension (float) image − It is the input image and it should be a 2−D array cm − It is the center of mass and (o,o)is taken as default. Example In the example given below, we are using the moments() function to get image moments − import mahotas as mh import numpy as np from pylab import imshow, show image = mh.imread(”sun.png”) # extracting the first channel (0th index) of the image array using slicing cimage=image[:,:,0] p0=5.5 p1=5.5 moment = mh.moments(cimage,p0,p1) print(moment) Output Following is the output of the above code − 2.971238276705602e+39 Getting Image Moments by Specifying Center of Mass The center of mass is a measure of the average position of the pixels, weighted by their intensities. By specifying the center of mass, we can obtain moments that are relative to that particular location. We can specify the center of mass to get image moments by passing the ”cm” parameter to the moments() function. By setting the center of mass, we can shift the coordinate system and obtain moments that are relative to a specific position within the image. Example In here, we are getting the image moment by specifying the center of mass co-ordinates − import mahotas as mh image = mh.imread(”nature.jpeg”, as_grey = True) moments = mh.moments(image, p0=1, p1=0, cm=(100, 100)) print(moments) Output After executing the above code, we get the following output − 40074427849.0 Getting Image moments by Disabling Float Conversion By disabling float conversion, we preserve the original data type of the input image during the moment calculation. This is helpful when we work with specific image formats, such as grayscale or binary images, where the pixel values are already in integer format. We can disable the conversion of the input image to a floating−point representation during the calculation of image moments in mahotas, by passing the ”convert_to_float” parameter to the moments() function. The convert_to_float parameter is explicitly set to False. This ensures that the input image is not converted to a floating−point representation during the moment calculation. Example In the following example, we are getting image moments by disabling float conversion − import mahotas as mh image = mh.imread(”tree.tiff”, as_grey = True) moments = mh.moments(image, p0=2, p1=0, cm=(0, 0), convert_to_float=False) print(moments) Output Output of the above code is as follows − 11029976739711.432 Getting Higher Order Image Moments Higher−order image moments provide more detailed information of the image”s pixel distribution, symmetry, and shape characteristics. These moments help us to capture complex patterns and variations that lower−order moments might overlook. We can get the higher order image moments in mahotas by specifying the desired order of moments using the ”p0” and ”p1” parameters, where higher values represent higher orders. Example Here, we are trying to get the higher order image moments − import mahotas as mh image = mh.imread(”sea.bmp”, as_grey = True) moments = mh.moments(image, p0=3, p1=3, cm=(10, 10)) print(moments) Output The output of the above code is as follows − 2.3690172519584466e+24
Mahotas – Roundness of Image The roundness of an image refers to the measure of how closely an object or a region in the image resembles a perfect circle. It is a metric used to quantify the degree of circularity or deviation from circularity. The roundness value is calculated by comparing the object”s shape to that of a circle. A perfectly round object would have a roundness value close to 1, while objects that are more elongated or irregular in shape would have roundness values closer to 0. Roundness of Image in Mahotas In Mahotas, we can calculate the roundness of an object using the mahotas.features.roundness() function. This function takes a binary image as input. A binary image is an image where each pixel is either classified as foreground (object of interest) or background. Generally, the foreground pixels are represented by white (pixel value = 1), and the background pixels are represented by black (pixel value = 0). The input binary image should be in a boolean format or represented as a NumPy array with boolean values. The mahotas.features.roundness() function The ”mahotas.features.roundness()” function accepts a binary image as input and returns a float value between 0 and 1. The closer the value is to 1.0, the closer the shape is to a perfect circle. Syntax Following is the basic syntax of the roundness() function in mahotas − mahotas.features.roundness(image) Where, ”image” is the Boolean image input. Example In the following example, we are finding the roundness of an image using the roundness() function − import mahotas as mh import numpy as np image = mh.imread(”sun.png”, as_grey = True) roundness = mh.features.roundness(image) print(“Roundness of the image= “, roundness) Output The output obtained is as follows − Roundness of the image= 4.98542867728303e-05 Blob Roundness in Binary Image Blob roundness refers to a measure of how closely a blob resembles a perfect circle. A roundness value close to 1 indicates a more circular blob, while a value significantly lower than 1 indicates a more elongated or irregular shape. To compute blob roundness in a binary image using Mahotas, we need to take an image having a clear separation between the foreground (blobs) and the background. Then, label the blobs in the binary image to assign a unique identifier (index) to each blob. It helps in distinguishing individual blobs. Thereafter, compute the roundness of each labeled blob. Example In here, we are trying to compute the blob roundness in a binary image − import mahotas as mh import numpy as np image = mh.imread(tree.tiff”, as_grey=True) # Labelling the blobs in the image labeled, _ = mh.label(image) # Computing the roundness of each blob roundness = mh.features.roundness(labeled) print(“Blob Roundness:”, roundness) Output Output of the above code is as follows − Blob Roundness: 2.0659091361803767 Using zernike_moment Zernike moments are mathematical representations of the shape of an object in an image. It captures the roundness and other shape attributes of an image by analyzing the distribution of intensity or color variations within the object. To determine the roundness of an image using Zernike Moments, we start by specifying a radius value. This radius determines the size of the circular region in which the moments will be computed. Choosing a smaller radius is ideal for analyzing smaller objects, while a larger radius is more suitable for larger objects. Once we have the Zernike Moments calculated, the first moment becomes particularly important when assessing image roundness. It serves as a representative measure of the overall roundness of the object within the image. By extracting the first element from the Zernike Moments list, we obtain a specific value that quantifies the roundness of the object accurately. Example Here, we are trying to find an image roundness with Zernike Moments − import mahotas as mh image = mh.imread(”nature.jpeg”, as_grey = True) # Setting the radius for calculating Zernike moments radius = 10 # Calculating the Zernike moments moments = mh.features.zernike_moments(image, radius=radius) # The first Zernike moment represents roundness roundness = moments[0] print(roundness) Output After executing the above code, we get the output as shown below − 0.3183098861837907
Mahotas – Centre of Mass of an Image The center of mass refers to the average position of the mass of an object. It is a point where the total mass of the object is concentrated. In simple terms, it represents the balancing point of an object. If the object is uniform and symmetric, the center of mass will be at its geometric center, otherwise not. Center of Mass of an Image in Mahotas The center of mass in Mahotas is determined by assigning a mass value to each pixel of the object, and then calculating the average position of these mass values. This results in the coordinates of the center of mass, which indicate the location of the object”s centroid within the image. Using mahotas.center_of_mass() Function The mahotas.center_of_mass() function is used to find the center of mass of an image. It calculates the average position (as a tuple of coordinates) of the pixel intensities in the image, providing a measure of where the “center” of the image is located. Following is the basic syntax of center_of_mass() function in mahotas − mahotas.center_of_mass(image) Were, image refers to the input image for which you want to find the center of mass. Example In the following example we are calculating the center of mass of the image ”nature.jpeg” − import mahotas as ms import numpy as np # Loading the image image = ms.imread(”nature.jpeg”) # Calculating the center of mass com = ms.center_of_mass(image) # Printing the center of mass print(“Center of mass of the image is:”, com) Output The center of mass is represented by a 3D coordinate in the form [x, y, z] as shown in the output below − Center of mass of the image is: [474.10456551 290.26772015 0.93327202] Center of Mass Using Numpy Functions The NumPy functions are built-in tools in the NumPy library that let you perform array operations and mathematical computations with ease in Python. To calculate the center of mass using NumPy functions in Mahotas, we need to determine the average position of the “weight” of an image. This is achieved by multiplying the x and y coordinates of each pixel by their intensity values, summing these weighted coordinates, and then dividing the result by the total sum of intensities. Following is the basic syntax to calculate center of mass of an image using numpy functions − com = np.array([np.sum(X * Y), np.sum(A * B)]) / np.sum(C) Where, ”X” and ”Y” represent the coordinate arrays, ”A” and ”B” represent the arrays corresponding to the values or intensities associated with the coordinates, and ”C” refers to the array representing the overall sum of the values or intensities. Example In here, we are creating a single coordinate array using. The first dimension of coords represents the y−coordinates and the second dimension represents the x−coordinates. We then access coords[1] to get the x−coordinates and coords[0] to get the y−coordinates when calculating the weighted sum − import mahotas as mh import numpy as np # Loading the image image = mh.imread(”tree.tiff”) # Creating a single coordinate array coords = np.indices(image.shape) # Calculating the weighted sum of x and y coordinates com = np.array([np.sum(coords[1] * image), np.sum(coords[0] * image)]) / np.sum(image) # Printing the center of mass print(“Center of mass:”, com) Output Following is the output of the above code − Center of mass: [ 7.35650493 -3.83720823] Center of Mass of a Specific Region The center of mass of a specific region is the region (area) of interest within the image. This could be a specific area, such as a bounding box or a selected region. The center of mass of a specific region is calculated by taking the weighted average of the x and y coordinates of each pixel in the ROI (Region of Interest), where the weights are the pixel intensities. The center of mass is returned as a tuple of two values representing the x and y coordinates, respectively. Example Following is example to calculate the center of mass of a region of interest of a grayscale image − import mahotas as mh import numpy as np # Load a grayscale image image = mh.imread(”nature.jpeg”, as_grey=True) # Defining a region of interest roi = image[30:90, 40:85] # Calculating the center of mass of the ROI center = mh.center_of_mass(roi) print(center) Output Output of the above code is as follows − [29.50213372 22.13203391]
Mahotas – Stretching Gamma Correction Stretching gamma correction refers to enhancing the overall contrast of an image. This is done by increasing the gamma value, which expands the range of intensity levels of the pixels of the image. The process of stretching gamma correction involves stretching the original input values to a new broader range of values. Stretching Gamma Correction in Mahotas In Mahotas, we can do stretching gamma correction of an image by using the mahotas.stretch() function. In gamma correction, a gamma value greater than 1 increases the contrast of the image, while a gamma value less than 1 decreases the contrast. Hence, by stretching the gamma, dark areas of the image become darker, and bright areas become brighter, resulting in a more pronounced distinction between different shades and details. Using the mahotas.stretch() Function The mahotas.stretch() function takes an image as input and returns a sharpened version of the image as output. The resulting image has enhanced contrast and improved visibility of details. The stretch() function determines the minimum and maximum intensity values in the image and transforms them to full range of pixel values (0−255 for 8−bit images). Syntax Following is the basic syntax for mh.stretch() function in mahotas − mahotas.stretch(img, arg0=None, arg1=None, dtype=<class ”numpy.uint8”>) where, image − It is the input image. arg0 (optional) − It is the minimum value for output (default is 0). arg1 (optional) − It is the maximum value for output (default is 255). dtype (optional) − It is the data type of output image (default is uint8). Example In the following example, we are increasing the contrast of a grayscale image by using mh.stretch() function − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”sun.png”) # Converting to grayscale gray_image = mh.colors.rgb2gray(image) # Decreasing gamma value corrected_gamma = 3.2 # Applying stretch gamma correction image stretch_gamma_corrected = mh.stretch(gray_image, corrected_gamma) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(gray_image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying Stretched gamma corrected image axes[1].imshow(stretch_gamma_corrected, cmap=”gray”) axes[1].set_title(”Stretched Gamma Corrected Image”) axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output Following is the output of the above code − Stretching Gamma Correction of RGB Image We can also perform stretching gamma correction for an RGB image in mahotas using the stretch() function. The gamma value used in the stretch function determines the extent of contrast enhancement. We can then convert the stretched image back to the RGB color space, by multiplying it by 255 (maximum intensity of an RGB image). Example The following example shows increasing of the contrast of an RGB image − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”nature.jpeg”) # Applying stretched gamma correction stretched_gamma_corrected = mh.stretch(image, 3) # Converting the image back to RGB stretched_gamma_corrected = stretched_gamma_corrected * 255 # Creating subplots to display images fig, axes = mtplt.subplots(1, 2) # Displaying the original RGB image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the stretched image axes[1].imshow(stretched_gamma_corrected) axes[1].set_title(”Stretched Gamma Corrected Image”) axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output Output of the above code is as follows − Interactive Stretching Gamma Correction Slider An interactive stretching gamma correction slider is a GUI element that allows users to adjust the gamma value to change the contrast dynamically by dragging the slider. To stretch gamma correction using an interactive slider in Mahotas, first create a slider for adjusting the gamma value. Then, implement a function to retrieve the new gamma value when the slider is moved and apply stretching gamma correction to the image. Finally, connect the function to the slider”s value change event, so it is called automatically when the slider is moved. Syntax Following is the basic syntax to create an interactive slider − from matplotlib.widgets import Slider Slider(slider_axis, name, min_value, max_value, valint) where, slider_axis − It is a list that defines the position and dimensions of the slider. name − It is the name of the slider. mini_value − It is the minimum value that the slider can go to. max_value − It is the maximum value that the slider can go to. valint − It is the starting value of the slider. Example In this example we are increasing the contrast using an interactive slider − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt from matplotlib.widgets import Slider # Loading the image image = mh.imread(”tree.tiff”) # Creating a figure and axes for the plot fig, axis = mtplt.subplots() # Displaying the original image axis.imshow(image) axis.set_title(”Stretching Gamma Correction”) axis.set_axis_off() # Creating a slider for stretched gamma adjustment slider_axis = mtplt.axes([0.2, 0.05, 0.6, 0.03]) stretched_gamma_slider = Slider(slider_axis, ”Stretched Gamma”, 0.1, 5.0, valinit=1.0) # Updating the stretched gamma correction and plot on change of slider value def update_stretched_gamma(val): stretched_gamma = stretched_gamma_slider.val corrected_image = mh.stretch(image, stretched_gamma) corrected_image = corrected_image * 255 axis.imshow(corrected_image) fig.canvas.draw_idle() stretched_gamma_slider.on_changed(update_stretched_gamma) # Showing the figure mtplt.show() Output The output produced is as follows −
Mahotas – Local Maxima in an Image A local maximum is a pixel or a particular region in an image that has a higher intensity or value than its neighboring pixels or regions. It represents a peak or a high point in the image data. One way to find local maxima is by performing a local neighborhood analysis. For each pixel in the image, its neighborhood is examined, and if the pixel is the maximum within its neighborhood, it is considered a local maximum. Local Maxima in an Image in Mahotas We can find the local maxima in an image in Mahotas using the locmax() function. It takes an image as input and returns a binary mask where the local maxima are marked as True or 1. The local_maxima() function in Mahotas uses a non-maximum suppression algorithm to locate the local maxima efficiently. By examining each pixel and its neighborhood, the function determines whether the pixel is the maximum within its local region. This analysis allows for the detection of peaks or high points in the image data, which can play an important role for various applications such as feature extraction, object detection, and image segmentation. The non−maximum suppression algorithm is used in object detection and edge detection tasks to eliminate redundant and weak detections by selecting the highest intensity or response value among neighboring pixels, thereby keeping only the local maxima and suppressing non−maximum values. Using the locmax() Function The locmax() function in Mahotas is used to identify local maxima in an input image efficiently. It takes a grayscale or single channel image as input and returns a binary mask where the local maxima are marked as True or 1. Syntax Following is the basic syntax of the locmax() function in mahotas − mahotas.Locmax(image_name) where, ”image_name” is the input image. Example Following is the basic example to find the local maxima in an image − import mahotas as mh import numpy as np from pylab import imshow, show import matplotlib.pyplot as plt image = mh.imread(”nature.jpeg”, as_grey=True) maxima = mh.locmax(image) print(“Maxima:”, maxima) imshow(maxima) show() Output Following is the output of the above code − Maxima: [[ True True True … True True True] [ True True True … True True True] [ True True True … True True True] … [False False False … False False False] [False False False … False False True] [ True False True … False False True]] The image displayed is as shown below − Using the regmax() Function We can also use the regmax() function in Mahotas for finding regional maxima in an image. A regional maximum is defined as a point in the image that has a higher intensity value than all of its neighboring pixels within a specified region. The regmax() function accepts an image as the input parameter and returns a boolean image of the same size as an input image. Following is the basic syntax of regmax function in mahotas − regmax(image) where, ”image” is a grayscale or color image on which regional maxima need to be identified. Example In here, we are trying to find the regional maxima within the connected regions in a grayscale image using the regmax() function − import mahotas as mh def find_local_maxima(image): regional_maxima = mh.regmax(image) return regional_maxima image = mh.imread(”sun.png”, as_grey=True) local_maxima_points = find_local_maxima(image) print(local_maxima_points) Output Output of the above code is as follows − [[False False False … False False False] [False False False … False False False] [False False False … False False False] … [False False False … False False False] [False False False … False False False] [False False False … True False False]] Regional Maxima of a Colored Image We can also find the regional maxima within the connected regions in a color image using the regmax() function. Example Now, we are trying to find the regional maxima within the connected regions in a color image using the regmax() function − import mahotas as mh def find_local_maxima(image): regional_maxima = mh.regmax(image) return regional_maxima image = mh.imread(”tree.tiff”) local_maxima_points = find_local_maxima(image) print(local_maxima_points) Output We get the output as follows − [[[False False False] [ True True True] [False False False] … [False False False] [False False False] [False False False]] . . . [False False False] [False False False] [ True False False]]]
Mahotas – Eccentricity of an Image The eccentricity of an image refers to the measure of how elongated or stretched the shape of an object or region within the image is. It provides a quantitative measure of how how much the shape deviates from a perfect circle. The eccentricity value ranges between 0 and 1, where − 0 − Indicates a perfect circle. Objects with an eccentricity of 0 have the least elongation and are perfectly symmetric. Close to 1 − Indicates increasingly elongated shapes. As the eccentricity value approaches 1, the shapes become more elongated and less circular. Eccentricity of an Image in Mahotas We can calculate the eccentricity of an image in Mahotas using the ”mahotas.features.eccentricity()” function. If the eccentricity value is higher, it indicates that the shapes in the image are more stretched or elongated. On the other hand, if the eccentricity value is lower, it indicates that the shapes are closer to being perfect circles or less elongated. The mahotas.features.eccentricity() function The eccentricity() function in mahotas helps us measure how stretched or elongated the shapes are in an image. This function takes an image with single channel as input and returns a float point number between 0 and 1. Syntax Following is the basic syntax of the eccentricity() function in mahotas − mahotas.features.eccentricity(bwimage) Where, ”bwimage” is the input image interpreted as a boolean value. Example In the following example, we are finding the eccentricity of an image − import mahotas as mh import numpy as np from pylab import imshow, show image = mh.imread(”nature.jpeg”, as_grey = True) eccentricity= mh.features.eccentricity(image) print(“Eccentricity of the image =”, eccentricity) Output Output of the above code is as follows − Eccentricity of the image = 0.8902515127811386 Calculating Eccentricity using Binary Image To convert a grayscale image into a binary format, we use a technique called thresholding. This process helps us to separate the image into two parts − foreground (white) and background (black). We do this by picking a threshold value (indicates pixel intensity), which acts as a cutoff point. Mahotas simplifies this process for us by providing the “>” operator, which allows us to compare pixel values with the threshold value and create a binary image. With the binary image ready, we can now calculate the eccentricity. Example Here, we are trying to calculate the eccentricity of a binary image − import mahotas as mh image = mh.imread(”nature.jpeg”, as_grey=True) # Converting image to binary based on a fixed threshold threshold = 128 binary_image = image > threshold # Calculating eccentricity eccentricity = mh.features.eccentricity(binary_image) print(“Eccentricity:”, eccentricity) Output After executing the above code, we get the following output − Eccentricity: 0.7943319646935899 Calculating Eccentricity using the Skeletonization Skeletonization, also known as thinning, is a process that aims to reduce the shape or structure of an object, representing it as a thin skeleton. We can achieve this using the thin() function in mahotas. The mahotas.thin() function takes a binary image as input, where the object of interest is represented by white pixels (pixel value of 1) and the background is represented by black pixels (pixel value of 0). We can calculate the eccentricity of an image using skeletonization by reducing the image to its skeleton representation. Example Now, we are calculating the eccentricity of an image using the skeletonization − import mahotas as mh import matplotlib.pyplot as plt # Read the image and convert it to grayscale image = mh.imread(”tree.tiff”) grey_image = mh.colors.rgb2grey(image) # Skeletonizing the image skeleton = mh.thin(grey_image) # Calculating the eccentricity of the skeletonized image eccentricity = mh.features.eccentricity(skeleton) # Printing the eccentricity print(eccentricity) # 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 skeletonized image axes[1].imshow(skeleton, cmap=”gray”) axes[1].set_title(”Skeletonized 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 − 0.8975030064719701 The image displayed is as shown below −
Mahotas – Image Stretch RGB Image stretching is a technique of enhancing the contrast of an image by mapping the original pixel values to a new range of values. This is generally done to improve the visibility of details and enhance the overall appearance of the image. In the case of RGB images, each pixel has three color channels− red, green, and blue. Stretching the RGB image involves independently stretching the values of each color channel to expand the range of intensities. Image Stretch RGB in Mahotas In Mahotas, RGB image stretching is performed by stretching the individual color channels (Red, Green, and Blue) of the image. The stretching process involves mapping the original pixel intensities to a new range to increase the contrast. After stretching the individual RGB channels, they need to be combined back to create the final stretched image. We can perform image stretching RGB in mahotas using the stretch() function and the stretch_rgb() function. Using the stretch() and the stretch_rgb() Functions The mahotas.stretch() and mahotas.stretch_rgb() functions stretches the pixel values in each channel of the RGB image between the specified min_value and max_value. Pixels with values below min_value will be set to min_value, and pixels with values above max_value will be set to max_value. The stretching is performed independently on each channel. Syntax Following is the basic syntax for mahotas.stretch() function − mahotas.stretch(img,arg0=None, arg1=None, dtype=<type ”numpy.uint8”>) Following is the basic syntax of mahotas.stretch_rgb() function − mahotas.stretch(img,arg0=None, arg1=None, dtype=<type ”numpy.uint8”>) Parameters Following are the parameters passed to the mahotas.stretch() function − img − It is the input image. arg0 and arg1 (optional) − These parameters specify the range within which the pixel values should be stretched. The interpretation of these arguments depends on their values − If both arg0 and arg1 are provided and are not None, they represent the minimum and maximum values that the pixel values should be stretched to. The pixel values below arg0 will be set to arg0, and pixel values above arg1 will be set to arg1. If only arg0 is provided and arg1 is None, arg0 should be a tuple or list of length 2 representing the percentile values. The pixel values below the value at the first element of arg0 will be set to that value, and pixel values above the value at the second element of arg0 will be set to that value. If both arg0 and arg1 are None, the pixel values will be stretched to the full range of the data type specified by the dtype parameter (default is numpy.uint8). dtype − It specifies the data type of the output image. The default value is numpy.uint8, which represents an unsigned 8-bit integer. Example In the following example, we are using the mahotas.stretch() function to stretch the image− import mahotas as mh import numpy as np import matplotlib.pyplot as plt image = mh.imread(”sun.png”) # Stretch image stretched = mh.stretch(image, arg0=100, arg1=280) # Create a figure with subplots fig, axes = plt.subplots(1, 2, figsize=(10, 5)) # Display the original image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].axis(”off”) # Display the stretched grayscale image axes[1].imshow(stretched, cmap=”gray”) axes[1].set_title(”Stretched Grayscale 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 − Example In this example, we will see how to use mahotas.stretch_rgb function to stretch an image − import mahotas as mh import numpy as np import matplotlib.pyplot as plt image = mh.imread(”nature.jpeg”) # Stretch image stretched = mh.stretch_rgb(image, arg0=100, arg1=280) # Create a figure with subplots fig, axes = plt.subplots(1, 2, figsize=(10, 5)) # Display the original image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].axis(”off”) # Display the stretched grayscale image axes[1].imshow(stretched, cmap=”gray”) axes[1].set_title(”Stretched Grayscale 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 − Stretching Image with Percentile Value To stretch an RGB image with percentile values in Mahotas, we can manually calculate the percentiles and use the stretch function individually on each color channel − Firstly, split the RGB image into individual color channels (red, green, and blue). This can be achieved by using the np.split() function from the NumPy library. Once the image is split, calculate the desired percentiles for each color channel using NumPy”s np.percentile() function. After obtaining the percentiles, use the stretch() function from Mahotas to each channel independently, using the calculated minimum and maximum values. This will stretch the pixel values within the desired percentile range for each channel. Finally, merge the stretched channels back into an RGB image by concatenating them along the color channel axis. The resulting image will be the stretched RGB image based on the specified percentiles. Example In here, we are trying to stretch an image with percentile value − import mahotas as mh import numpy as np from pylab import imshow, show image = mh.imread(”nature.jpeg”) # Splitting the RGB image into individual channels r, g, b = np.split(image, 3, axis=2) # Calculating percentiles for each channel r_min, r_max = np.percentile(r, [17, 90]) g_min, g_max = np.percentile(g, [25, 75]) b_min, b_max = np.percentile(b, [50, 99]) # Stretching each channel independently stretched_r = mh.stretch(r, r_min, r_max) stretched_g = mh.stretch(g, g_min, g_max) stretched_b = mh.stretch(b, b_min, b_max) # Merging the stretched channels back into an RGB image stretched_image = np.concatenate((stretched_r, stretched_g, stretched_b), axis=2) imshow(stretched_image) show() Output After executing the above code, we get the following output −
Mahotas – Increase Gamma Correction Let us first learn about what is gamma correction before understanding about increase gamma correction. Gamma correction adjusts the brightness of images to match how our eyes perceive light. Our eyes don”t see light in a linear way, so without correction, images may appear too dark or bright. Gamma correction applies a mathematical transformation to the brightness values, making the image look more natural by adjusting its brightness levels. Now, increasing gamma correction refers to adjusting the gamma value to make the overall image brighter. When the gamma value is increased, the dark areas appear brighter and enhance the overall contrast of the image. Increasing Gamma Correction in Mahotas In Mahotas, increasing gamma correction refers to adjusting the gamma value when changing the brightness of the pixels. The gamma is a positive value, where − A gamma value less than 1 will brighten the image. A gamma value greater than 1 will darken the image. A gamma value of 1 represents no correction and indicates a linear relationship between the pixel values and luminance. Gamma correction in Mahotas involves applying a power−law transformation to the intensity values of the image. The power−law transformation is defined as follows − new_intensity = old_intensity^gamma Here, old_intensity − It is the original intensity value of a pixel new_intensity − It is the transformed intensity value after gamma correction. The gamma determines the degree of correction applied to the image. When gamma is increased in Mahotas, it means that the intensity values are raised to a higher power. This adjustment affects the overall brightness of the image. Mahotas does not provide a direct way to do gamma correction, however it can be achieved by using mahotas with numpy library. Example In the following example, we are darkening a grayscale image by decreasing the gamma value − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”sea.bmp”) # Converting it to grayscale gray_image = mh.colors.rgb2gray(image) # Decreasing gamma value corrected_gamma = 1.5 # Updating the image to use the corrected gamma value gamma_correction = np.power(gray_image, corrected_gamma) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(gray_image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the gamma corrected image axes[1].imshow(gamma_correction, cmap=”gray”) axes[1].set_title(”Gamma Corrected Image”) axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output Following is the output of the above code − Using Interactive Gamma Correction Slider An interactive gamma correction slider is a GUI element that allows users to adjust the gamma value dynamically. Users can increase or decrease the gamma value by dragging the slider, providing real−time feedback on the display. We can increase gamma correction using interactive gamma correction slider in mahotas, by first determining the desired gamma value to increase the correction. Then, apply the power−law transformation to the image by raising the pixel values to the power of the inverse gamma value. Syntax Following is the basic syntax to create an interactive slider − from matplotlib.widgets import Slider Slider(slider_axis, name, min_value, max_value, valint) where, slider_axis − It is a list that defines the position and dimensions of the slider. name − It is the name of the slider. mini_value − It is the minimum value that the slider can go to. max_value − It is the maximum value that the slider can go to. valint − It is the starting value of the slider. Example Here, we are trying to increase gamma correction using interactive gamma correction slider − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt from matplotlib.widgets import Slider # Loading the image image = mh.imread(”tree.tiff”) # Converting it to grayscale image = mh.colors.rgb2gray(image) # Creating a figure and axes for the plot fig, axis = mtplt.subplots() # Displaying the original image axis.imshow(image, cmap=”gray”) axis.set_title(”Gamma Correction”) axis.set_axis_off() # Creating a slider for gamma adjustment slider_axis = mtplt.axes([0.2, 0.05, 0.6, 0.03]) gamma_slider = Slider(slider_axis, ”Gamma”, 0.1, 5.0, valinit=1.0) # Updating the gamma correction and plot on change of slider value def update_gamma(val): gamma = gamma_slider.val corrected_image = np.power(image, gamma) axis.imshow(corrected_image, cmap=”gray”) fig.canvas.draw_idle() gamma_slider.on_changed(update_gamma) # Showing the figure mtplt.show() Output Output of the above code is as follows. First we are trying to increase the gamma correction using the slider as shown below − Now, decreasing gamma correction using the slider − Using Batch Gamma correction The batch gamma correction applies multiple gamma values to a single image. This helps in comparing the original image side−by−side at different gamma values to see the impact of increasing gamma correction. In Mahotas, we can adjust the brightness of an image using batch gamma correction by first iterating over a list of predetermined gamma values. Then applying the power−law transformation on the input image with different gamma values. Example Now, we are trying to increase gamma value using batch gamma correction − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt from matplotlib.widgets import Slider # Loading the image image = mh.imread(”sun.png”) # Converting it to grayscale image = mh.colors.rgb2gray(image) # Defining a list of gamma values gamma_values = [0.21, 0.82, 2, 5] # Creating subplots to display images for each gamma value fig, axes = mtplt.subplots(1, len(gamma_values) + 1) axes[0].imshow(image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Applying gamma correction for each gamma value for i, gamma in enumerate(gamma_values): corrected_image = np.power(image, gamma) axes[i + 1].imshow(corrected_image, cmap=”gray”) axes[i + 1].set_title(f”Gamma={gamma}”) axes[i + 1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output Following is the output of the above code −
Mahotas – Color-Space Conversion Color spaces refers to different types of color modes, used in image processing and signals and system for various purposes. Some of the common color spaces are − CMY”K− It is a subtractive color model used in printing and is represented by four color channels: Cyan (C), Magenta (M), Yellow (Y), and Key (K) for black. Y”UV− It is a color space used in video encoding and image processing. It separates the image information into luminance (Y”) and chrominance (UV) components. The Y” channel represents the brightness or grayscale information, while the U and V channels represent the color information. YIQ− It is a color space used in analog television systems, where the Y channel represents brightness and the I and Q channels represent color information. Y”CbCr− It is a color space commonly used in digital image and video encoding, where the Y” channel represents brightness and the Cb and Cr channels represent color information. The Y” channel contains the grayscale information, while the Cb and Cr channels represent the blue−difference and red−difference chroma components, respectively. HSV− HSV (Hue, Saturation, Value) is a color space used to represent colors in a way that is more perceptually meaningful to humans. In our tutorial, we will widely discuss about RGB color space. RGB Color Space RGB stands for red green and blue. It is the most widely used color space, and we will discuss about it in detail in the further chapters. The RGB model states that each color image is actually formed of three different images. Red image, Blue image, and black image. A normal grayscale image is defined by only one matrix, but a color image is actually composed of three different matrices. One color image matrix = red matrix + blue matrix + green matrix This can be best seen in this figure below − Available Functions Following are the different functions available in mahotas for color space conversion − S.No Function & Description 1 rgb2gray() This function converts an RGB image to a grayscale image. 2 rgb2grey() This function converts an RGB image to a grayscale image. 3 rgb2lab() This function converts an RGB image to L*a*b coordinates. 4 rgb2sepia() This function converts an RGB image to sepia i.e. a reddish−brown color. 5 rgb2xyz() This function converts an RGB image to XYZ color space i.e. brightness, color and intensity 6 xyz2lab() This functions converts XYZ to L*a*b color space. 7 xyz2rgb() This function converts XYZ to RGB color space. Now, lets us see examples of some of these functions. The rgb2grey() Function The rgb2grey() function is used to convert an RGB image to a grayscale image. This function assumes that the input image is a 2D NumPy array representing an RGB image, where the dimensions are (height, width, 3) for the height, width, and RGB channels, respectively. If the image is already grayscale (i.e., only has one channel), the function simply returns the image without any modifications. Example Following is the basic example of converting an RGB image to a grayscale image using the rgbtogrey() function − import mahotas as mh import numpy as np from pylab import imshow, show # Loading the image image = mh.imread(”nature.jpeg”) # Converting it to grayscale grey_image = mh.colors.rgb2grey(image) imshow(grey_image) show() Output After executing the above code, we get the following output − The rgb2sepia() Function The rgb2grey() function is used to convert an RGB image to a sepia toned image i.e. a reddish−brown color image. To convert an RGB image to sepia, the function applies a transformation to the RGB values of each pixel. This transformation involves adjusting the red, green, and blue channels to achieve the desired sepia effect. Example Here is the basic implementation of RGB color space to sepia toned image − import mahotas as mh import numpy as np from pylab import imshow, show # Loading the image image = mh.imread(”nature.jpeg”) # Converting it to grayscale sepia_image = mh.colors.rgb2sepia(image) imshow(sepia_image) show() Output Following is the output of the above code − We have discussed these functions in detail in the remaining chapters of this section.