Mahotas – Mean Value of Image Mean value of an image refers to the average brightness of all the pixels of an image. Brightness is a property of an image that determines how light or dark an image appears to the human eye. It is determined by the pixel intensity value; higher pixel intensity values represent brighter areas, while lower pixel intensity values represent darker areas. The mean value of an image is widely used in image segmentation, which involves dividing an image into distinct regions. It can also be used in image thresholding which refers to converting an image into binary image consisting of foreground and background pixels. Mean Value of Image in Mahotas Mahotas does not have a built-in function to find the mean of value of an image. However, we can find the mean value of an image by using mahotas and numpy library together. We can use the mean() function in the numpy library to find the mean pixel intensity value of an image. The mean() function works by iteratively going over each pixel and summing its intensity value. Once all the pixels have been traversed, it divides the sum by the total number of pixels. The mean pixel intensity value of an image can be calculated using the following formula − Mean = Sum of all pixel values / Total number of pixels For example, let”s assume that an image is composed of 2 pixels each with an intensity value of 5. Then the mean can be calculated as follows − Mean = 10 / 2 Mean = 5 The numpy.mean() function The numpy.mean() function takes an image as input and returns the average brightness of all its pixels as a decimal number. The mean function works on any type of input image such as RGB, grayscale or labeled. Syntax Following is the basic syntax of the mean() function in numpy − numpy.mean(image) Where, image − It is the input image. Example In the following example, we are finding the average pixel intensity value of an image using the np.mean() function. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”sun.png”) # Finding the mean value mean_value = np.mean(image) # Printing the mean value print(”Mean value of the image is =”, mean_value) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 1) # Displaying the original image axes.imshow(image) axes.set_title(”Original Image”) axes.set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output Following is the output of the above code − Mean value of the image is = 105.32921300415184 The image obtained is as shown below − Mean Value of each Channel We can also find the mean value of each channel of an RGB image in Mahotas. RGB images to refer to images having three−color channels − Red, Green, and Blue. Each pixel in an RGB image has three intensity values, one for each color channel. The channel value of red is 0, green is 1 and blue is 2. These values can be used to separate an RGB image into its individual color components. In mahotas, to find the mean pixel intensity value of each channel of an RGB image, we first separate the RGB image into separate channels. This is achieved by specifying the channel value. Once the channels are separated, we can find their mean value individually. Example In the example mentioned below, we are finding the mean pixel intensity value of each channel 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”) # Getting the red channel. red_channel = image[:, :, 0] # Getting the green channel. green_channel = image[:, :, 1] # Getting the blue channel. blue_channel = image[:, :, 2] # Finding the mean value of each channel mean_red = np.mean(red_channel) mean_green = np.mean(green_channel) mean_blue = np.mean(blue_channel) # Printing the mean value of each channel print(”Mean value of the Red channel is =”, mean_red) print(”Mean value of the Green channel is =”, mean_green) print(”Mean value of the Blue channel is =”, mean_blue) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 1) # Displaying the original image axes.imshow(image) axes.set_title(”Original Image”) axes.set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output Output of the above code is as follows − Mean value of the Red channel is = 135.4501688464837 Mean value of the Green channel is = 139.46532482847343 Mean value of the Blue channel is = 109.7802007397084 The image produced is as follows − Mean Value of Grayscale Image We can find the mean value of a grayscale image as well. Grayscale images refer to the image having only a single−color channel. Each pixel of a grayscale image is represented by a single intensity value. The intensity value of a grayscale image can range from 0 (black) to 255 (white). Any value between 0 and 255 will produce a shade of gray. Lower values will produce darker shades while higher values will produce lighter shades. In mahotas, we first convert an input RGB image to grayscale using the mh.colors.rgb2gray() function. Then, we find its mean pixel intensity value using the mean() function. Example In this example, we are finding the mean pixel intensity value of a grayscale image. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”tree.tiff”) # Converting it to grayscale grayscale_image = mh.colors.rgb2gray(image) # Finding the mean value of the grayscale image mean_value = np.mean(grayscale_image) # Printing the mean value of the image print(”Mean value of the grayscale image is =”, mean_value) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 1) # Displaying the grayscale image axes.imshow(grayscale_image, cmap=”gray”) axes.set_title(”Grayscale Image”) axes.set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output After executing the above code, we get the following output − Mean value of the grayscale
Category: mahotas
Mahotas – Local Binary Patterns Local Binary Patterns (LBP) is a method that generates a binary pattern. It compares the intensity values of a central pixel with its neighbors. Each pixel in the neighborhood is assigned a value of 1 if it is greater than or equal to the center pixel”s intensity, and 0 otherwise. The binary patterns are used to compute statistical measures or histogram representations that capture the texture information in the image. The resulting descriptors can be utilized in various applications, such as texture classification, object recognition, and image retrieval. Local Binary Patterns uses a technique known as Linear Binary Patterns. The Linear Binary Pattern considers a linear (straight) neighborhood for creating a binary pattern. Let us discuss briefly about linear binary patterns below. Linear Binary Patterns Linear Binary Patterns are used to describe the texture of an image. It works by comparing the intensity values of pixels in a neighborhood around a central pixel and encoding the result as a binary number. In simpler terms, LBP looks at the pattern formed by the pixel values around a particular pixel and represents that pattern with a series of 0s and 1s. Here, we look at linear binary patterns of an image − Example In the example mentioned below, we are trying to perform the above discussed function. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt image = mh.imread(”nature.jpeg”, as_grey=True) # Linear Binary Patterns lbp = mh.features.lbp(image, 5, 5) mtplt.hist(lbp) mtplt.title(”Linear Binary Patterns”) mtplt.xlabel(”LBP Value”) mtplt.ylabel(”Frequency”) mtplt.show() Output After executing the above code, we obtain the following output − We will discuss about the linear binary patterns in detail in the further chapter.
Mahotas – XYZ to RGB Conversion We learnt about XYZ color space, RGB color space, and RGB to XYZ conversion in our previous tutorial. Now let us discuss about the conversion of XYZ color space to RGB color space. When we convert from XYZ to RGB, we are taking the XYZ values of a color, which represent its perceptual properties, and transforming them into red, green, and blue values. This conversion allows us to represent the color in a format that is suitable for display on a particular device or screen. XYZ to RGB Conversion in Mahotas In Mahotas, we can convert an XYZ image to an RGB image using the colors.xyz2rgb() function. The XYZ to RGB conversion in Mahotas involves the following steps − Normalize the XYZ values − Normalize the X, Y, and Z values so that they range between 0 and 1. This step ensures that the XYZ values are relative to a reference white point and allows for consistent color calculations. Convert normalized XYZ to linear RGB − Next, use a conversion matrix to convert the normalized XYZ values to linear RGB values. The conversion matrix specifies how the XYZ coordinates contribute to the red, green, and blue components of the resulting color. The matrix multiplication is performed to obtain the linear RGB values. Apply gamma correction − Gamma correction adjusts the brightness of the RGB values to match the response of the human visual system. Scale the RGB values − After gamma correction, the RGB values are typically in the range of 0 to 1. To represent the colors in the 8−bit range (0−255), you need to scale the RGB values. Multiply each of the gamma−corrected RGB values by 255 to bring them to the appropriate scale. Result − Once the scaling is applied, you have obtained the RGB color values. These values represent the intensities of the red, green, and blue channels of the resulting color. Using the mahotas.colors.xyz2rgb() Function The mahotas.colors.xyz2rgb() function takes an XYZ image as input and returns the RGB color space version of the image. The resulting RGB image retains the structure and content of the original XYZ image, however some color detail is lost. Syntax Following is the basic syntax of the xyz2rgb() function in mahotas − mahotas.colors.xyz2rgb(xyz, dtype={float}) where, xyz − It is the input image in XYZ color space. dtype (optional) − It is the data type of the returned image (default is float). Example In the following example, we are converting an XYZ image to an RGB image using the mh.colors.xyz2rgb() function − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”nature.jpeg”) # Converting it to XYZ xyz_image = mh.colors.rgb2xyz(image) # Converting back to RGB (lossy) rgb_image = mh.colors.xyz2rgb(xyz_image) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original XYZ image axes[0].imshow(xyz_image) axes[0].set_title(”XYZ Image”) axes[0].set_axis_off() # Displaying the RGB image axes[1].imshow(rgb_image) axes[1].set_title(”RGB 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 Transformation Matrix We can use a transformation matrix to convert an XYZ image to an RGB image. The transformation matrix has a set of values that are used to transform a XYZ pixel to RGB pixel. The XYZ pixels are converted to RGB pixels by doing matrix multiplication between the transformation matrix and the XYZ image. We achieve this by using the dot() function in the numpy library. The values of each pixel are then normalized from the range of 0 to 1 (intensity range of XYZ color) to the range of 0 to 255 (intensity range of RGB colors) by multiplying by 255 and then dividing by the maximum intensity of that pixel to obtain the RGB image. Example The following example shows conversion of an XYZ image to an RGB image using transformation matrix − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Function to convert XYZ to RGB def xyz_to_rgb(xyz_image): # XYZ to RGB conversion matrix xyz_to_rgb_matrix = np.array([[3.2406, -1.5372, -0.4986], [-0.9689, 1.8758, 0.0415],[0.0557, -0.2040, 1.0570]]) # Perform the XYZ to RGB conversion using matrix multiplication rgb_image = np.dot(xyz_image, xyz_to_rgb_matrix.T) # Scale the RGB values from the range [0, 1] to [0, 255] rgb_image = (rgb_image * 255.0 / np.max(rgb_image)).astype(np.uint8) return rgb_image # Loading the image image = mh.imread(”tree.tiff”) # Converting it to XYZ xyz_image = mh.colors.rgb2xyz(image) # Converting back to RGB (lossy) rgb_image = xyz_to_rgb(xyz_image) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original XYZ image axes[0].imshow(xyz_image) axes[0].set_title(”XYZ Image”) axes[0].set_axis_off() # Displaying the RGB image axes[1].imshow(rgb_image) axes[1].set_title(”RGB 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 −
Mahotas – SURF Integral SURF, which stands for Speeded−Up Robust Features, is an algorithm used to detect features. The SURF integral is a key concept within this algorithm. To understand the SURF integral, let”s start with the idea of an image. An image is composed of pixels, which are tiny dots that store information about the intensity of the image at that particular location. Now, imagine dividing the image into a small local neighborhood. The SURF integral is a way to efficiently calculate the total pixel value for each local neighborhood. SURF Integral in Mahotas In Mahotas, we can use the mahotas.features.surf.integral() function to compute the SURF integral of an image. Following is the basic approach of how the function works − Initialization − First, the function initializes the integral image by setting all the pixel values to zero. Integral images refer to images that store the sum of all pixels up to a certain point. Recursive Sum Calculation − The function then proceeds to calculate the sum of pixels for each point in the integral image. It does this recursively, meaning it calculates the sum for each point based on the previous sums. As the integral images store the sum of all pixels up to a specific point, they can significantly increase the speed of computing SURF descriptors. Since the function uses recursion, it can be slow for computing the sum of large images. The mahotas.features.surf.integral() function The mahotas.features.surf.integral() function takes a grayscale image as input and returns an integral image as output. The returned result is a new image, typically in the form of a NumPy array, where each pixel value corresponds to the sum of pixel intensities up to that pixel location. Syntax Following is the basic syntax of the surf.integral() function in mahotas − mahotas.features.surf.integral(f, in_place=False, dtype=<class ”numpy.float64”>) Where, f − It is the input image. in_place (optional) − It a flag which determines whether to overwrite the input image (default is ”False”). dtype (optional) − It specifies the data type of the output image (default is float64). Example In the following example, we are calculating the SURF integral of an image using the mh.features.surf.integral() function. import mahotas as mh from mahotas.features import surf import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”sea.bmp”) # Converting it to grayscale image = mh.colors.rgb2gray(image) # Getting the SURF integral surf_integral = surf.integral(image) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the surf integral axes[1].imshow(surf_integral) axes[1].set_title(”SURF Integral”) 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 − SURF Integral of a Random Image We can also compute SURF integral of a randomly generate two−dimensional image. A two−dimensional random image refers to an image where each pixel is assigned a random intensity value. The intensity value can range from 0 (black) to 255 (white). In mahotas, to create a 2−D random image we first specify its dimensions. Then, we pass these dimensions along with the intensity range of the pixels to np.random.randint() function. After that we can compute the SURF integral of the image using the surf.integral() function. Example In the example mentioned below, we are computing the SURF integral of a randomly generated 2−D image. import mahotas as mh from mahotas.features import surf import numpy as np import matplotlib.pyplot as mtplt # Specifying dimensions of image l, w = 1000, 1000 # Creating a random 2-D image image = np.random.randint(0, 256, (l, w)) # Getting the SURF integral surf_integral = surf.integral(image) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the surf integral axes[1].imshow(surf_integral) axes[1].set_title(”SURF Integral”) 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 − SURF Integral of a Threshold Image In addition to random 2−D images, we can also compute the SURF integral of a threshold image. A threshold image is a binary image where the pixels are classified into the foreground or the background. The foreground pixels are white and represented by the value 1, while the background pixels are black and represented by value 0. In mahotas, we first threshold the input image using any thresholding algorithm. Let us assume . This can be done by using the mh.thresholding.bernsen() function on a grayscale image. Then, we can compute the SURF integral of threshold image using the surf.integral() function. Example In here, we are calculating SURF integral of a threshold image. import mahotas as mh from mahotas.features import surf import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”tree.tiff”) # Converting it to grayscale image = mh.colors.rgb2gray(image) # Thresholding the image image = mh.thresholding.bernsen(image, 5, 5) # Getting the SURF integral surf_integral = surf.integral(image) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the surf integral axes[1].imshow(surf_integral) axes[1].set_title(”SURF Integral”) axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output After executing the above code, we get the following output −
Mahotas – Making Image Wavelet Center Image wavelet centering refers to shifting the wavelet coefficients of an image to the wavelet center, a point at which the wavelet reaches its maximum amplitude. Wavelet coefficients are numerical values representing the contribution of different frequencies to an image. Wavelet coefficients are obtained by breaking an image into individual waves using a wavelet transformation. By centering the coefficients, the low and high frequencies can be aligned with the central frequencies to remove noise from an image. Making Image Wavelet Center in Mahotas In Mahotas, we can use the mahotas.wavelet_center() function to make an image wavelet centered to reduce noise. The function performs two major steps to make the image wavelet centered, they are as follows − First it decomposes the signals of the original image into wavelet coefficients. Next, it takes the approximation coefficients, which are coefficients with low frequencies, and aligns them with central frequencies. By doing the alignment of frequencies, the average intensity of the image is removed, hence removing noise. The mahotas.wavelet_center() function The mahotas.wavelet_center() function takes an image as input, and returns a new image with the wavelet center at the origin. It decomposes (breaks−down) the original input image using a wavelet transformation and then shifts the wavelet coefficients to the center of the frequency spectrum. The function ignores a border region of the specified pixel size when finding the image wavelet center. Syntax Following is the basic syntax of the wavelet_center() function in mahotas − mahotas.wavelet_center(f, border=0, dtype=float, cval=0.0) where, f − It is the input image. border (optional) − It is the size of the border area (default is 0 or no border). dtype (optional) − It is the data type of the returned image (default is float). cval (optional) − It is the value used to fill the border area (default is 0). Example In the following example, we are making an image wavelet centered using the mh.wavelet_center() function. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”sun.png”) # Converting it to grayscale image = mh.colors.rgb2gray(image) # Centering the image centered_image = mh.wavelet_center(image) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the centered image axes[1].imshow(centered_image, cmap=”gray”) axes[1].set_title(”Centered 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 − Centering using a Border We can perform image wavelet centering using a border to manipulate the output image. A border area refers to a region surrounding an object in the image. It separates the object from background region or neighboring objects. In mahotas, we can define an area that should not be considered when doing wavelet centering by setting the pixel values to zero. This is done by passing a value to the border parameter of the mahotas.wavelet_center() function. The function ignores as many pixels as specified in the parameter when doing image wavelet centering. For example, if border parameter is set to 500, then 500 pixels on all sides will be ignored when centering the image wavelet. Example In the example mentioned below, we are ignoring a border of certain size when centering an image wavelet. 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 image = mh.colors.rgb2gray(image) # Centering the image with border centered_image = mh.wavelet_center(image, border=500) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the centered image axes[1].imshow(centered_image, cmap=”gray”) axes[1].set_title(”Centered 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 − Centering by applying Padding We can also do centering by applying padding to fill the border area with a shade of gray. Padding refers to the technique of adding extra pixel values around the edges of an image to create a border. In mahotas, padding can be applied by specifying a value to the cval parameter of the mahotas.wavelet_center() function. It allows us to fill the border region with a color, with the value ranging from 0 (black) to 255 (white). Note − Padding can only be applied if a border area is present. Hence, the value or border parameter should not be 0. Example In here, we are ignoring a border of specific pixel size and applying padding to center an image wavelet. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”nature.jpeg”) # Converting it to grayscale image = mh.colors.rgb2gray(image) # Centering the image with border centered_image = mh.wavelet_center(image, border=100, cval=109) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the centered image axes[1].imshow(centered_image, cmap=”gray”) axes[1].set_title(”Centered Image”) axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output After executing the above code, we get the following output −
Mahotas – Majority Filter The majority filter is used to remove the noise from an image. It works by looking at a pixel in an image and considering its neighboring pixels. The majority filter calculates the most common pixel value among its neighbors and replaces the original pixel value with that common value. Imagine you have a black−and−white image where white represents the object you”re interested in and black represents the background. However, due to various reasons, there might be some small black dots (noise) scattered around the object. So to reduce the noise, it counts how many neighboring pixels are black and how many are white. Then, it replaces the original pixel value with the color (black or white) that appears most frequently among its neighbors. Majority Filter in Mahotas To apply the majority filter in mahotas, we can use the majority_filter() function. The majority filter in Mahotas uses a structuring element to examine pixels in a neighborhood. The structuring element counts the pixel values within the neightborhood and replaces the value of each pixel with the most common value to reduce noise. The size of the structuring element determines the extent of smoothing. A larger neighborhood results in stronger smoothing effect, while reducing some finer details, whereas a smaller neightborhood results in less smoothing but maintains more details. The mahotas.majority_filter() function The majority_filter() function applies the majority filter to the input image using the specified neighborhood size. It replaces each pixel value with the majority value among its neighbors. The filtered image is stored in the output array. Syntax Following is the basic syntax of the majority filter in mahotas − mahotas.majority_filter(img, N=3, out={np.empty(img.shape, bool)}) Where, img − It is the input image. N − It is the size of the filter. It must be an odd integer. The default value is 3. Out (optional) − It specifies the output array where the filtered image will be stored. It must be an empty boolean array with the same size as the input image. Example Following is the basic example to filter the image using the majority_filter() function − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt image=mh.imread(”picture.jpg”, as_grey = True) filtered_image = mh.majority_filter(image) # Displaying the original image fig, axes = mtplt.subplots(1, 2, figsize=(9, 4)) axes[0].imshow(image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].axis(”off”) # Displaying the majority filtered image axes[1].imshow(filtered_image, cmap=”gray”) axes[1].set_title(”Majority Filtered”) axes[1].axis(”off”) mtplt.show() Output After executing the above code, we get the following output − By Specifying Window Size To specify the window size in Mahotas, we need to pass it as a parameter to the majority_filter() function. The window size is the number of pixels that will be used to determine the majority value for each pixel in the image. The size of window must be an odd integer. This is because the majority filter works by finding the most common value in a neighborhood of pixels. If the window size is even, there will be two pixels with the same value in the center of the window, and the majority filter will not be able to determine which value is the most common. Example import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt image=mh.imread(”picture.jpg”, as_grey = True) # Specify a filter size filter_size = 19 # Apply majority filter with the specified filter size filtered_image = mh.majority_filter(image, N=filter_size) # Displaying the original image fig, axes = mtplt.subplots(1, 2, figsize=(9, 4)) axes[0].imshow(image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].axis(”off”) # Displaying the majority filtered image axes[1].imshow(filtered_image, cmap=”gray”) axes[1].set_title(”Majority Filtered”) axes[1].axis(”off”) mtplt.show() Output Following is the output of the above code − By Storing Result in an Output Array We can store the result of the majority filter in an output array as well using Mahotas. To achieve this, we first need to create an empty array using the NumPy library. This array is initialized with the same shape as the input image to store the resultant filtered image. The data type of the array is specified as bool, assuming a Boolean image. Finally, we store the resultant filtered image in the output array by passing it as a parameter to the majority_filter() function. Example In here, we are trying to apply majority filter to a grayscale image and store the result in a specific output array − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt image=mh.imread(”pic.jpg”, as_grey = True) # Create an output array for the filtered image output = np.empty(image.shape, dtype=bool) # Apply majority filter with a 3×3 neighborhood # store the result in the output array mh.majority_filter(image, N=3, out=output) # Displaying the original image fig, axes = mtplt.subplots(1, 2, figsize=(9, 4)) axes[0].imshow(image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].axis(”off”) # Displaying the majority filtered image axes[1].imshow(output, cmap=”gray”) axes[1].set_title(”Majority Filtered”) axes[1].axis(”off”) mtplt.show() Output Output of the above code is as follows −
Mahotas – Getting Border of Labels Getting the border of labels refers to extracting the border pixels of a . A border can be defined as a region whose pixels are located at the edges of an image. A border represents the transition between different regions of an image. Getting borders of labels involves identifying the border regions in the labeled image and separating them from the background. Since a labeled image consists of only the foreground pixels and the background pixels, the borders can be easily identified as they are present adjacent to the background regions. Getting Border of labels in Mahotas In Mahotas, we can use the mahotas.labeled.borders() function to get the border of labels. It analyzes the neighboring pixels of the labeled image and considers the connectivity patterns to get the borders. The mahotas.labeled.borders() function The mahotas.labeled.borders() function takes a labeled image as input and returns an image with the highlighted borders. In the resultant image, the border pixels have a value of 1 and are part of the foreground. Syntax Following is the basic syntax of the borders() function in mahotas − mahotas.labeled.borders(labeled, Bc={3×3 cross}, out={np.zeros(labeled.shape, bool)}) Where, labeled − It is the input labeled image. Bc (optional) − It is the structuring element used for connectivity. out (optional) − It is the output array (defaults to new array of same shape as labeled). Example In the following example, we are getting the borders of labels using the mh.labeled.borders() function. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”nature.jpeg”, as_grey=True) # Applying thresholding image = image > image.mean() # Converting it to a labeled image labeled, num_objects = mh.label(image) # Geting border of labels borders = mh.labeled.borders(labeled) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the labeled image axes[0].imshow(labeled) axes[0].set_title(”Labeled Image”) axes[0].set_axis_off() # Displaying the borders axes[1].imshow(borders) axes[1].set_title(”Border Labels”) 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 − Getting Borders by using a Custom Structuring Element We can also get borders of labels by using a custom structuring element. A structuring element is an array which consists of only 1s and 0s. It is used to define the connectivity structure of the neighboring pixels. Pixels that are included in the connectivity analysis have the value 1, while the pixels that are excluded have the value 0. In mahotas, we create a custom structuring element using the mh.disk() function. Then, we set this custom structuring element as the Bc parameter in the borders() function to get the borders of labels. Example Here, we are getting borders of labels using a custom structuring element. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”sea.bmp”, as_grey=True) # Applying thresholding image = image > image.mean() # Converting it to a labeled image labeled, num_objects = mh.label(image) # Geting border of labels borders = mh.labeled.borders(labeled, mh.disk(5)) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the labeled image axes[0].imshow(labeled) axes[0].set_title(”Labeled Image”) axes[0].set_axis_off() # Displaying the borders axes[1].imshow(borders) axes[1].set_title(”Border Labels”) 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 −
Mahotas – Hit & Miss Transform The Hit & Miss transform is a binary that detects specific patterns = or shapes within an image. The operation compares a structuring element with the input binary image. The structuring element consists of foreground (1) and background (0) pixels arranged in a specific pattern that represents the desired shape or pattern to be detected. The Hit−or−Miss Transform performs a pixel−wise logical AND operation between the structuring element and the image, and then checks if the result matches a pre−defined condition. The condition specifies the exact arrangement of foreground and background pixels that should be present in the matched pattern. If the condition is satisfied, the output pixel is set to 1, indicating a match; otherwise, it is set to 0. Hit & Miss transform in Mahotas In Mahotas, we can use the mahotas.hitmiss() function to perform Hit & Miss transformation on an image. The function uses a structuring element ”Bc” to determine whether a specific pattern exists in the input image. The structuring element in Mahotas can take on three values: 0, 1, or 2. A value of 1 indicates the foreground of the structuring element, while 0 represents the background. The value 2 is used as a “don”t care” value, meaning that a match should not be performed for that particular pixel. To identify a match, the structuring element”s values must overlap with the corresponding pixel values in the input image. If the overlap satisfies the conditions specified by the structuring element, the pixel is considered a match. The mahotas.hitmiss() function The mahotas.hitmiss() takes a grayscale image as an input and returns a binary image as output. The white pixels represent areas where there is a match between the structuring element and the input image, while the black pixels represent areas where there is no match. Syntax Following is the basic syntax of the hitmiss() function in mahotas − mahotas.hitmiss(input, Bc, out=np.zeros_like(input)) Where, input − It is the input grayscale image. Bc − It is the pattern that needs to be matched in the input image. It can have a value of 0, 1, or 2. out (optional) − It defines in which array to store the output image (default is of same size as input). Example The following example shows hit & miss transformation on an image using the mh.hitmiss() function. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”tree.tiff”) # Converting it to grayscale image = mh.colors.rgb2gray(image) # Applying thresholding threshold_image = mh.thresholding.bernsen(image, 5, 200) # Creating hit & miss template template = np.array([[1, 2, 1, 2, 1],[2, 1, 1, 1, 2],[2, 2, 1, 2, 2]]) # Applying hit & miss transformation hit_miss = mh.hitmiss(threshold_image, template) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the hit & miss transformed image axes[1].imshow(hit_miss, cmap=”gray”) axes[1].set_title(”Hit & Miss Transformed 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 − By Detecting Edges We can also detect edges of an image by applying the Hit & Miss transformation. The edges represent the boundary between different regions in an image. These are areas where the difference in intensity value between the neighboring pixels is high. In mahotas, to detect edges using Hit & Miss transform, we first create a structuring element. This structuring element matches the edges of the template with the input image. We then perform on the image and then pass the structuring element as the Bc parameter to the hitmiss() function. For example, the following structuring element can be used to detect edges in an input image − [[1, 2, 1] [2, 2, 2] [1, 2, 1]] In here, the 1s are present at the top−rightmost, top−leftmost, bottom−rightmost, and bottom−leftmost positions of the structuring element. The edges are usually present at these locations in an image. The 1s present in the structuring element matches with the pixels having intensity value 1 in the image, thus highlighting the edges as the foreground. Example In this example, we are trying to detect edges of an image by applying the Hit & Miss transformation − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”nature.jpeg”) # Converting it to grayscale image = mh.colors.rgb2gray(image).astype(np.uint8) # Applying thresholding threshold_value = mh.thresholding.rc(image) threshold_image = image > threshold_value # Creating hit & miss template template = np.array([[1, 2, 1],[2, 2, 2],[1, 2, 1]]) # Applying hit & miss transformation hit_miss = mh.hitmiss(threshold_image, template) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the hit & miss transformed image axes[1].imshow(hit_miss, cmap=”gray”) axes[1].set_title(”Hit & Miss Transformed 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 − By Detecting Diagonals We can use the Hit & Miss transformation to detect the diagonals of an image as well. Diagonals are indicated by a linear pattern connecting opposite corners of an image. These are the region where the pixel intensity changes along a diagonal path. In mahotas, we first perform on the input image. We then pass a structuring element as the Bc parameter to the hitmiss() function. This structuring element matches the diagonals of the template with the diagonals of the input image. For example, the following structuring element can be used to detect diagonals in an input image − [[0, 2, 0] [2, 0, 2] [0, 2, 0]] In here, the 0s run along a diagonal path from the top leftmost to the bottom rightmost position, and from the top rightmost to the bottom leftmost position. The diagonals are usually present at these locations in an image. The 0s present in the structuring element matches with the pixels having intensity value 0 in
Mahotas – Soft Threshold Soft threshold refers to decreasing the noise (denoising) of an image to improve its quality. It assigns a continuous range of values to the pixels based on their closeness to the threshold value. This results in a gradual transition between the foreground and background regions. In soft threshold, the threshold value determines the balance between denoising and image preservation. A higher threshold value results in stronger denoising but leads to loss of information. Conversely, a lower threshold value retains more information but results in unwanted noise. Soft Threshold in Mahotas In Mahotas, we can use the thresholding.soft_threshold() function to apply a soft threshold on an image. It dynamically adjusts the threshold value based on the neighboring pixels to enhance images with non−uniform noise levels. By using dynamic adjustment, the function proportionally reduces intensity of those pixels whose intensity exceeds the threshold value and assigns them to the foreground. On the other hand, if a pixel”s intensity is below the threshold it is assigned to the background. The mahotas.thresholding.soft_threshold() function The mahotas.thresholding.soft_threshold() function takes a grayscale image as input and returns an image on which soft threshold has been applied. It works by comparing the pixel intensity with the provided threshold value. Syntax Following is the basic syntax of the soft_threshold() function in mahotas − mahotas.thresholding.soft_threshold(f, tval) Where, f − It is the input grayscale image. tval − It is the threshold value. Example In the following example, we are applying a soft threshold on a grayscale image using the mh.thresholding.soft_threshold() function. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”tree.tiff”) # Converting it to grayscale image = mh.colors.rgb2gray(image) # Setting threshold value tval = 150 # Applying soft threshold on the image threshold_image = mh.thresholding.soft_threshold(image, tval) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the threshold image axes[1].imshow(threshold_image, cmap=”gray”) axes[1].set_title(”Soft Threshold 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 − Soft Threshold using the Mean value We can apply a soft threshold using the mean value of the pixel intensities on an image. The mean value refers to the average intensity of an image. It is calculated by summing the intensity value of all pixels and then dividing it by the total number of pixels. In mahotas, we can find the mean pixel intensities of all the pixels of an image using the numpy.mean() function. The mean value can then be passed to the tval parameter of mahotas.thresholding.soft_threshold() function to generate a soft threshold image. This approach of applying soft threshold maintains a good balance between denoising and image quality as the threshold value is neither too high nor too low. Example The following example shows a soft threshold being applied on a grayscale image when the threshold is the mean value of pixel intensities. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”tree.tiff”) # Converting it to grayscale image = mh.colors.rgb2gray(image) # Setting mean threshold value tval = np.mean(image) # Applying soft threshold on the image threshold_image = mh.thresholding.soft_threshold(image, tval) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the threshold image axes[1].imshow(threshold_image, cmap=”gray”) axes[1].set_title(”Soft Threshold 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 − Soft Threshold using the Percentile value In addition to mean value, we can also apply a soft threshold using the percentile value on the pixel”s intensities of an image. Percentile refers to the value below which a given percentage of data falls; in image processing it refers to distribution of pixel intensities in an image. For example, let”s set the threshold percentile to 85. This means that only pixels with intensities greater than 85% of the other pixels in the image will be classified as foreground, while the remaining pixels will be classified as background. In mahotas, we can use numpy.percentile() function to set a threshold value based on a percentile of pixel intensity. This value is then used in the soft_thresholding() function to apply a soft threshold on an image. Example In this example, we show how soft threshold is applied when threshold is found using percentile value. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”tree.tiff”) # Converting it to grayscale image = mh.colors.rgb2gray(image) # Setting percentile threshold value tval = np.percentile(image, 85) # Applying soft threshold on the image threshold_image = mh.thresholding.soft_threshold(image, tval) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the threshold image axes[1].imshow(threshold_image, cmap=”gray”) axes[1].set_title(”Soft Threshold Image”) axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output After executing the above code, we get the following output −
Mahotas – Distance Transform Distance transformation is a technique that calculates the distance between each pixel and the nearest background pixel. Distance transformation works on distance metrics, which define how the distance between two points is calculated in a space. Applying distance transformation on an image creates the distance map. In the distance map, dark shades are assigned to the pixels near the boundary, indicating a short distance, and lighter shades are assigned to pixels further away, indicating a larger distance to the nearest background pixel. Distance Transform in Mahotas In Mahotas, we can use the mahotas.distance() function to perform distance transformation on an image. It uses an iterative approach to create a distance map. The function first initializes the distance values for all pixels in the image. The background pixels are assigned a distance value of infinity, while the foreground pixels are assigned a distance value of zero. Then, the function updates the distance value of each background pixel based on the distances of its neighboring pixels. This occurs until all the distance value of all the background pixels has been computed. The mahotas.distance() function The mahotas.distance() function takes an image as input and returns a distance map as output. The distance map is an image that contains the distance between each pixel in the input image and the nearest background pixel. Syntax Following is the basic syntax of the distance() function in mahotas − mahotas.distance(bw, metric=”euclidean2”) Where, bw − It is the input image. metric (optional) − It specifies the type of distance used to determine the distance between a pixel and a background pixel (default is euclidean2). Example In the following example, we are performing distance transformation on an image using the mh.distance() function. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”nature.jpeg”) # Converting it to grayscale image = mh.colors.rgb2gray(image) # Finding distance map distance_map = mh.distance(image) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the distance transformed image axes[1].imshow(distance_map) axes[1].set_title(”Distance Transformed 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 Labeled Image We can also perform distance transformation using a labeled image. A labeled image refers to an image where distinct regions are assigned unique labels for segmenting the image into different regions. In mahotas, we can apply distance transformation on an input image by first reducing its noise using the mh.gaussian_filter() function. Then, we use the mh.label() function to separate the foreground regions from the background regions. We can then create a distance map using the mh.distance() function. This will calculate the distance between the pixels of the foreground regions and the pixels of the background region. Example In the example mentioned below, we are finding the distance map of a filtered labeled image. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”tree.tiff”) # Converting it to grayscale image = mh.colors.rgb2gray(image) # Applying gaussian filtering gauss_image = mh.gaussian_filter(image, 3) gauss_image = (gauss_image > gauss_image.mean()) # Converting it to a labeled image labeled, num_objects = mh.label(gauss_image) # Finding distance map distance_map = mh.distance(labeled) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the distance transformed image axes[1].imshow(distance_map) axes[1].set_title(”Distance Transformed 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 Using Euclidean Distance Another way of performing distance transformation on an image is using euclidean distance. Euclidean distance is the straight-line distance between two points in a coordinate system. It is calculated as the square root of the sum of squared differences between the coordinates. For example, let”s say there are two points A and B with coordinate values of (2, 3) and (5, 7) respectively. Then the squared difference of x and y coordinate will be (5−2)2 = 9 and (7−3)2 = 16. Sum of the square will be 9 + 16 = 25 and square root of this will be 5, which is the euclidean distance between point A and point B. In mahotas, we can use euclidean distance instead of the default euclidean2 as the distance metric. To do this, we pass the value ”euclidean” to the metric parameter. Note − The euclidean should be written as ”euclidean” (in single quotes), since the data type of metric parameter is string. Example In this example, we are using euclidean distance type for distance transformation of an image. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”sun.png”) # Converting it to grayscale image = mh.colors.rgb2gray(image) # Applying gaussian filtering gauss_image = mh.gaussian_filter(image, 3) gauss_image = (gauss_image > gauss_image.mean()) # Finding distance map distance_map = mh.distance(gauss_image, metric=”euclidean”) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the distance transformed image axes[1].imshow(distance_map) axes[1].set_title(”Distance Transformed Image”) axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output After executing the above code, we get the following output −