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
Category: mahotas
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 – Installation We all have learnt earlier that Mahotas provides a wide range of tools for working with images including filtering, feature detection, segmentation, and more. If you are interested in using Mahotas in your Python projects, you will need to install it on your system. We can install Mahotas in several ways, including using pip, conda, or installing from source. In this tutorial, we will cover the steps required to install Mahotas on your machine using different methods. By the end of this tutorial, you will have Mahotas up and running on your system, ready to use for your image processing and computer vision tasks. Using Pip Pip is a package manager for Python that allows us to easily install and manage thirdparty libraries and packages. If we have Python and pip installed on our system, we can install Mahotas by running the following command in our terminal or command prompt− pip install mahotas This will download and install the latest version of Mahotas from the Python Package Index (PyPI) and install it on our system as shown in the output below− C:UsersLenovo>pip install mahotas Collecting mahotas Using cached mahotas-1.4.13-cp310-cp310-win_amd64.whl (1.7 MB) Requirement already satisfied: numpy in c:userslenovoappdatalocalprogramspythonpython310libsite-packages (from mahotas) (1.24.3) Installing collected packages: mahotas Successfully installed mahotas-1.4.13 Using Conda Conda is another package manager for Python that is often used for scientific computing and data analysis. If we are using the Anaconda distribution of Python, we can install Mahotas by running the following command in our terminal or Anaconda prompt− conda install -c conda-forge mahotas This will download and install Mahotas from the conda−forge channel (open−source packages) and install it in our Anaconda environment as shown in the output below− (base) C:UsersLenovo>conda install -c conda-forge mahotas Collecting package metadata (current_repodata.json): done Solving environment: The environment is inconsistent, please check the package plan carefully The following packages are causing the inconsistency: – defaults/win-64::anaconda-client==1.10.0=py39haa95532_0 – defaults/win-64::anaconda-navigator==2.1.4=py39haa95532_0 . . . The following packages will be UPDATED: ca-certificates pkgs/main::ca-certificates-2022.07.19~ –> conda-forge::cacertificates- 2023.5.7-h56e8100_0 openssl 1.1.1q-h2bbff1b_0 –> 1.1.1th2bbff1b By using the Conda−Forge channel, we can access a wide variety of packages and stay upto−date with the latest developments in the scientific computing and data analysis communities. If we don”t want to install conda-forge channel to our conda configuration permanently, we can use the command as shown below to install only mahotas package − conda install -c https://conda.anaconda.org/conda-forge mahotas From Source If we want to install a specific version of Mahotas or need to modify the source code, we can download the source distribution from PyPL − and install it manually. Steps to install mahotas from source To install from source, first we need to download the source distribution and extract it to a directory on our system. Then, we need to open a terminal or command prompt and navigate to the directory where we extracted the source code. Let us learn to install mahotas from source step-by-step − Step1 Install the required dependencies − Mahotas requires NumPy, SciPy, and OpenCV to be installed. We can install these packages using pip or our package manager. For example, using pip − pip install numpy scipy opencv-python Or, If you prefer to use your package manager (on Linux), use the below command − sudo apt-get install python-numpy python-scipy python-opencv Step 2 Download the Mahotas source code − You can download the source code from the official website − . Once you have downloaded the source code, extract it to a directory of your choice. Step 3 Build and install Mahotas − Open a terminal or command prompt and navigate to the directory where you extracted the Mahotas source code. Run the following command to build Mahotas − python mahotassource.py build Step 4 Once the build process completes successfully, run the following command to install Mahotas − sudo python mahotassource.py install After these steps, Mahotas should be installed on your system. You can test it by importing it in a Python script − import mahotas That”s it! You have successfully installed Mahotas from source. These were some of the most common methods of installing Mahotas, depending on your environment and preferences. Choose the method that works best for you and start using Mahotas for your computer vision and image processing tasks.
Mahotas – Loading an Image To perform any operation on an image, we first need to load it into memory. Once the image is loaded, we can access its pixels and apply various operations on it. Loading an image refers to reading an image file from a storage device (such as hard drive, USB drive, or network location) into memory. Loading an Image in Mahotas To load an image using Mahotas, we can use the imread() function, which reads an image file and returns it as a NumPy array. A NumPy array is a grid of values, all of the same type, and indexed by a tuple of nonnegative integers. In the case of an image, the array represents the pixel values of the image. Using the imread() Function The imread() function is the core method in Mahotas for reading and loading images. It accepts a file path as input and returns a NumPy array representing the loaded image. This function can read various image file formats, such as JPEG, PNG, BMP, TIFF, etc. Following is the basic syntax of imread() function in Mahotas − mahotas.imread(”image.file_format”) Where, ”image.file_format” is the actual path and format of the image you want to load. Example In the following example, we are using the imread() function to load an image file named “nature.jpeg” from the current directory. The resulting image is stored in the ”image” variable as a NumPy array − import mahotas as mh from pylab import imshow, show # Loading the image using Mahotas image = mh.imread(”nature.jpeg”) # displaying the original image imshow(image) show() Output Output of the above code is as follows − Loading Different Image Formats The image format refers to the different file formats used to store and encode images digitally. Each format has its own specifications, characteristics, and compression methods. Mahotas provides a wide range of image formats, including common formats like JPEG, PNG, BMP, TIFF, and GIF. We can pass the file path of an image in any of these formats to the imread() function. Example In this example, we demonstrate the versatility of Mahotas by loading images in different formats using the imread() function. Each loaded image is stored in a separate variable − import mahotas as ms import matplotlib.pyplot as mtplt # Loading JPEG image image_jpeg = ms.imread(”nature.jpeg”) # Loading PNG image image_png = ms.imread(”sun.png”) # Loading BMP image image_bmp = ms.imread(”sea.bmp”) # Loading TIFF image image_tiff = ms.imread(”tree.tiff”) # Creating a figure and subplots fig, axes = mtplt.subplots(2, 2) # Displaying JPEG image axes[0, 0].imshow(image_jpeg) axes[0, 0].axis(”off”) axes[0, 0].set_title(”JPEG Image”) # Displaying PNG image axes[0, 1].imshow(image_png) axes[0, 1].axis(”off”) axes[0, 1].set_title(”PNG Image”) # Displaying BMP image axes[1, 0].imshow(image_bmp) axes[1, 0].axis(”off”) axes[1, 0].set_title(”BMP Image”) # Displaying TIFF image axes[1, 1].imshow(image_tiff) axes[1, 1].axis(”off”) axes[1, 1].set_title(”TIFF Image”) # Adjusting the spacing and layout mtplt.tight_layout() # Showing the figure mtplt.show() Output The image displayed is as follows − Loading Color Images The color images are the ones we typically see, containing various colors. They are composed of three color channels− red, green, and blue. Each pixel”s color is determined by the combination of these three channels. Color images can represent a wide range of colors and are similar to what we see with our eyes. Loading color images in Mahotas means reading an image file that contains color information. The resulting image is represented as a 3D array, where each element represents the color values for each pixel in the red, green, and blue channels. Following is the basic syntax for loading grayscale Images in Mahotas − mahotas.imread(”image.file_format”) Where, ”image.file_format” is the actual path and format of the image you want to load. Example Following is an example of loading a color image in Mahotas − import mahotas as ms import matplotlib.pyplot as mtplt # Loading colored image colored_image = ms.imread(”nature.jpeg”) # Displaying colored image mtplt.imshow(colored_image) mtplt.axis(”off”) mtplt.show() Output Output of the above code is as follows − Loading Color and Grayscale image To load a grayscale image in mahotas, we need to pass the ”as_grey=True” parameter to the imread() function. Example In the following example, we are trying to load a grayscale image and a color image together using Mahotas − import mahotas as ms import matplotlib.pyplot as mtplt # Loading color image color_image = ms.imread(”tree.tiff”) # Loading grayscale image grayscale_image = ms.imread(”tree.tiff”, as_grey=True) # Creating a figure and subplots fig, axes = mtplt.subplots(1, 2) # Displaying colored image axes[0].imshow(color_image) axes[0].axis(”off”) axes[0].set_title(”Colored Image”) # Displaying grayscale image axes[1].imshow(grayscale_image, cmap=”gray”) axes[1].axis(”off”) axes[1].set_title(”Grayscaled Image”) # Adjusting the spacing and layout mtplt.tight_layout() # Showing the figure mtplt.show() Output The output is as shown below −
Mahotas – Displaying Shape of an Image When working with image data, there are scenarios when we need to display the shape of an image. Displaying the shape of an image refers to revealing the dimensions and characteristics of the image, such as its width, height, and color channels; where height corresponds to the number of rows, width corresponds to the number of columns, and channels indicate the number of color channels in the image (e.g., 3 for RGB images). Displaying the Shape of an Image in Mahotas In mahotas, we can display the shape of an image using the shape attribute of a NumPy array, which represents the image. By accessing this attribute, we can obtain the dimensions of the image and determine the appropriate operations to perform based on its shape. Let us discuss different steps and functions offered by Mahotas to extract and visualize the shape information, along with practical examples. Step1: Importing and Loading Images To begin, first we need to import the Mahotas library and load the image we want to analyze. Once Mahotas is installed, we can start working with image shape analysis. Step 2: Displaying the shape of an image To display the shape of an image in mahotas, we can use the shape attribute of a NumPy array. The shape attribute returns a tuple of integers representing the dimensions of the array. In the case of an image, it will provide information about its width, height, and channels. image_shape = image.shape print(“Image Shape:”, image_shape) This will print the shape of the loaded image in the format (height, width, channels). Step 3: Extracting Individual Dimensions When we talk about extracting individual dimensions of the shape in Mahotas, we are referring to obtaining specific information about the size and color components of an image. In simpler terms, an image has different properties like its height, width, and the number of color channels (such as red, green, and blue). Extracting individual dimensions means isolating and getting these specific pieces of information separately. height = image_shape[0] width = image_shape[1] channels = image_shape[2] print(“Height:”, height) print(“Width:”, width) print(“Channels:”, channels) By executing this code, we will access the dimensions of the image using indexing where, The first index corresponds to the height, The second index corresponds to the width The third index corresponds to the number of channels. This will give the individual dimensions of the image. Step 4: Checking for Grayscale Images The Grayscale images are black and white images, where each pixel represents the intensity or brightness of that particular point. It does not have any color information. Think of it as a black and white photograph. Sometimes, we encounter grayscale images, which have only one channel instead of the usual three channels for color images (Red, Green, and Blue). To determine if an image is grayscale, we can check if the number of channels is equal to 1. is_grayscale = channels == 1 if is_grayscale: print(“The image is grayscale.”) else: print(“The image is not grayscale.”) By executing this code, you will find out whether the loaded image is grayscale or not. Based on the result, we can proceed with the appropriate analysis. Step 5: Displaying the Shape Information on the Image Now, let”s explore how to display shape information on the image itself. We can draw shapes or add text overlays to highlight specific shape characteristics. This can be useful when presenting or saving the image with annotated shape information. import matplotlib.pyplot as plt # Create a figure and axes fig, ax = plt.subplots() # Display the image ax.imshow(image) # Add text for shape information ax.text(10, 20, f”Shape: {image_shape}”, color=”white”, fontsize=10, bbox=dict(facecolor=”black”)) # Remove axis ticks ax.set_xticks([]) ax.set_yticks([]) # Show the figure plt.show() When we will execute this code, it will display the image with the shape information overlaid on it. The shape information will be positioned at the specified coordinates, and the text will be displayed in white color on a black bounding box, making it more visible on the image. Complete Example Now, let us look at the complete code that encompasses all the steps discussed above − # Installing the library import mahotas as ms import matplotlib.pyplot as mtplt # Loading the image image = ms.imread(”sun.png”) # Displaying the shape of an image image_shape = image.shape print(“Image Shape:”, image_shape) # Extracting individual dimensions height = image_shape[0] width = image_shape[1] channels = image_shape[2] print(“Height:”, height) print(“Width:”, width) print(“Channels:”, channels) # Checking if the image is grayscale is_grayscale = channels == 1 if is_grayscale: print(“The image is grayscale.”) else: print(“The image is not grayscale.”) # Create a figure and axis fig, ax = mtplt.subplots() # Display the image ax.imshow(image) # Add text for shape information ax.text(350, 200, f”Shape: {image_shape}”, color=”white”, fontsize=8, bbox=dict(facecolor=”green”)) # Remove axis ticks ax.set_xticks([]) ax.set_yticks([]) # Display the image ax.imshow(image) # Add text overlay for dimensions text = f”Dimensions: {width}x{height}x{channels}” if not is_grayscale else f”Dimensions: {width}x{height} (Grayscale)” ax.text(18, 100, text, color=”red”, fontsize=12, fontweight=”bold”) # Remove axis ticks and labels ax.axis(”off”) # Show the image with shape information mtplt.show() Output After executing the above code, we get the following output− Image Shape: (1280, 843, 3) Height: 1280 Width: 843 Channels: 3 The image is not grayscale.
Mahotas – Computer Vision Computer vision, a subfield of artificial intelligence and computer science, focuses on enabling computers to gain a high−level understanding of visual information from images or videos. Inspired by human visual perception, computer vision aims to replicate and surpass human visual capabilities using algorithms, machine learning, and deep learning techniques. Computer vision is an interdisciplinary approach that uses computer science, engineering, mathematics, and databases to make a machine understand the visual data over the years. The availability of large datasets, deep learning, and training machine learning models made computer vision more functional. One such open−source computer vision and image processing library is mahotas. Mahotas is a computer vision library containing image processing operations such as filtering, morphological operations and classification as well as other modern computer vision functions. In this tutorial, we will explore the fundamental concepts, key techniques, and real−world applications of computer vision. Understanding Computer Vision Computer vision involves developing algorithms and models that allow computers to interpret and understand visual data. It encompasses a wide range of tasks, including image classification, object detection and tracking, image segmentation, facial recognition, scene understanding, and 3D reconstruction. The ultimate goal is to enable computers to extract meaningful information from visual data and make intelligent decisions based on that information. Foundations of Computer Vision Computer Vision draws its inspiration from the human visual system, aiming to replicate and even surpass human visual perception in certain tasks. The field finds its roots in the 1960s when researchers began exploring techniques for image recognition and pattern detection. Early approaches focused on handcrafted features and rule−based systems, but with the advent of machine learning and deep learning, computer vision experienced a revolutionary shift. Image Representation− The cornerstone of computer vision lies in how visualinformation is represented and processed. Pixels in images are transformed into numerical data, which can be analyzed and interpreted by algorithms. Feature Extraction− In image analysis, feature extraction plays a vital role in identifying relevant patterns and structures. Early methods included edge detection and corner detection, while modern approaches utilize deep learning to learn abstract features. Machine Learning and Deep Learning− Machine learning algorithms, particularly deep learning neural networks, have been instrumental in the rapid progress of computer vision. Convolutional Neural Networks (CNNs) have achieved remarkable success in tasks like image classification, object detection, and segmentation. Applications of Computer Vision The applications of computer vision are diverse and continually expanding as technology advances. Here are some key areas where computer vision has made a significant impact − Image Classification− Computer vision enables machines to classify objects and scenes in images with remarkable accuracy. From identifying everyday objects to recognizing specific species in nature, image classification has a wide range of applications. Object Detection− Object detection goes beyond classification by not only recognizing objects but also localizing them within the image. It is crucial in tasks such as surveillance, autonomous vehicles, and augmented reality. Image Segmentation− Image segmentation involves dividing an image into meaningful regions, facilitating further analysis and understanding. It is utilized in medical imaging, scene understanding, and video processing. Facial Recognition− Facial recognition technology has numerous applications, including biometric authentication, surveillance, and social media tagging. Optical Character Recognition (OCR)− OCR enables machines to recognize and convert printed or handwritten text in images into editable and searchable digital formats. It is widely used in document digitization and automation. Challenges in Computer Vision While computer vision has made tremendous strides, it still faces several challenges, some of which are − Limited Data− Deep learning models thrive on vast amounts of labeled data, and obtaining annotated datasets for every application can be cumbersome and expensive. Interpretability− Deep learning models are often considered “black boxes,” making it difficult to understand how they arrive at their decisions, which can be crucial in critical applications like healthcare and security. Robustness− Computer vision algorithms must be robust to variations in lighting conditions, viewpoints, and occlusions to perform reliably in real−world scenarios.
Mahotas – XYZ to LAB Conversion We have discussed about XYZ and LAB color space in our previous tutorial. Now let us discuss about the conversion of XYZ color space to LAB color space. To convert XYZ to LAB, we need to perform some calculations using specific formulas. These formulas involve adjusting the XYZ values based on a reference white point, which represents a standard for viewing colors. The adjusted values are then transformed into LAB components using mathematical equations. In simple terms, the XYZ to LAB conversion allows us to represent colors in a way that aligns better with how our eyes perceive them, making it easier to analyze and compare colors accurately. XYZ to LAB Conversion in Mahotas In Mahotas, we can convert an XYZ image to an LAB image using the colors.xyz2lab() function. The XYZ to LAB conversion in mahotas involves the following steps − Normalize XYZ values − First, we need to normalize the XYZ values by dividing them by the white point values. The white point represents the reference color that is considered pure white. This normalization step ensures that the color values are relative to the white point. Calculate LAB values − Once the XYZ values are normalized, mahotas uses a specific transformation matrix to convert them to LAB. This transformation takes into account the nonlinearities in human color perception and adjust the color values accordingly. Obtain LAB values − Finally, mahotas provides the LAB values for the color you started with. The resulting LAB values can then be used to describe the color in terms of its lightness and the two color axes. L component − The L component in LAB represents the lightness of the color and ranges from 0 to 100. Higher values indicate brighter colors, while lower values indicate darker colors. A and B components − The A and B components in LAB represent the color information. The A component ranges from green (-) to red (+), while the B component ranges from blue (-) to yellow (+). These components provide information about the color characteristics of the XYZ values. Using the mahotas.colors.xyz2lab() Function The mahotas.colors.xyz2lab() function takes an XYZ image as input and returns the LAB version of the image. The resulting LAB image retains the structure and overall content of the original XYZ image but updates the color of each pixel. Syntax Following is the basic syntax of the xyz2lab() function in mahotas − mahotas.colors.xyz2lab(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 a LAB image using the mh.colors.xyz2lab() function − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”sun.png”) # Converting RGB to XYZ xyz_image = mh.colors.rgb2xyz(image) # Converting XYZ to LAB lab_image = mh.colors.xyz2lab(xyz_image) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the XYZ image axes[0].imshow(xyz_image) axes[0].set_title(”XYZ Image”) axes[0].set_axis_off() # Displaying the LAB image axes[1].imshow(lab_image) axes[1].set_title(”LAB 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 − XYZ to LAB Conversion of a Random Image We can convert a randomly generated XYZ image to LAB color space by first creating an image with any desired dimensions. Next, assign random values to the X, Y, and Z channels for each pixel. The X, Y, and Z channels represent different color components. Once you have the XYZ image, you can then convert it to an LAB image. The resulting image will be in the LAB color space with distinct lightness and color channels. Example The following example shows conversion of a randomly generated XYZ image to an LAB image − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Function to create XYZ image def create_xyz_image(width, height): xyz_image = np.zeros((height, width, 3), dtype=np.float32) for y in range(height): for x in range(width): # Assign XYZ values to the pixel xyz_image[y, x, 0] = 0.035319468 xyz_image[y, x, 1] = 0.655582062 xyz_image[y, x, 2] = 0.157362328 return xyz_image # Defining the dimensions of the image width = 512 height = 512 # Generating the XYZ image xyz_image = create_xyz_image(width, height) # Converting XYZ to LAB lab_image = mh.colors.xyz2lab(xyz_image) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the XYZ image axes[0].imshow(xyz_image) axes[0].set_title(”XYZ Image”) axes[0].set_axis_off() # Displaying the LAB image axes[1].imshow(lab_image) axes[1].set_title(”LAB 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 – Features Mahotas is a popular image processing library. It has numerous functions on image processing and analysis. Some of its functionalities are given below − Feature Detection Mahotas can detect several features in the image using various functions like blob Detection, Harris corner detection, and SIFT features. These features are used in understanding the image, and extracting useful information and interesting patterns in the image. Image Filtering Mahotas is provided with many filtering functions such as mean filter, median filter, majority filter, rank filter etc. It also consists of filtering algorithms such as Gaussian and sobel filters. These filters are applied to the image to reduce noise and process the image as desired without losing its quality. Image Segmentation Mahotas can perform image segmentation very effectively. Some of the image segmentation functions includes− image thresholding, Watershed segmentation, distance transform etc. These algorithms divide the image into foreground and background to identify the objects in the image. Image Measurements Mahotas uses a variety of functions to measure various properties of the image including area, perimeter of the objects, centroid and bounding box. These measurements are used to find the size and orientation of the objects, further used in image analysis. Image Input Output One of the vital mahotas feature is that it can process images in various formats such as PNG, JPEG, TIFF, WEBP, BMP, and TIFF-based microscopy formats (LSM and STK). Mahotas can also write the outputs in the above mentioned formats. However these are not built−in formats in mahotas, but other libraries integrated with mahotas can support these formats. Other functions such as convex points calculations, Zernike & Haralick, TAS features, convolution, Sobel edge detection, Watershed, morphological processing, image thresholding, LBP etc. provides additional support in a wide range of image processing applications such as object recognition, medical image analysis and video processing. Connected Component Analysis Connected component analysis is a fundamental operation in image analysis that involves identifying and labeling connected regions in binary images. Mahotas provides functions to perform connected component analysis, allowing users to extract individual objects or regions of interest from the image. This operation is commonly used in applications like object counting, particle analysis, and image segmentation. Mathematical Morphology Mahotas offers a range of mathematical morphology operations, which allow users to analyze the shapes and structures within images. These operations include skeletonization, distance transform, and watershed transform. Skeletonization extracts the “skeleton” or centerline of objects in the image, while the distance transform provides information about the distance of each pixel to the nearest object boundary. The watershed transform is used for image segmentation based on the concept of water flow in a topographic map. Morphological Operations Mahotas includes a variety of morphological operations such as erosion, dilation, opening, and closing. These operations are fundamental in image segmentation, shape analysis, and feature extraction. Mahotas” efficient implementation of these operations enables users to process images quickly and accurately. Image Classification Mahotas supports image classification, enabling users to train machine learning models on extracted image features. By combining Mahotas” feature extraction capabilities with machine learning libraries like scikit−learn, users can perform tasks such as image recognition, object classification, and scene categorization.
Mahotas – History Mahotas is a powerful Python library for image processing and computer vision. It has gained widespread recognition and popularity among researchers, developers, and data scientists. The rich set of functionalities, efficient performance, and ease of use in mahotas have made it a valuable tool in various applications. In this tutorial, we will embark on a journey through the history of Mahotas, tracing its origins, major milestones, and the impact it has had on the field of image analysis. Origins and Early Development The development of Mahotas began in the late 2000s by Luis Pedro Coelho, a well known researcher in computer vision and image processing. It started when Coelho identified the need for a versatile and efficient image processing library. Coelho aimed to create a tool that would bridge the gap between Python and C++, providing researchers with the computational power of C++ and the ease of use of Python. Thus, the journey of Mahotas begun, with Coelho”s vision driving its initial development. Release and Growth Mahotas was officially released as an open−source project in 2010. It successfully marked a significant milestone in the field of image processing. Its initial release included a core set of functionalities that laid the foundation for subsequent developments. The library quickly garnered attention from the computer vision community, due to its comprehensive functionality and efficient implementation. Continuous Development and Expansion Since its beginning, Mahotas has witnessed continuous development and expansion. Researchers and developers from all around the world contributed to its growth due to the open−source nature of the project. This collaborative effort of the researchers led to the incorporation of new features, bug fixes, and performance improvements, making Mahotas a robust and reliable library for image processing. Integration with Scientific Python Ecosystem One of the key factors behind Mahotas” success is its seamless integration with the Scientific Python ecosystem. Mahotas is designed to work hand in hand with other popular libraries such as NumPy, SciPy, and scikit−image. This integration provides users with a comprehensive set of tools for data manipulation, scientific computing, and image analysis. The interoperability of Mahotas with these libraries has expanded its capabilities and enhanced its usability in various research domains. Adoption and Impact Over the years, Mahotas has gained significant adoption and made a substantial impact on the field of image analysis. Researchers and practitioners from diverse disciplines, including biomedicine, remote sensing, robotics, and industrial inspection, have utilized Mahotas for their image processing needs. Its efficient algorithms and functions have enabled groundbreaking research and applications, pushing the boundaries of what is possible in image analysis. Community and Support Mahotas owes much of its success to its vibrant and supportive community. The opensource nature of the project has fostered a collaborative environment where researchers and developers actively contribute to its development and improvement. The community provides valuable feedback, reports bugs, suggests new features, and shares their experiences and use cases, creating a rich ecosystem around Mahotas. Continuous Innovation and Future Prospects The development of Mahotas does not stand still. As new technologies and methodologies emerge, the Mahotas team continues to innovate and improve the library. The integration of deep learning techniques, advancements in 3D image processing, and the exploration of explainable AI are just a few areas where Mahotas can continue to evolve and make significant contributions. Additionally, the expanding use of Mahotas in real−time applications, edge computing, and embedded systems opens up new possibilities for its application in a variety of domains.
Mahotas – Loading Image as Grey The grayscale images are a kind of black−and−white images or gray monochrome, consisting of only shades of gray. The contrast ranges from black to white, which is the weakest intensity to the strongest respectively. The grayscale image only contains the brightness information and no color information. This is the reason of white being the maximum luminance (brightness) and black being the zero luminance, and everything in between consists shades of gray. Loading Grayscale Images In Mahotas, loading grayscale images involves reading an image file that only contains intensity values for each pixel. The resulting image is represented as a 2D array, where each element represents the intensity of a pixel. Following is the basic syntax for loading grayscale Images in Mahotas − mahotas.imread(”image.file_format”, as_grey=True) Where, ”image.file_format” is the actual path and format of the image you want to load and ”as_grey=True” is passed as an argument to indicate that we want to load the image as grayscale. Example Following is an example of loading a grayscale image in Mahotas − import mahotas as ms import matplotlib.pyplot as mtplt # Loading grayscale image grayscale_image = ms.imread(”nature.jpeg”, as_grey=True) # Displaying grayscale image mtplt.imshow(grayscale_image, cmap=”gray”) mtplt.axis(”off”) mtplt.show() Output After executing the above code, we get the output as shown below − Loading Different Image Formats as Grayscale The image format refers to the different file formats used to store and encode images digitally. Each format has its own specifications, characteristics, and compression methods. Mahotas provides a wide range of image formats, including common formats like JPEG, PNG, BMP, TIFF, and GIF. We can pass the file path of a grayscale image in any of these formats to the imread() function. Example In this example, we demonstrate the versatility of Mahotas by loading grayscale images in different formats using the imread() function. Each loaded image is stored in a separate variable − import mahotas as ms import matplotlib.pyplot as mtplt # Loading JPEG image image_jpeg = ms.imread(”nature.jpeg”, as_grey = True) # Loading PNG image image_png = ms.imread(”sun.png”,as_grey = True) # Loading BMP image image_bmp = ms.imread(”sea.bmp”,as_grey = True) # Loading TIFF image image_tiff = ms.imread(”tree.tiff”,as_grey = True) # Creating a figure and subplots fig, axes = mtplt.subplots(2, 2) # Displaying JPEG image axes[0, 0].imshow(image_jpeg) axes[0, 0].axis(”off”) axes[0, 0].set_title(”JPEG Image”) # Displaying PNG image axes[0, 1].imshow(image_png) axes[0, 1].axis(”off”) axes[0, 1].set_title(”PNG Image”) # Displaying BMP image axes[1, 0].imshow(image_bmp) axes[1, 0].axis(”off”) axes[1, 0].set_title(”BMP Image”) # Displaying TIFF image axes[1, 1].imshow(image_tiff) axes[1, 1].axis(”off”) axes[1, 1].set_title(”TIFF Image”) # Adjusting the spacing and layout mtplt.tight_layout() # Showing the figure mtplt.show() Output The image displayed is as follows − Using the Color Mode ”L” The “L” color mode represents luminance, which is a measure of the brightness of a color. It is derived from the RGB (Red, Green, Blue) color model, in which the intensity values of the red, green and blue channels are combined to calculate the grayscale intensity. The “L” mode discards the color information and represents the image using only the grayscale intensity values. To load image as grey by specifying the color mode as “L” in mahotas, we need to pass the parameter as_grey=”L” to the imread() function. Example In here, we are loading a grayscale image and specifying the color mode as ”L” − import mahotas as ms import matplotlib.pyplot as mtplt # Loading grayscale image image = ms.imread(”sun.png”) grayscale_image = ms.imread(”sun.png”, as_grey = ”L”) # 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 grayscale image axes[1].imshow(grayscale_image, cmap=”gray”) axes[1].axis(”off”) axes[1].set_title(”Grayscaled Image”) # Adjusting the spacing and layout mtplt.tight_layout() # Showing the figure mtplt.show() Output Following is the output of the above code −