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.
Category: mahotas
Mahotas – Eroding Image Eroding an image in image processing refers to shrinking the pixels of an image. The erosion process will remove pixels around the edges of an image. This operation scans the image and checks if all the pixels within the neighborhood of a particular pixel are foreground pixels. If they are, the pixel is eroded or removed. Eroding an Image in Mahotas By eroding an image in Mahotas, we refer to removing the number of pixels to the boundaries of objects or regions in an image. This operation is commonly used to modify the shapes and structures in an image. We can erode an image in mahotas using the erode() function. It is used to shrink 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 erosion process. The mahotas.erode() function The mahotas.erode() 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 minimum value of all the pixels in the neighborhood. A pixel is set to 0, if any neighbourhood pixels have a value of 0. The erode() 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 erosion operation removes those pixels to the object”s boundary, making it smaller. Syntax Following is the basic syntax of the erode() function in mahotas − mahotas.erode(A, Bc={3×3 cross}, out={np.empty_as(A)}) where, A − It is the input image on which erosion will be performed. It should be a NumPy array representing a grayscale image. Bc (optional) − It is the structuring element used for erosion. By default, it is set to a 3×3 cross−shaped structuring element. out (optional) − It specifies the output array to store the result. Example Following is the basic example to erode an image in mahotas using the erode() 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 erosion with a square kernel of size 3×3 eroded_image = mh.erode(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 eroded image axes[1].imshow(eroded_image, cmap=”gray”) axes[1].set_title(”Eroded 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 − Erosion with Varying Structuring Element Sizes We can also erode an image using different structuring element sizes to modify 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 erosion using the largest structuring element. Then, we continue with additional dilations using smaller structuring elements. Each erosion operation further shrinks the objects in the image by removing pixels from their boundaries. Example In here, we are trying to erode 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 erosion with the largest structuring element largest_se = mh.disk(8) eroded_image = mh.erode(image, Bc=largest_se) # Performing additional erosions with smaller structuring elements smaller_se_1 = mh.disk(2) smaller_se_2 = mh.disk(5) eroded_image = mh.erode(eroded_image, Bc=smaller_se_1) eroded_image = mh.erode(eroded_image, Bc=smaller_se_2) # Displaying the eroded image imshow(eroded_image) show() Output The output obtained is as shown below − Erosion 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 erosion. This operation applies the circular kernel to each pixel of the image, shrinking the foreground pixels accordingly. In simpler terms, it it shrinks the regions based on the connectivity defined by the circular kernel. 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 10 radius = 10 kernel = mh.disk(radius) eroded_image = mh.erode(image, kernel) # Display the eroded image imshow(eroded_image) show() Output Following is the output of the above code −
Mahotas – RGB to XYZ Conversion The XYZ color space is a is a three−dimensional color model that represents the brightness, color, and intensity of that color, based on human perception. In the XYZ color space − The Y component represents the luminance or brightness of the color. The X and Z components determine the chromaticity coordinates or the color”s position on the color spectrum. By combining different values of X, Y, and Z, any visible color can be represented within the XYZ color space. When we convert from RGB to XYZ, we are taking the red, green, and blue values of a color and changing them into different values called XYZ. This helps us separate the color information from the specifics of how it appears on a particular display or device. RGB to XYZ Conversion in Mahotas In Mahotas, we can convert an RGB image to an XYZ image using the colors.rgb2xyz() function. The RGB to XYZ conversion in Mahotas involves the following steps − Normalize the RGB values − Normalize the RGB values of a pixel, generally represented as integers ranging from 0 to 255, to a normalized range between 0 and 1. This step ensures that the RGB values are consistent and comparable. Gamma Correction − Before converting from RGB to XYZ, Mahotas applies gamma correction to the RGB values. Gamma correction adjusts the brightness levels of the image, ensuring that the resulting XYZ values are more accurate representations of the original colors. Linearize the RGB Values − After gamma correction, the RGB values are converted to a linear color space. In this linear RGB color space, the intensity values are proportional to the actual physical light intensity. This linear transformation allows for more accurate color calculations. Conversion Matrix − Mahotas uses a conversion matrix to transform the linear RGB values into XYZ values. The conversion matrix represents the relationship between the RGB and XYZ color spaces. It contains coefficients that determine how much of each color channel contributes to the resulting XYZ values. Output − After applying the conversion matrix, Mahotas provides the XYZ values as the output. These XYZ values represent the color of the input RGB image in a color space that is more perceptually uniform and closer to how the human visual system perceives colors Using the mahotas.colors.rgb2xyz() Function The mahotas.colors.rgb2xyz() function takes an RGB image as input and returns the XYZ color space version of the image. The resulting XYZ image retains the structure and content of the original RGB image. Syntax Following is the basic syntax of the rgb2xyz() function in mahotas − mahotas.colors.rgb2xyz(rgb, dtype={float}) where, rgb − It is the input image in RGB 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 RGB image to an XYZ image using the mh.colors.rgb2xyz() 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 XYZ xyz_image = mh.colors.rgb2xyz(image) # Create a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original RGB image axes[0].imshow(image) axes[0].set_title(”RGB Image”) axes[0].set_axis_off() # Displaying the XYZ image axes[1].imshow(xyz_image) axes[1].set_title(”XYZ 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 Conversion Matrix Another approach that we can use to convert an RGB image to an XYZ image is by using a conversion matrix. The conversion matrix consists of coefficients that relate the RGB components of a pixel to the XYZ components. These value of XYZ components of each pixel can be calculated as follows − X = 0.412456 * r + 0.357576 * g + 0.180437 * b Y = 0.212672 * r + 0.715152 * g + 0.072175 * b Z = 0.019334 * r + 0.119193 * g + 0.950471 * b where X, Y, and Z values represent the corresponding values in the XYZ color space. Example The following example shows conversion of an RGB image to XYZ image using conversion matrix values for the RGB channels − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Function to convert RGB to XYZ def rgb_to_xyz(rgb): height, width, _ = rgb.shape xyz_image = np.zeros((height, width, 3)) for i in range(height): for j in range(width): # Separating the RGB image into individual channels r, g, b = rgb[i, j] x = 0.412456 * r + 0.357576 * g + 0.180437 * b y = 0.212672 * r + 0.715152 * g + 0.072175 * b z = 0.019334 * r + 0.119193 * g + 0.950471 * b xyz_image[i, j] = [x, y, z] return xyz_image # Loading the image image = mh.imread(”tree.tiff”) # Converting it to XYZ xyz_image = rgb_to_xyz(image) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original RGB image axes[0].imshow(image) axes[0].set_title(”RGB Image”) axes[0].set_axis_off() # Displaying the XYZ image axes[1].imshow(xyz_image) axes[1].set_title(”XYZ 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 – Handling Images Mahotas is a popular Python package for handling images and performing various operations on them. It can do many things with images, like separating them into different parts, finding edges, and recognizing objects in them. With Mahotas, we can find all the faces in a set of pictures, or identify different types of flowers in a collection of images. We can also find the edges of objects in pictures, or make blurry pictures clearer in Mahotas. In this tutorial, we will take a brief overview of how to use Mahotas to handle images, including how to read, write, and display images. We will also learn how to perform common image processing tasks such as filtering, segmentation, and feature extraction. Image Handling with Mahotas One of the key features of Mahotas is its ability to handle images in various formats, such as JPEG, PNG, and BMP. Mahotas provides us with functions for reading and writing images, as well as for converting images between different formats. Let us learn how to read an image in Mahotas. Reading Images Reading images in mahotas refers to the process of loading image data from a file. To read an image in Mahotas, we can use the imread() function. This function reads an image from a file and returns a NumPy array representing the image. Example In the code below, we are trying to read a JPEG image named ”nature.jpeg” − import mahotas as ms image = ms.imread(”nature.jpeg”) print (“The image is read.”) Output This will read the image from the file ”nature.jpeg” and store it in the variable ”image” − The image is read. We can also display the image once we have read it as discussed below. Displaying an Image Once you have read an image, we can display it using the matplotlib library. The matplotlib library is used for data visualization and plotting. Example Let us display the image ”nature.jpeg, using matplotlib library as shown below − import matplotlib.pyplot as plt import mahotas as ms image = ms.imread(”nature.jpeg”) plt.imshow(image) plt.show() Output Following is the image obtained while executing the above code− Writing Images Writing images in Mahotas refers to saving the image data to a file in a specific image format such as PNG, JPEG, BMP, TIFF, etc. We can use the imsave() function to write an image in Mahotas. This function is part of the image input/output (IO) module in mahotas. Example In the code below, we are trying to save an image named ”nature.jpeg” − import mahotas as ms image = ms.imread(”nature.jpeg”) print (“The image is read.”) # Write the image to a file ms.imsave(”writing.jpeg”, image) print (“The image data is saved.”) Output This will save the image to a file ”writing.jpeg” − The image is read. The image data is saved. Image Processing with Mahotas Image processing refers to a set of techniques which is used to perform several operations on images. These techniques are helpful in improving the visual quality of images, extract useful information, or prepare them for analysis. Some common image processing tasks are filtering, segmentation, and feature extraction. In this section, we will explore some of the key image processing functions provided by Mahotas. Filtering In mahotas, filtering refers to modifying the appearance of an image or extracting useful information from it. This is done by applying a mathematical operation to an image. The filtering process is useful in removing noise, smoothening an image, enhancing edges, or performing other operations that help improve the quality or interpretability of an image. Example In the following example, we are trying to smooth an image using a Gaussian filter. The Gaussian filter blurs an image, which can be used to reduce noise or smooth an image − import mahotas as ms import numpy as np import matplotlib.pyplot as mtplt # Loading an image and converting it to grayscale image = ms.imread(”nature.jpeg”, as_grey=True) # Applying a Gaussian filter # Standard deviation sigma = 15 gaussian_filtered = ms.gaussian_filter(image, sigma) # 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 filtered image axes[1].imshow(gaussian_filtered, cmap=”gray”) axes[1].set_title(”Gaussian Filtered”) axes[1].axis(”off”) mtplt.show() Output This will smooth the original image using a Gaussian filter with a standard deviation of 15 as shown below − Segmentation Segmentation in mahotas refers to the process of dividing an image into meaningful regions or segments based on certain characteristics or criteria. These segments can represent different objects, regions of interest, or distinct areas within the image. Example Now, let”s go through a basic example of image segmentation using thresholding with Mahotas. Thresholding is used to separate objects from the background based on pixel intensity values. It simplifies an image by converting it into a binary image where eachpixel is classified as either foreground (object) or background − import mahotas as ms import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = ms.imread(”nature.jpeg”, as_grey=False) # Performing thresholding # Calculating threshold value by taking mean of image threshold = np.mean(image) # creating binary image # comparing each pixel value with the threshold binary_image = image > threshold # Perform connected component analysis # assigns a unique label to each connected region in the binary image labeled_image, num_objects = ms.label(binary_image) # Displaying the original image fig, ax = mtplt.subplots(1, 2, figsize=(9, 4)) ax[0].imshow(image, cmap=”gray”) ax[0].set_title(”Original Image”) # Displaying the segmented image ax[1].imshow(labeled_image, cmap=”rainbow”) ax[1].set_title(”Segmented Image ({} objects)”.format(num_objects)) mtplt.show() Output The output of the above code is as shown below − Feature Extraction Feature extraction in Mahotas refers to the process of extracting meaningful and informative features from an image. These features can represent various aspects of the image, such as texture, shape, or color, and can be used to describe and differentiate objects or regions within the image. Example Now, let”s see an example of how to calculate Zernike moments using Mahotas. The Zernike moments are a set of numerical values that describe the shape of an object or region within an image. They provide
Mahotas – Filtering Regions Filtering regions refers to excluding specific regions of a labeled image based on certain criteria. A commonly used criteria for filtering regions is based on their size. By specifying a size limit, regions that are either too small or too large can be excluded to get a clean output image. Another criterion for filtering regions is to check whether a region is bordered or not. By applying these filters, we can selectively remove or retain regions of interest in the image. Filtering Regions in Mahotas In Mahotas, we can convert filter regions of a labeled image by using the labeled.filter_labeled() function. This function applies filters to the selected regions of an image while leaving other regions unchanged. Using the mahotas.labeled.filter_labeled() Function The mahotas.labeled.filter_labeled() function takes a labeled image as input and removes unwanted regions based on certain properties. It identifies the regions based on the labels of an image. The resultant image consists of only regions that match the filter criterion. Syntax Following is the basic syntax of the filter_labeled() function in mahotas − mahotas.labeled.filter_labeled(labeled, remove_bordering=False, min_size=None, max_size=None) where, labeled − It is the array. remove_bordering (optional) − It defines whether to remove regions touching the border. min_size (optional) − It is the minimum size of the region that needs to be kept (default is no minimum). max_size (optional) − It is the maximum size of the region that needs to be kept (default is no maximum). Example In the following example, we are filtering a labeled image to remove border pixels. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image_rgb = mh.imread(”tree.tiff”) image = image_rgb[:,:,0] # Applying gaussian filtering image = mh.gaussian_filter(image, 4) image = (image > image.mean()) # Converting it to a labeled image labeled, num_objects = mh.label(image) # Applying filters filtered_image, num_objects = mh.labeled.filter_labeled(labeled, remove_bordering=True) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original RGB image axes[0].imshow(image_rgb) axes[0].set_title(”RGB Image”) axes[0].set_axis_off() # Displaying the filtered image axes[1].imshow(filtered_image) axes[1].set_title(”Filtered 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 − Filtering on Regions of Specific Size We can also filter regions of specific size in an image. In this way, we can remove regions from labeled images which do not fall within a specific size limit (regions that are too small or too large). In mahatos, we can achieve this by specifying values to the optional parameter min_size and max_size in the labeled.filter_label() function. Example The following example shows filtering a labeled image to remove regions of specific size. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image_rgb = mh.imread(”tree.tiff”) image = image_rgb[:,:,0] # Applying gaussian filtering image = mh.gaussian_filter(image, 4) image = (image > image.mean()) # Converting to a labeled image labeled, num_objects = mh.label(image) # Applying filters filtered_image, num_objects = mh.labeled.filter_labeled(labeled, min_size=10, max_size=50000) # Create a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original RGB image axes[0].imshow(image_rgb) axes[0].set_title(”RGB Image”) axes[0].set_axis_off() # Displaying the filtered image axes[1].imshow(filtered_image) axes[1].set_title(”Filtered 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 − Filtering on Border Regions and Regions of Specific Size We can filter bordered regions along with regions of a specific size in an image. In this, we remove regions which touch the border and regions which do not fall within a specific size limit. In mahotas, we can do this by specifying values to the optional parameter min_size and max_size and setting the optional parameter remove_bordering to True in the labeled.filter_label() function. Example In this example, a filter is applied to remove border regions and regions of specific size of a labeled image. import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image_rgb = mh.imread(”tree.tiff”) image = image_rgb[:,:,0] # Applying gaussian filtering image = mh.gaussian_filter(image, 4) image = (image > image.mean()) # Converting it to a labeled image labeled, num_objects = mh.label(image) # Applying filters filtered_image, num_objects = mh.labeled.filter_labeled(labeled, remove_bordering=True, min_size=1000, max_size=50000) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original RGB image axes[0].imshow(image_rgb) axes[0].set_title(”RGB Image”) axes[0].set_axis_off() # Displaying the filtered image axes[1].imshow(filtered_image) axes[1].set_title(”Filtered Image”) axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output The output produced is as shown below −
Mahotas – Creating RGB Image An RGB image is a type of digital image that uses the red, green, and blue color model to represent colors. Each pixel in an RGB image is represented by three color channels− red, green, and blue, which store the intensity values (ranging from 0 to 255) for each color. For example, a pixel with full intensity in all three channels (255, 255, 255) represents white, while a pixel with zero intensity in all three channels (0, 0, 0) represents black. Creating RGB Images in Mahotas An RGB image in Mahotas is a 3−dimensional array of shape (h,w,3); where h and w are the height and width of the image, and 3 represents the three channels: red, green, and blue. To create an RGB image using Mahotas, you need to define the dimensions of your image, create an empty numpy array with the desired dimensions, and set the pixel values for each channel. Mahotas does not have direct functions for creating RGB images. However, you can use numpy and mahotas libraries to create RGB images. Example Following is an example to create a gradient RGB image from red to green horizontally and blue to white vertically in mahotas − import mahotas as mh import numpy as np from pylab import imshow, show # Define the dimensions of the image width = 200 height = 150 # Create an empty numpy array with the desired dimensions image = np.zeros((height, width, 3), dtype=np.uint8) # Set the pixel values for each channel # Here, we”ll create a gradient from red to green horizontally and blue to white vertically for y in range(height): for x in range(width): # Red channel gradient r = int(255 * x / width) # Green channel gradient g = int(255 * (width – x) / width) # Blue channel gradient b = int(255 * y / height) # Set pixel values image[y, x] = [r, g, b] # Save the image mh.imsave(”rgb_image.png”, image) # Display the image imshow(image) show() Output Following is an output of the above code − Creating an RGB Image from Color Intensities Color intensities refer to the values that represent the strength or magnitude of each color channel in an image. Higher intensity values result in brighter or more saturated colors, while lower intensity values result in darker or less saturated colors. To create an RGB image from color intensities using Mahotas, you need to create separate arrays representing the intensities for the red, green, and blue color channels. These arrays should have the same dimensions as the desired output image. Example In the following example, we are creating a random RGB noise image from randomly generated color intensities − import mahotas as mh import numpy as np from pylab import imshow, show # Create arrays for red, green, and blue color intensities red_intensity = np.random.randint(0, 256, size=(100, 100), dtype=np.uint8) green_intensity = np.random.randint(0, 256, size=(100, 100), dtype=np.uint8) blue_intensity = np.random.randint(0, 256, size=(100, 100), dtype=np.uint8) # Stack color intensities to create an RGB image rgb_image = np.dstack((red_intensity, green_intensity, blue_intensity)) # Display the RGB image imshow(rgb_image) show() Output After executing the above code, we get the following output − Creating an RGB Image from a Single Color To create an RGB image from a single color, you can initialize an array with the desired dimensions and assign the same RGB value to each pixel. This results in a uniform color appearance throughout the image. Example In here, we are creating a single color image by setting the color to blue using the RGB values (0, 0, 255) − import mahotas as mh import numpy as np from pylab import imshow, show # Define the dimensions of the image width, height = 100, 100 # Create a single color image (blue in this case) blue_image = np.full((height, width, 3), (0, 0, 255), dtype=np.uint8) # Display the blue image imshow(blue_image) show() Output The image obtained is as follows −
Mahotas – Euler Number of an Image Imazine you have a drawing with different shapes on it. The Euler number allows us to count how many holes are in those shapes and how many separate parts they can be divided into (connected components). This can help in analyzing and characterizing the structure of the image. Mathematically, it can be defined as − E = C – H where, E is the Euler number, C is the number of connected components, and H is the number of holes in the image. Euler Number of an Image in Mahotas In Mahotas, you can calculate the Euler number using the mahotas.euler() function. This function takes a binary image as input, where the objects of interest are represented by white pixels and the background is represented by black pixels. It then calculates the Euler number based on the connectivity and holes in the objects present in the image. Using the mahotas.euler() Function The mahotas.euler() function takes a binary image as input and returns the Euler characteristic value as an integer. The Euler characteristic is a topological measure that describes the connectivity and shape of objects in an image. It is defined as the difference between the number of connected components and the number of holes in the image. Following is the basic syntax of euler() function in mahotas − mahotas.euler(f, n=8) Where, ”f” is a 2−D binary image and ”n” is the connected component in integer which is either 4 or 8 (default is 8). Example In the following example, we are computing the Euler number of a binary image ”nature.jpeg” by loading it as a grayscale image and then thresholding it to create a binary image − import mahotas as mh import numpy as np # Load binary image as a NumPy array binary_image = mh.imread(”nature.jpeg”, as_grey=True) > 0 # Compute Euler number euler_number = mh.euler(binary_image) # Print result print(“Eu ler Number:”, euler_number) Output Following is the output of the above code − Euler Number: -2.75 Euler Number with Different Connectivity We can also calculate the Euler number with different connectivity in Mahotas using the euler() function. The connectivity parameter determines which neighboring pixels are considered in the calculation. For example, using connectivity−4 considers only the immediate horizontal and vertical neighbors, while connectivity−8 includes diagonal neighbors as well. Example Here, we are calculating the Euler number with different connectivity for the “nature.jpeg” image − import mahotas as mh import numpy as np # Load the image as a grayscale image image = mh.imread(”sun.png”, as_grey=True) # Threshold the image to create a binary image thresholded_image = image > 0 # Compute the Euler number with 4-connectivity euler_number_4conn = mh.euler(thresholded_image, 4) # Compute the Euler number with 8-connectivity euler_number_8conn = mh.euler(thresholded_image, 8) # Print the results print(“Euler Number (4-connectivity):”, euler_number_4conn) print(“Euler Number (8-connectivity):”, euler_number_8conn) Output Output of the above code is as follows − Euler Number (4-connectivity): -4.75 Euler Number (8-connectivity): -4.75 Euler Number Calculation with Labelled Image A labeled image assigns unique integer labels to connected components in a binary image. In Mahotas, the euler function takes a labelled image as input and returns the Euler number for the entire image. The calculation takes into account the number of objects, holes, and tunnels between objects. Example In here, we are calculating the Euler number of a labeled image derived from the “sea.bmp” image − import mahotas as mh import numpy as np # Load the image as a grayscale image image = mh.imread(”sea.bmp”, as_grey=True) # Threshold the image to create a binary image thresholded_image = image > 0 # Label the connected components in the binary image labeled_image, num_labels = mh.label(thresholded_image) # Compute the Euler number of the labeled image euler_number = mh.euler(labeled_image) # Print the result print(“Euler Number:”, euler_number) Output After executing the above code, we get the following output − Euler Number: -44.75 Euler Number Calculation with Binary Image In Mahotas, the Euler number of a binary image can be calculated using the euler() function. By loading the binary image and converting it to a boolean format, the euler function takes the image as input and returns the Euler number as an integer. Example In the example given below, we are calculating the Euler number of a binary image created from the “nature.jpeg” image using Mahotas − import mahotas as mh # load binary image and convert to boolean format image = mh.imread(”sun.png”, as_grey= True) image = image.astype(bool) # calculate the Euler number euler_number = mh.euler(image) # print the result print(“Euler number of the binary image is:”, euler_number) Output The result obtained is as follows − Euler number of the binary image is: -4.75
Mahotas – RGB to Sepia Sepia refers to a special coloring effect that makes a picture look old−fashioned and warm. When you see a sepia−toned photo, it appears to have a reddish−brown tint. It”s like looking at an image through a nostalgic filter that gives it a vintage vibe. To convert an RGB image to sepia, you need to transform the red, green, and blue color channels of each pixel to achieve the desired sepia tone. RGB to Sepia Conversion in Mahotas In Mahotas, we can convert an RGB image to sepia−toned image using the colors.rgb2sepia() function. To understand the conversion, let”s start with the RGB color model − In RGB, an image is made up of three primary colors− red, green, and blue. Each pixel in the image has a value for these three colors, which determines its overall color. For example, if a pixel has high red and low green and blue values, it will appear as a shade of red. Now, to convert an RGB image to Sepia using mahotas, we follow a specific formula. The formula involves calculating new values for the red, green, and blue channels of each pixel. These new values create the Sepia effect by giving the image a warm, brownish tone. RGB to Sepia Conversion Steps Here”s a simple explanation of the conversion process − Start with an RGB image − An RGB image is composed of three color channels− red, green, and blue. Each pixel in the image has intensity values for these three channels, ranging from 0 to 255. Calculate the intensity of each pixel − To convert to sepia, we first need to calculate the overall intensity of each pixel. This can be done by taking a weighted average of the red, green, and blue color channels. The weights used in the average can be different depending on the desired sepia effect. Adjust the intensity values − Once we have the intensity values, we can apply some specific transformations to obtain the sepia effect. These transformations involve adjusting the levels of red, green, and blue channels in a way that mimics the sepia tone. This can be done by increasing the red channel”s intensity, decreasing the blue channel”s intensity, and keeping the green channel relatively unchanged. Clip the intensity values − After the adjustments, some intensity values may go beyond the valid range (0 to 255 for 8−bit images).To ensure the values stay within this range, we need to clip them. Values below 0 are set to 0, and values above 255 are set to 255. Reconstruct the sepia image − Finally, the adjusted intensity values are used to reconstruct the sepia image. The image now appears with the desired sepia tone, giving it a vintage look. Using the mahotas.colors.rgb2sepia() Function The mahotas.colors.rgb2sepia() function takes an RGB image as input and returns the sepia version of the image. The resulting sepia image retains the overall structure and content of the original RGB image but introduces a warm, brownish tone. Syntax Following is the basic syntax of the rgb2sepia() function in mahotas − mahotas.colors.rgb2sepia(rgb) where, rgb is the input image in RGB color space. Example In the following example, we are converting an RGB image to a sepia image using the mh.colors.rgb2sepia() 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 Sepia sepia_image = mh.colors.rgb2sepia(image) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original RGB image axes[0].imshow(image) axes[0].set_title(”RGB Image”) axes[0].set_axis_off() # Displaying the sepia image axes[1].imshow(sepia_image) axes[1].set_title(”Sepia 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 Factor Another approach we can use to convert an RGB to sepia is by adjusting the intensities of each color channel using predetermined coefficients, which are based on the contribution of each channel to the final sepia image. The contribution of each channel to sepia is calculated as follows − TR = 0.393 * R + 0.769 * G + 0.189 * B TG = 0.349 * R + 0.686 * G + 0.168 * B TB = 0.272 * R + 0.534 * G + 0.131 * B where TR, TG and TB are the transformation factors of red, green, and blue respectively. Applying these transformations to each pixel in the RGB image results in the entire sepiatoned image. Example The following example shows conversion of an RGB image to sepia using transformation factors of the RGB channels − import mahotas as mh import numpy as np import matplotlib.pyplot as plt # Loading the image image = mh.imread(”sun.png”) # Getting the dimensions of the image height, width, _ = image.shape # Converting it to Sepia # Creating an empty array for the sepia image sepia_image = np.empty_like(image) for y in range(height): for x in range(width): # Geting the RGB values of the pixel r, g, b = image[y, x] # Calculating tr, tg, tb tr = int(0.393 * r + 0.769 * g + 0.189 * b) tg = int(0.349 * r + 0.686 * g + 0.168 * b) tb = int(0.272 * r + 0.534 * g + 0.131 * b) # Normalizing the values if necessary if tr > 255: tr = 255 if tg > 255: tg = 255 if tb > 255: tb = 255 # Setting the new RGB values in the sepia image array sepia_image[y, x] = [tr, tg, tb] # Creating a figure and axes for subplots fig, axes = plt.subplots(1, 2) # Displaying the original RGB image axes[0].imshow(image) axes[0].set_title(”RGB Image”) axes[0].set_axis_off() # Displaying the sepia image axes[1].imshow(sepia_image) axes[1].set_title(”Sepia Image”) axes[1].set_axis_off() # Adjusting spacing between subplots plt.tight_layout() # Showing the figures plt.show() Output Output of the above code is as follows −
Mahotas – Image Ellipse Axes An ellipse is a geometric shape which is defined as a curve in a plane surrounding two focal points, such that the sum of the distances to the two focal points is constant for every point on the curve. The major axis of an ellipse refers to the longest diameter, which passes through the two farthest points on the ellipse. The minor axis, on the other hand, is the shortest diameter and is perpendicular to the major axis, intersecting it at the center of the ellipse. These axes provide information about the size, orientation, and aspect ratio of the object or region. Image Ellipse Axes in Mahotas When dealing with ellipses in Mahotas, the axes are specified as a tuple of two values− the lengths of the semi−major and semi−minor axes. Mahotas provides the mahotas.ellipse_axes() function to easily detect ellipses in images and obtain their major and minor axes lengths. Using the ellipse.axes() Function The ellipse_axes() function in Mahotas is used to detect ellipse within images. This function accepts a binary image as input and returns the lengths of the major and minor axes. Syntax Following is the basic syntax to find the ellipse axes of an image in Mahotas − mahotas.features.ellipse_axes(bwimage) where, ”bwimage” is the single channel array of image, interpreted as Boolean. Example In the following example, we will learn how to find image ellipse axes in mahotas − import mahotas as mh import numpy as np image=mh.imread(”nature.jpeg”, as_grey = True) smajor,sminor = mh.features.ellipse_axes(image) print(smajor,sminor) Output Following is the output of the above code − 739.0056545212358 336.5943563176811 Fitting an Ellipse to a Set of Points We can also fit an ellipse to a specific points of interest by generating random points within the image. The random points are generated in a uniform distribution between 0 and 1 using the np.random.rand() function from the NumPy library. Each random number represents a coordinate value on a particular axis. To ensure that the generated points fall within the image boundaries − we multiply the randomly generated values by the shape of the image. The shape of the image represents the dimensions of the image as a tuple (height, width). By multiplying the randomly generated values by the image”s shape, we effectively scale the points to match the dimensions of the image. The resulting points are represented as (x, y) coordinates, where x represents the column index and y represents the row index of the image. Example In here, we are trying to fit an ellipse to a set of given points − import numpy as np import mahotas as mh image = mh.imread(”tree.tiff”, as_grey=True) # Generating a set of random points np.random.seed(0) points = np.random.rand(87, 2) * image.shape[:2] # Fitting an ellipse to the set of points # calculating the axes major_axis, minor_axis = mh.features.ellipse_axes(points) print(major_axis, minor_axis) Output Output of the above code is as follows − 50.226155204899634 1.0 Fitting an Ellipse to a Grayscale Image Using ROI We can fit an ellipse to a grayscale image using Region Of Interest (ROI) by generating random points of interest. Then, we need to create a binary image, where the points are set to white (pixel value 255), and the background is black (pixel value 0). We can accomplish this by − Initializing an array of zeros with the same shape as the original image. Then, setting the indices of the points of interest within the array to 255, effectively marking those locations as points of interest. This binary image allows us to isolate and focus on the specific points we want to analyze when fitting the ellipse, enabling us to estimate the estimate the ellipse”s parameters accurately based on the chosen points. Example Here, we are fitting an ellipse to a grayscale image using Region Of Interest − import numpy as np import mahotas as mh image = mh.imread(”sun.png”, as_grey=True) # Generating a binary image with points of interest np.random.seed(0) points = np.random.rand(100, 2) * image.shape[:2] points = points.astype(int) binary_image = np.zeros(image.shape, dtype=np.uint8) binary_image[points[:, 0], points[:, 1]] = 255 # Fitting an ellipse to the points of interest major_axis, minor_axis = mh.features.ellipse_axes(binary_image) print(major_axis, minor_axis) Output While executing the above code, we get the following output − 722.1261184969184 479.52790970346524
Mahotas – Convolution of Image Convolution in image processing is used to perform various filter operations on an image. Here are some of them − Extract features − It is used to detect the features such as edges, corners, blobs, etc. by applying specific filters. Filtering − It is used to perform smoothing and sharpening operationson an image. Compression − we can compress the image by removing the redundant information in the image. Convolution of an Image in Mahotas In Mahotas, you can perform convolution on an image using the convolve() function. This function takes two arguments− the input image and the kernel; where, the kernel is a small matrix that defines the operation to be applied during convolution, such as blurring, sharpening, edge detection, or any other desired effect. Using the convolve() Function The convolve() function is used to perform convolution on an image. Convolution is a mathematical operation that takes two arrays− image and convolution kernel, and produces a third array (output image). The convolution kernel is a small array of numbers that is used to filter the image. The convolution operation is performed by multiplying the corresponding elements of the image and the kernel, and then adding the results together. The output of the convolution operation is a new image that has been filtered by the kernel. Following syntax to perform convolution in mahotas − convolve(image, kernel, mode=”reflect”, cval=0.0, out=None) where, image − It is the input image. kernel − It is the convolution kernel. mode − It specifies how to handle the edges of the image. It can be reflect, nearest constant, ignore, wrap, or mirror. Reflect is selected as default. cval − It specifies the value to use for pixels that are outside of the image. out − It specifies the output image. If the out argument is not specified, a new image will be created. Example In the following example, we first resize the input image ”nature.jpeg” to a ”4×4” shape using mh.imresize() function. Then, we create a ”4×4” kernel with all values set to 1. Finally, we use the mh.convolve() function to perform convolution − import mahotas as mh import numpy as np # Load the image image = mh.imread(”nature.jpeg”, as_grey=True) # Resize the image to 4×4 image = mh.imresize(image, (4, 4)) # Create a 4×4 kernel kernel = np.ones((4, 4)) # Perform convolution result = mh.convolve(image, kernel) print (result) Output Following is the output of the above code − [[3155.28 3152.84 2383.42 1614. ] [2695.96 2783.18 2088.38 1393.58] [1888.48 1970.62 1469.53 968.44] [1081. 1158.06 850.68 543.3 ]] Convolution with a Gaussian Kernel A Gaussian kernel in Mahotas is a small matrix of numbers that is used to blur or smoothen an image. The Gaussian kernel applies a weighted average to each pixel, where the weights are determined by a bell−shaped curve called the Gaussian distribution. The kernel gives higher weights to nearby pixels and lower weights to distant ones. This process helps to reduce noise and enhance features in the image, resulting in a smoother and more visually pleasing output. Following is the basic syntax of Gaussian kernel in mahotas − mahotas.gaussian_filter(array, sigma) Where, array − It is the input array. sigma − It is the standard deviation for Gaussian kernel. Example Following is an example of convolving an image with gaussian filter. Here, we are resizing the image, converting it to grayscale, applying a Gaussian filter to blur the image, and then displaying the original and blurred images side by side − import mahotas as mh import matplotlib.pyplot as mtplt # Load the image image = mh.imread(”sun.png”) # Convert to grayscale if needed if len(image.shape) > 2: image = mh.colors.rgb2gray(image) # Resize the image to 128×128 image = mh.imresize(image, (128, 128)) # Create the Gaussian kernel kernel = mh.gaussian_filter(image, 1.0) # Reduce the size of the kernel to 20×20 kernel = kernel[:20, :20] # Blur the image blurred_image = mh.convolve(image, kernel) # Creating a figure and subplots fig, axes = mtplt.subplots(1, 2) # Displaying original image axes[0].imshow(image) axes[0].axis(”off”) axes[0].set_title(”Original Image”) # Displaying blurred image axes[1].imshow(blurred_image) axes[1].axis(”off”) axes[1].set_title(”Gaussian Filter Image”) # Adjusting the spacing and layout mtplt.tight_layout() # Showing the figure mtplt.show() Output Output of the above code is as follows − Convolution with Padding Convolution with padding in Mahotas resfers to adding extra pixels or border around the edges of an image. before performing the convolution operation. The purpose of padding is to create a new image with increased dimensions, without losing information from the original image”s edges. Padding ensures that the kernel can be applied to all pixels, including those at the edges, resulting in a convolution output with the same size as the original image. Example Here, we have defined a custom kernel as a NumPy array to emphasize the edges of the image − import numpy as np import mahotas as mh import matplotlib.pyplot as mtplt # Create a custom kernel kernel = np.array([[0, -1, 0],[-1, 5, -1], [0, -1, 0]]) # Load the image image = mh.imread(”sea.bmp”, as_grey=True) # Add padding to the image padding = np.pad(image, 150, mode=”wrap”) # Perform convolution with padding padded_image = mh.convolve(padding, kernel) # Creating a figure and subplots fig, axes = mtplt.subplots(1, 2) # Displaying original image axes[0].imshow(image) axes[0].axis(”off”) axes[0].set_title(”Original Image”) # Displaying padded image axes[1].imshow(padded_image) axes[1].axis(”off”) axes[1].set_title(”Padded Image”) # Adjusting the spacing and layout mtplt.tight_layout() # Showing the figure mtplt.show() Output After executing the above code, we get the following output − Convolution with a Box Filter for Blurring Convolution with a box filter in Mahotas is a technique that can be used for blurring images. A box filter is a simple filter where each element in the filter kernel has the same value, resulting in a uniform weight distribution. Convolution with a box filter involves sliding the kernel over the image and taking the average of the pixel values in the region covered by the kernel. This average value is then used to replace the center pixel value in the output image. The process is repeated for all