Learn Mahotas – Centre of Mass of an Image work project make money

Mahotas – Centre of Mass of an Image The center of mass refers to the average position of the mass of an object. It is a point where the total mass of the object is concentrated. In simple terms, it represents the balancing point of an object. If the object is uniform and symmetric, the center of mass will be at its geometric center, otherwise not. Center of Mass of an Image in Mahotas The center of mass in Mahotas is determined by assigning a mass value to each pixel of the object, and then calculating the average position of these mass values. This results in the coordinates of the center of mass, which indicate the location of the object”s centroid within the image. Using mahotas.center_of_mass() Function The mahotas.center_of_mass() function is used to find the center of mass of an image. It calculates the average position (as a tuple of coordinates) of the pixel intensities in the image, providing a measure of where the “center” of the image is located. Following is the basic syntax of center_of_mass() function in mahotas − mahotas.center_of_mass(image) Were, image refers to the input image for which you want to find the center of mass. Example In the following example we are calculating the center of mass of the image ”nature.jpeg” − import mahotas as ms import numpy as np # Loading the image image = ms.imread(”nature.jpeg”) # Calculating the center of mass com = ms.center_of_mass(image) # Printing the center of mass print(“Center of mass of the image is:”, com) Output The center of mass is represented by a 3D coordinate in the form [x, y, z] as shown in the output below − Center of mass of the image is: [474.10456551 290.26772015 0.93327202] Center of Mass Using Numpy Functions The NumPy functions are built-in tools in the NumPy library that let you perform array operations and mathematical computations with ease in Python. To calculate the center of mass using NumPy functions in Mahotas, we need to determine the average position of the “weight” of an image. This is achieved by multiplying the x and y coordinates of each pixel by their intensity values, summing these weighted coordinates, and then dividing the result by the total sum of intensities. Following is the basic syntax to calculate center of mass of an image using numpy functions − com = np.array([np.sum(X * Y), np.sum(A * B)]) / np.sum(C) Where, ”X” and ”Y” represent the coordinate arrays, ”A” and ”B” represent the arrays corresponding to the values or intensities associated with the coordinates, and ”C” refers to the array representing the overall sum of the values or intensities. Example In here, we are creating a single coordinate array using. The first dimension of coords represents the y−coordinates and the second dimension represents the x−coordinates. We then access coords[1] to get the x−coordinates and coords[0] to get the y−coordinates when calculating the weighted sum − import mahotas as mh import numpy as np # Loading the image image = mh.imread(”tree.tiff”) # Creating a single coordinate array coords = np.indices(image.shape) # Calculating the weighted sum of x and y coordinates com = np.array([np.sum(coords[1] * image), np.sum(coords[0] * image)]) / np.sum(image) # Printing the center of mass print(“Center of mass:”, com) Output Following is the output of the above code − Center of mass: [ 7.35650493 -3.83720823] Center of Mass of a Specific Region The center of mass of a specific region is the region (area) of interest within the image. This could be a specific area, such as a bounding box or a selected region. The center of mass of a specific region is calculated by taking the weighted average of the x and y coordinates of each pixel in the ROI (Region of Interest), where the weights are the pixel intensities. The center of mass is returned as a tuple of two values representing the x and y coordinates, respectively. Example Following is example to calculate the center of mass of a region of interest of a grayscale image − import mahotas as mh import numpy as np # Load a grayscale image image = mh.imread(”nature.jpeg”, as_grey=True) # Defining a region of interest roi = image[30:90, 40:85] # Calculating the center of mass of the ROI center = mh.center_of_mass(roi) print(center) Output Output of the above code is as follows − [29.50213372 22.13203391]

Learn Mahotas – Cropping an Image work project make money

Mahotas – Cropping an Image Cropping an image refers to selecting and extracting a specific region of interest from an image and discarding the rest. It allows us to focus on a particular area or object within an image while removing irrelevant or unwanted portions. To crop an image in general, you need to define the coordinates or dimensions of the region you want to keep. Cropping an Image in Mahotas To crop an image using Mahotas, we can use NumPy array slicing operation to select the desired region of the image. We need to define the coordinates or dimensions of the desired ROI. This can be done by specifying the starting point, width, and height of the region to be cropped. By extracting and isolating the ROI, we can analyze, manipulate, or display only the relevant part of the image. Example In the following example, we are cropping the image to the desired size − import mahotas as mh import numpy as np import matplotlib.pyplot as plt image = mh.imread(”sun.png”) cropping= image[50:1250,40:340] # Create a figure with subplots fig, axes = plt.subplots(1, 2, figsize=(10, 5)) # Display the original image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].axis(”off”) # Display the cropped image axes[1].imshow(cropping, cmap=”gray”) axes[1].set_title(”Cropped Image”) axes[1].axis(”off”) # Adjust the layout and display the plot plt.tight_layout() plt.show() Output Following is an output of the above code − Cropping a Square Region To crop a square region in mahotas, we need to determine the starting and ending rows and columns. Here is an approach to calculate these values − Step 1 − Find the minimum dimension of the image. Step 2 − Compute the starting row by subtracting the minimum dimension from the total number of rows and dividing the result by 2. Step 3 − Calculate the ending row by adding the starting row to the minimum dimension. Step 4 − Compute the starting column using a similar approach. Step 5 − Calculate the ending column by adding the starting column to the minimum dimension. Using the calculated starting and ending rows and columns, we can crop the square region from the image. We accomplish this by indexing the image array with the appropriate row and column ranges. Example Here, we are trying to crop an image in a square region − import mahotas as mh import numpy as np import matplotlib.pyplot as plt image = mh.imread(”tree.tiff”) # Get the minimum dimension size = min(image.shape[:2]) # Calculating the center of the image center = tuple(map(lambda x: x // 2, image.shape[:2])) # Cropping a square region around the center crop = image[center[0] – size // 2:center[0] + size // 2, center[1] – size // 2:center[1] + size // 2] # Create a figure with subplots fig, axes = plt.subplots(1, 2, figsize=(10, 5)) # Display the original image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].axis(”off”) # Display the cropped image axes[1].imshow(crop, cmap=”gray”) axes[1].set_title(”Cropped Image”) axes[1].axis(”off”) # Adjust the layout and display the plot plt.tight_layout() plt.show() Output After executing the above code, we get the following output − Cropping a Circular Region To crop the image to a circular region in mahotas, we need to determine the center coordinates and radius of the circle. We can achieve this by calculating the center as the midpoint of the image dimensions and setting the radius as half the minimum dimension. Next, we create a boolean mask with the same dimensions as the image, where True values indicate the pixels within the circular region. We accomplish this by calculating the distance of each pixel from the center and setting True for pixels that fall within the specified radius. Now that we have the circular mask, we can apply it to the image by setting the values outside the circular region to zero. Finally, we get the cropped image. Example Now, we are trying to crop an image in a circular region − import mahotas as mh import numpy as np import matplotlib.pyplot as plt image = mh.imread(”sun.png”) # Calculating the center of the image center = tuple(map(lambda x: x // 2, image.shape[:2])) # Calculating the radius as half the minimum dimension radius = min(image.shape[:2]) // 2 # Creating a boolean mask of zeros mask = np.zeros(image.shape[:2], dtype=bool) # Creating meshgrid indices y, x = np.ogrid[:image.shape[0], :image.shape[1]] # Setting mask values within the circular region to True mask[(x – center[0])**2 + (y – center[1])**2 <= radius**2] = True crop = image.copy() # Setting values outside the circular region to zero crop[~mask] = 0 # 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 cropped image axes[1].imshow(crop, cmap=”gray”) axes[1].set_title(”Cropped Image”) axes[1].axis(”off”) # Adjust the layout and display the plot plt.tight_layout() plt.show() Output The output obtained is as shown below −

Learn Mahotas – Dilating an Image work project make money

Mahotas – Dilating an Image Dilating an image in image processing refers to expanding the pixels of an image. The dilation process will add pixels around the edges of an image. This happens because the algorithm looks at each pixel in the image and checks its neighboring pixels. If any of the neighboring pixels are part of an object, it adds those pixels to the object”s boundary. Dilating an Image in Mahotas By dilating an image in Mahotas, we refer to adding the number of pixels to the boundaries of regions in an image. This operation is commonly used to enhance or modify the shapes and structures in an image. We can dilate an image in mahotas using the dilate() function. It is used to expand an element A by using structuring element B. A structuring element is a small matrix or shape that defines the neighborhood around each pixel. It is used to determine which pixels should be considered during the dilation process. The mahotas.dilate() function The mahotas.dilate() function takes the input image and the structuring element as arguments, and returns a new Numpy array. The value of the output pixel is determined as the maximum value of all the pixels in the neighborhood. If any neighboring pixels have a value of 1, the output pixel is set to 1. The dilate() function scans the image, pixel by pixel, and checks the neighborhood defined by the structuring element. If any neighboring pixels are part of an object, the dilation operation adds those pixels to the object”s boundary, making it larger. Syntax Following is the basic syntax of the dilate() function in mahotas − mahotas.dilate(A, Bc=None, out=None, output=None) where, A − It is the input image on which dilation will be performed. It can be a 2D or 3D NumPy array representing grayscale or binary image data. Bc (optional) − It is the structuring element used for dilation. Default is None. out (deprecated) / output (optional) − It specifies the output array to store the result. If not provided, a new array is created and returned as the output. Example Following is the basic example to dilate an image in mahotas using the dilate() function − import mahotas as mh import matplotlib.pyplot as plt import numpy as np image = mh.imread(”nature.jpeg”, as_grey=True).astype(np.uint8) # Performing dilation with a square kernel of size 3×3 dilated_image = mh.dilate(image, Bc=mh.disk(3)) # Create a figure with subplots fig, axes = plt.subplots(1, 2, figsize=(7,5 )) # Display the original image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].axis(”off”) # Display the dilated image axes[1].imshow(dilated_image, cmap=”gray”) axes[1].set_title(”Dilated Image”) axes[1].axis(”off”) # Adjust the layout and display the plot plt.tight_layout() plt.show() Output After executing the above code, we get the output as follows − Dilation with Varying Structuring Element Sizes We can also dilate an image using different structuring element sizes to enhance different aspects of the image. These elements are created with different sizes using the Mahotas disk() function. By choosing different radii or sizes for the structuring elements, we can achieve diverse effects. To begin, we perform the dilation using the largest structuring element. Then, we continue with additional dilations using smaller structuring elements. This approach allows us to modify the image in multiple ways, enhancing specific features and achieving the desired visual effects. Example In here, we are trying to dilate an image with varying structuring element sizes − import mahotas as mh import numpy as np from pylab import imshow, show image = mh.imread(”nature.jpeg”, as_grey=True).astype(np.uint8) # Performing dilation with the largest structuring element largest_se = mh.disk(8) dilated_image = mh.dilate(image, Bc=largest_se) # Performing additional dilations with smaller structuring elements smaller_se_1 = mh.disk(2) smaller_se_2 = mh.disk(5) dilated_image = mh.dilate(dilated_image, Bc=smaller_se_1) dilated_image = mh.dilate(dilated_image, Bc=smaller_se_2) # Displaying the dilated image imshow(dilated_image) show() Output The output obtained is as shown below − Dilation with a Circular-shaped Kernel To create a circular−shaped kernel, we can use the disk() function from Mahotas. By specifying the desired radius, this function generates a NumPy array representing the circular kernel. Once we have the image and circular kernel ready, we can proceed to perform dilation. This operation applies the circular kernel to each pixel of the image, expanding the white regions accordingly. In simpler terms, it enhances the bright areas in the image, making them larger. Example Now, we are dilating an image with a circular−shaped kernel − import mahotas as mh import numpy as np from pylab import imshow, show # Load image image = mh.imread(”sun.png”, as_grey=True).astype(np.uint8) # Circular kernel with radius 5 radius = 5 kernel = mh.disk(radius) dilated_image = mh.dilate(image, kernel) # Display the dilated image imshow(dilated_image) show() Output Following is the output of the above code −

Learn Mahotas – Getting Image Moments work project make money

Mahotas – Getting Image Moments Image moment is the statistical measure that is used to describe various properties of an image. It provides information about the shape, location, orientation of the objects and intensity of objects within an image. In general, image moments are calculated by summing up the product of pixel intensities and their corresponding spatial coordinates. These moments can be used to derive useful features such as centroid (mean position), area, orientation, and scale of objects in an image. Higher−order moments can capture more complex shape characteristics. Getting Image moments in Mahotas In the context of Mahotas, image moments are calculated using the mahotas.moments() function. This function takes an image as input and returns a set of moments that characterize the image. Mahotas provides various types of moments, including raw moments, central moments, normalized moments, and Hu moments. These moments can be useful for tasks such as object recognition, image alignment, and shape analysis. Using the mahotas.moments() Function The mahotas.moments() function is used to get the moments of an image or a region of interest (ROI). This function takes an image object as input and returns a numpy array containing the computed moments. A NumPy array is like a table of data where each value is arranged in a grid, and you can perform calculations on the entire grid or specific parts of it easily. Following is the basic syntax of the moments() function in Mahotas − mahotas.moments(image, p0, p1, cm=(0, 0), convert_to_float=True) where, p0 − It is the power of the first dimension (float) p1 − It is the power of the second dimension (float) image − It is the input image and it should be a 2−D array cm − It is the center of mass and (o,o)is taken as default. Example In the example given below, we are using the moments() function to get image moments − import mahotas as mh import numpy as np from pylab import imshow, show image = mh.imread(”sun.png”) # extracting the first channel (0th index) of the image array using slicing cimage=image[:,:,0] p0=5.5 p1=5.5 moment = mh.moments(cimage,p0,p1) print(moment) Output Following is the output of the above code − 2.971238276705602e+39 Getting Image Moments by Specifying Center of Mass The center of mass is a measure of the average position of the pixels, weighted by their intensities. By specifying the center of mass, we can obtain moments that are relative to that particular location. We can specify the center of mass to get image moments by passing the ”cm” parameter to the moments() function. By setting the center of mass, we can shift the coordinate system and obtain moments that are relative to a specific position within the image. Example In here, we are getting the image moment by specifying the center of mass co-ordinates − import mahotas as mh image = mh.imread(”nature.jpeg”, as_grey = True) moments = mh.moments(image, p0=1, p1=0, cm=(100, 100)) print(moments) Output After executing the above code, we get the following output − 40074427849.0 Getting Image moments by Disabling Float Conversion By disabling float conversion, we preserve the original data type of the input image during the moment calculation. This is helpful when we work with specific image formats, such as grayscale or binary images, where the pixel values are already in integer format. We can disable the conversion of the input image to a floating−point representation during the calculation of image moments in mahotas, by passing the ”convert_to_float” parameter to the moments() function. The convert_to_float parameter is explicitly set to False. This ensures that the input image is not converted to a floating−point representation during the moment calculation. Example In the following example, we are getting image moments by disabling float conversion − import mahotas as mh image = mh.imread(”tree.tiff”, as_grey = True) moments = mh.moments(image, p0=2, p1=0, cm=(0, 0), convert_to_float=False) print(moments) Output Output of the above code is as follows − 11029976739711.432 Getting Higher Order Image Moments Higher−order image moments provide more detailed information of the image”s pixel distribution, symmetry, and shape characteristics. These moments help us to capture complex patterns and variations that lower−order moments might overlook. We can get the higher order image moments in mahotas by specifying the desired order of moments using the ”p0” and ”p1” parameters, where higher values represent higher orders. Example Here, we are trying to get the higher order image moments − import mahotas as mh image = mh.imread(”sea.bmp”, as_grey = True) moments = mh.moments(image, p0=3, p1=3, cm=(10, 10)) print(moments) Output The output of the above code is as follows − 2.3690172519584466e+24

Learn Mahotas – RGB to Gray Conversion work project make money

Mahotas – RGB to Gray Conversion RGB to grayscale conversion in image processing transforms a colored image in the RGB color space to a grayscale image. RGB images are composed of three color channels− red, green, and blue. Each pixel in an RGB image is represented by a combination of intensity values for these three channels, resulting in a wide range of colors. On the other hand, grayscale images are single−channel images (contain only shades of (gray) where each pixel represents the intensity of the corresponding location in the original image. The intensity values range from black (0) to white (255), with intermediate shades of gray. RGB to Gray Conversion in Mahotas In Mahotas, we can convert an RGB image to grayscale image using the colors.rgb2gray() function. The function calculates the grayscale intensity of each pixel based on the weighted average of its RGB values. The weights reflect the human perception of colors, with red having the highest weight, followed by green, and then blue. The mahotas.colors.rgb2gray() Function The mahotas.colors.rgb2gray() function takes an RGB image as input and returns the grayscale version of the image. The resulting grayscale image retains the structure and overall content of the original RGB image but lacks color information. Syntax Following is the basic syntax of the rgb2gray() function in mahotas − mahotas.colors.rgb2gray(rgb_image, dtype=float) where, rgb_image − 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 a grayscale image using the mh.colors.rgb2gray() function − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”nature.jpeg”) # Converting it to grayscale gray_image = mh.colors.rgb2gray(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 grayscale image axes[1].imshow(gray_image, cmap=”gray”) axes[1].set_title(”Grayscale 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 Average of RGB Channels We can also convert an RGB image to grayscale image using average of RGB channels. The intensity of red, green, and blue color of each pixel is summed and divided by three to obtain the average intensity value. We can achieve this using the mean() function of the numpy library. The resulting image will be a grayscale image having a single channel where each pixel represents the average intensity across the RGB channels, where each color channel contributes equally to the overall grayscale intensity. Example The following example shows conversion of an RGB image to grayscale using average of RGB channels − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”sun.png”) # Converting it to grayscale gray_image = np.mean(image, axis=2).astype(np.uint8) # 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 grayscale image axes[1].imshow(gray_image, cmap=”gray”) axes[1].set_title(”Grayscale Image”) axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output Output of the above code is as follows − Using Luminosity Luminosity is a term used to describe the perceived brightness or lightness of an object or image. In image processing, luminosity assigns specific weights to the red, green, and blue color channels of each pixel and combines them to calculate the grayscale intensity value. The luminosity in the RGB color space can be calculated using the following formula − Luminosity = 0.2989 * R + 0.5870 * G + 0.1140 * B Where, the values 0.2989, 0.5870, and 0.1140 are the weights assigned to the red, green, and blue channels, respectively. These weights are derived from the standard Rec.709 color space used for digital displays. This method produces better results compared to simply averaging the RGB channels, as it better captures the visual perception of the original image. Example Here, we have defined the luminosity of RGB to convert a colored image to its equivalent grayscale image − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”tree.tiff”) # Converting it to gray gray_image = np.dot(image[…, :3], [0.2989, 0.5870, 0.1140]).astype(np.uint8) # 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 grayscale image axes[1].imshow(gray_image, cmap=”gray”) axes[1].set_title(”Grayscale Image”) axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output After executing the above code, we get the following output −

Learn Mahotas – Stretching Gamma Correction work project make money

Mahotas – Stretching Gamma Correction Stretching gamma correction refers to enhancing the overall contrast of an image. This is done by increasing the gamma value, which expands the range of intensity levels of the pixels of the image. The process of stretching gamma correction involves stretching the original input values to a new broader range of values. Stretching Gamma Correction in Mahotas In Mahotas, we can do stretching gamma correction of an image by using the mahotas.stretch() function. In gamma correction, a gamma value greater than 1 increases the contrast of the image, while a gamma value less than 1 decreases the contrast. Hence, by stretching the gamma, dark areas of the image become darker, and bright areas become brighter, resulting in a more pronounced distinction between different shades and details. Using the mahotas.stretch() Function The mahotas.stretch() function takes an image as input and returns a sharpened version of the image as output. The resulting image has enhanced contrast and improved visibility of details. The stretch() function determines the minimum and maximum intensity values in the image and transforms them to full range of pixel values (0−255 for 8−bit images). Syntax Following is the basic syntax for mh.stretch() function in mahotas − mahotas.stretch(img, arg0=None, arg1=None, dtype=<class ”numpy.uint8”>) where, image − It is the input image. arg0 (optional) − It is the minimum value for output (default is 0). arg1 (optional) − It is the maximum value for output (default is 255). dtype (optional) − It is the data type of output image (default is uint8). Example In the following example, we are increasing the contrast of a grayscale image by using mh.stretch() function − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”sun.png”) # Converting to grayscale gray_image = mh.colors.rgb2gray(image) # Decreasing gamma value corrected_gamma = 3.2 # Applying stretch gamma correction image stretch_gamma_corrected = mh.stretch(gray_image, corrected_gamma) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(gray_image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying Stretched gamma corrected image axes[1].imshow(stretch_gamma_corrected, cmap=”gray”) axes[1].set_title(”Stretched Gamma Corrected Image”) axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output Following is the output of the above code − Stretching Gamma Correction of RGB Image We can also perform stretching gamma correction for an RGB image in mahotas using the stretch() function. The gamma value used in the stretch function determines the extent of contrast enhancement. We can then convert the stretched image back to the RGB color space, by multiplying it by 255 (maximum intensity of an RGB image). Example The following example shows increasing of the contrast of an RGB image − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”nature.jpeg”) # Applying stretched gamma correction stretched_gamma_corrected = mh.stretch(image, 3) # Converting the image back to RGB stretched_gamma_corrected = stretched_gamma_corrected * 255 # Creating subplots to display images fig, axes = mtplt.subplots(1, 2) # Displaying the original RGB image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the stretched image axes[1].imshow(stretched_gamma_corrected) axes[1].set_title(”Stretched Gamma Corrected Image”) axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output Output of the above code is as follows − Interactive Stretching Gamma Correction Slider An interactive stretching gamma correction slider is a GUI element that allows users to adjust the gamma value to change the contrast dynamically by dragging the slider. To stretch gamma correction using an interactive slider in Mahotas, first create a slider for adjusting the gamma value. Then, implement a function to retrieve the new gamma value when the slider is moved and apply stretching gamma correction to the image. Finally, connect the function to the slider”s value change event, so it is called automatically when the slider is moved. Syntax Following is the basic syntax to create an interactive slider − from matplotlib.widgets import Slider Slider(slider_axis, name, min_value, max_value, valint) where, slider_axis − It is a list that defines the position and dimensions of the slider. name − It is the name of the slider. mini_value − It is the minimum value that the slider can go to. max_value − It is the maximum value that the slider can go to. valint − It is the starting value of the slider. Example In this example we are increasing the contrast using an interactive slider − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt from matplotlib.widgets import Slider # Loading the image image = mh.imread(”tree.tiff”) # Creating a figure and axes for the plot fig, axis = mtplt.subplots() # Displaying the original image axis.imshow(image) axis.set_title(”Stretching Gamma Correction”) axis.set_axis_off() # Creating a slider for stretched gamma adjustment slider_axis = mtplt.axes([0.2, 0.05, 0.6, 0.03]) stretched_gamma_slider = Slider(slider_axis, ”Stretched Gamma”, 0.1, 5.0, valinit=1.0) # Updating the stretched gamma correction and plot on change of slider value def update_stretched_gamma(val): stretched_gamma = stretched_gamma_slider.val corrected_image = mh.stretch(image, stretched_gamma) corrected_image = corrected_image * 255 axis.imshow(corrected_image) fig.canvas.draw_idle() stretched_gamma_slider.on_changed(update_stretched_gamma) # Showing the figure mtplt.show() Output The output produced is as follows −

Learn Mahotas – Local Maxima in an Image work project make money

Mahotas – Local Maxima in an Image A local maximum is a pixel or a particular region in an image that has a higher intensity or value than its neighboring pixels or regions. It represents a peak or a high point in the image data. One way to find local maxima is by performing a local neighborhood analysis. For each pixel in the image, its neighborhood is examined, and if the pixel is the maximum within its neighborhood, it is considered a local maximum. Local Maxima in an Image in Mahotas We can find the local maxima in an image in Mahotas using the locmax() function. It takes an image as input and returns a binary mask where the local maxima are marked as True or 1. The local_maxima() function in Mahotas uses a non-maximum suppression algorithm to locate the local maxima efficiently. By examining each pixel and its neighborhood, the function determines whether the pixel is the maximum within its local region. This analysis allows for the detection of peaks or high points in the image data, which can play an important role for various applications such as feature extraction, object detection, and image segmentation. The non−maximum suppression algorithm is used in object detection and edge detection tasks to eliminate redundant and weak detections by selecting the highest intensity or response value among neighboring pixels, thereby keeping only the local maxima and suppressing non−maximum values. Using the locmax() Function The locmax() function in Mahotas is used to identify local maxima in an input image efficiently. It takes a grayscale or single channel image as input and returns a binary mask where the local maxima are marked as True or 1. Syntax Following is the basic syntax of the locmax() function in mahotas − mahotas.Locmax(image_name) where, ”image_name” is the input image. Example Following is the basic example to find the local maxima in an image − import mahotas as mh import numpy as np from pylab import imshow, show import matplotlib.pyplot as plt image = mh.imread(”nature.jpeg”, as_grey=True) maxima = mh.locmax(image) print(“Maxima:”, maxima) imshow(maxima) show() Output Following is the output of the above code − Maxima: [[ True True True … True True True] [ True True True … True True True] [ True True True … True True True] … [False False False … False False False] [False False False … False False True] [ True False True … False False True]] The image displayed is as shown below − Using the regmax() Function We can also use the regmax() function in Mahotas for finding regional maxima in an image. A regional maximum is defined as a point in the image that has a higher intensity value than all of its neighboring pixels within a specified region. The regmax() function accepts an image as the input parameter and returns a boolean image of the same size as an input image. Following is the basic syntax of regmax function in mahotas − regmax(image) where, ”image” is a grayscale or color image on which regional maxima need to be identified. Example In here, we are trying to find the regional maxima within the connected regions in a grayscale image using the regmax() function − import mahotas as mh def find_local_maxima(image): regional_maxima = mh.regmax(image) return regional_maxima image = mh.imread(”sun.png”, as_grey=True) local_maxima_points = find_local_maxima(image) print(local_maxima_points) Output Output of the above code is as follows − [[False False False … False False False] [False False False … False False False] [False False False … False False False] … [False False False … False False False] [False False False … False False False] [False False False … True False False]] Regional Maxima of a Colored Image We can also find the regional maxima within the connected regions in a color image using the regmax() function. Example Now, we are trying to find the regional maxima within the connected regions in a color image using the regmax() function − import mahotas as mh def find_local_maxima(image): regional_maxima = mh.regmax(image) return regional_maxima image = mh.imread(”tree.tiff”) local_maxima_points = find_local_maxima(image) print(local_maxima_points) Output We get the output as follows − [[[False False False] [ True True True] [False False False] … [False False False] [False False False] [False False False]] . . . [False False False] [False False False] [ True False False]]]

Learn Mahotas – Eccentricity of an Image work project make money

Mahotas – Eccentricity of an Image The eccentricity of an image refers to the measure of how elongated or stretched the shape of an object or region within the image is. It provides a quantitative measure of how how much the shape deviates from a perfect circle. The eccentricity value ranges between 0 and 1, where − 0 − Indicates a perfect circle. Objects with an eccentricity of 0 have the least elongation and are perfectly symmetric. Close to 1 − Indicates increasingly elongated shapes. As the eccentricity value approaches 1, the shapes become more elongated and less circular. Eccentricity of an Image in Mahotas We can calculate the eccentricity of an image in Mahotas using the ”mahotas.features.eccentricity()” function. If the eccentricity value is higher, it indicates that the shapes in the image are more stretched or elongated. On the other hand, if the eccentricity value is lower, it indicates that the shapes are closer to being perfect circles or less elongated. The mahotas.features.eccentricity() function The eccentricity() function in mahotas helps us measure how stretched or elongated the shapes are in an image. This function takes an image with single channel as input and returns a float point number between 0 and 1. Syntax Following is the basic syntax of the eccentricity() function in mahotas − mahotas.features.eccentricity(bwimage) Where, ”bwimage” is the input image interpreted as a boolean value. Example In the following example, we are finding the eccentricity of an image − import mahotas as mh import numpy as np from pylab import imshow, show image = mh.imread(”nature.jpeg”, as_grey = True) eccentricity= mh.features.eccentricity(image) print(“Eccentricity of the image =”, eccentricity) Output Output of the above code is as follows − Eccentricity of the image = 0.8902515127811386 Calculating Eccentricity using Binary Image To convert a grayscale image into a binary format, we use a technique called thresholding. This process helps us to separate the image into two parts − foreground (white) and background (black). We do this by picking a threshold value (indicates pixel intensity), which acts as a cutoff point. Mahotas simplifies this process for us by providing the “>” operator, which allows us to compare pixel values with the threshold value and create a binary image. With the binary image ready, we can now calculate the eccentricity. Example Here, we are trying to calculate the eccentricity of a binary image − import mahotas as mh image = mh.imread(”nature.jpeg”, as_grey=True) # Converting image to binary based on a fixed threshold threshold = 128 binary_image = image > threshold # Calculating eccentricity eccentricity = mh.features.eccentricity(binary_image) print(“Eccentricity:”, eccentricity) Output After executing the above code, we get the following output − Eccentricity: 0.7943319646935899 Calculating Eccentricity using the Skeletonization Skeletonization, also known as thinning, is a process that aims to reduce the shape or structure of an object, representing it as a thin skeleton. We can achieve this using the thin() function in mahotas. The mahotas.thin() function takes a binary image as input, where the object of interest is represented by white pixels (pixel value of 1) and the background is represented by black pixels (pixel value of 0). We can calculate the eccentricity of an image using skeletonization by reducing the image to its skeleton representation. Example Now, we are calculating the eccentricity of an image using the skeletonization − import mahotas as mh import matplotlib.pyplot as plt # Read the image and convert it to grayscale image = mh.imread(”tree.tiff”) grey_image = mh.colors.rgb2grey(image) # Skeletonizing the image skeleton = mh.thin(grey_image) # Calculating the eccentricity of the skeletonized image eccentricity = mh.features.eccentricity(skeleton) # Printing the eccentricity print(eccentricity) # Create a figure with subplots fig, axes = plt.subplots(1, 2, figsize=(7,5 )) # Display the original image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].axis(”off”) # Display the skeletonized image axes[1].imshow(skeleton, cmap=”gray”) axes[1].set_title(”Skeletonized Image”) axes[1].axis(”off”) # Adjust the layout and display the plot plt.tight_layout() plt.show() Output The output obtained is as shown below − 0.8975030064719701 The image displayed is as shown below −

Learn Mahotas – Image Stretch RGB work project make money

Mahotas – Image Stretch RGB Image stretching is a technique of enhancing the contrast of an image by mapping the original pixel values to a new range of values. This is generally done to improve the visibility of details and enhance the overall appearance of the image. In the case of RGB images, each pixel has three color channels− red, green, and blue. Stretching the RGB image involves independently stretching the values of each color channel to expand the range of intensities. Image Stretch RGB in Mahotas In Mahotas, RGB image stretching is performed by stretching the individual color channels (Red, Green, and Blue) of the image. The stretching process involves mapping the original pixel intensities to a new range to increase the contrast. After stretching the individual RGB channels, they need to be combined back to create the final stretched image. We can perform image stretching RGB in mahotas using the stretch() function and the stretch_rgb() function. Using the stretch() and the stretch_rgb() Functions The mahotas.stretch() and mahotas.stretch_rgb() functions stretches the pixel values in each channel of the RGB image between the specified min_value and max_value. Pixels with values below min_value will be set to min_value, and pixels with values above max_value will be set to max_value. The stretching is performed independently on each channel. Syntax Following is the basic syntax for mahotas.stretch() function − mahotas.stretch(img,arg0=None, arg1=None, dtype=<type ”numpy.uint8”>) Following is the basic syntax of mahotas.stretch_rgb() function − mahotas.stretch(img,arg0=None, arg1=None, dtype=<type ”numpy.uint8”>) Parameters Following are the parameters passed to the mahotas.stretch() function − img − It is the input image. arg0 and arg1 (optional) − These parameters specify the range within which the pixel values should be stretched. The interpretation of these arguments depends on their values − If both arg0 and arg1 are provided and are not None, they represent the minimum and maximum values that the pixel values should be stretched to. The pixel values below arg0 will be set to arg0, and pixel values above arg1 will be set to arg1. If only arg0 is provided and arg1 is None, arg0 should be a tuple or list of length 2 representing the percentile values. The pixel values below the value at the first element of arg0 will be set to that value, and pixel values above the value at the second element of arg0 will be set to that value. If both arg0 and arg1 are None, the pixel values will be stretched to the full range of the data type specified by the dtype parameter (default is numpy.uint8). dtype − It specifies the data type of the output image. The default value is numpy.uint8, which represents an unsigned 8-bit integer. Example In the following example, we are using the mahotas.stretch() function to stretch the image− import mahotas as mh import numpy as np import matplotlib.pyplot as plt image = mh.imread(”sun.png”) # Stretch image stretched = mh.stretch(image, arg0=100, arg1=280) # Create a figure with subplots fig, axes = plt.subplots(1, 2, figsize=(10, 5)) # Display the original image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].axis(”off”) # Display the stretched grayscale image axes[1].imshow(stretched, cmap=”gray”) axes[1].set_title(”Stretched Grayscale Image”) axes[1].axis(”off”) # Adjust the layout and display the plot plt.tight_layout() plt.show() Output Following is the output of the above code − Example In this example, we will see how to use mahotas.stretch_rgb function to stretch an image − import mahotas as mh import numpy as np import matplotlib.pyplot as plt image = mh.imread(”nature.jpeg”) # Stretch image stretched = mh.stretch_rgb(image, arg0=100, arg1=280) # Create a figure with subplots fig, axes = plt.subplots(1, 2, figsize=(10, 5)) # Display the original image axes[0].imshow(image) axes[0].set_title(”Original Image”) axes[0].axis(”off”) # Display the stretched grayscale image axes[1].imshow(stretched, cmap=”gray”) axes[1].set_title(”Stretched Grayscale Image”) axes[1].axis(”off”) # Adjust the layout and display the plot plt.tight_layout() plt.show() Output Following is the output of the above code − Stretching Image with Percentile Value To stretch an RGB image with percentile values in Mahotas, we can manually calculate the percentiles and use the stretch function individually on each color channel − Firstly, split the RGB image into individual color channels (red, green, and blue). This can be achieved by using the np.split() function from the NumPy library. Once the image is split, calculate the desired percentiles for each color channel using NumPy”s np.percentile() function. After obtaining the percentiles, use the stretch() function from Mahotas to each channel independently, using the calculated minimum and maximum values. This will stretch the pixel values within the desired percentile range for each channel. Finally, merge the stretched channels back into an RGB image by concatenating them along the color channel axis. The resulting image will be the stretched RGB image based on the specified percentiles. Example In here, we are trying to stretch an image with percentile value − import mahotas as mh import numpy as np from pylab import imshow, show image = mh.imread(”nature.jpeg”) # Splitting the RGB image into individual channels r, g, b = np.split(image, 3, axis=2) # Calculating percentiles for each channel r_min, r_max = np.percentile(r, [17, 90]) g_min, g_max = np.percentile(g, [25, 75]) b_min, b_max = np.percentile(b, [50, 99]) # Stretching each channel independently stretched_r = mh.stretch(r, r_min, r_max) stretched_g = mh.stretch(g, g_min, g_max) stretched_b = mh.stretch(b, b_min, b_max) # Merging the stretched channels back into an RGB image stretched_image = np.concatenate((stretched_r, stretched_g, stretched_b), axis=2) imshow(stretched_image) show() Output After executing the above code, we get the following output −

Learn Mahotas – Increase Gamma Correction work project make money

Mahotas – Increase Gamma Correction Let us first learn about what is gamma correction before understanding about increase gamma correction. Gamma correction adjusts the brightness of images to match how our eyes perceive light. Our eyes don”t see light in a linear way, so without correction, images may appear too dark or bright. Gamma correction applies a mathematical transformation to the brightness values, making the image look more natural by adjusting its brightness levels. Now, increasing gamma correction refers to adjusting the gamma value to make the overall image brighter. When the gamma value is increased, the dark areas appear brighter and enhance the overall contrast of the image. Increasing Gamma Correction in Mahotas In Mahotas, increasing gamma correction refers to adjusting the gamma value when changing the brightness of the pixels. The gamma is a positive value, where − A gamma value less than 1 will brighten the image. A gamma value greater than 1 will darken the image. A gamma value of 1 represents no correction and indicates a linear relationship between the pixel values and luminance. Gamma correction in Mahotas involves applying a power−law transformation to the intensity values of the image. The power−law transformation is defined as follows − new_intensity = old_intensity^gamma Here, old_intensity − It is the original intensity value of a pixel new_intensity − It is the transformed intensity value after gamma correction. The gamma determines the degree of correction applied to the image. When gamma is increased in Mahotas, it means that the intensity values are raised to a higher power. This adjustment affects the overall brightness of the image. Mahotas does not provide a direct way to do gamma correction, however it can be achieved by using mahotas with numpy library. Example In the following example, we are darkening a grayscale image by decreasing the gamma value − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(”sea.bmp”) # Converting it to grayscale gray_image = mh.colors.rgb2gray(image) # Decreasing gamma value corrected_gamma = 1.5 # Updating the image to use the corrected gamma value gamma_correction = np.power(gray_image, corrected_gamma) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(gray_image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Displaying the gamma corrected image axes[1].imshow(gamma_correction, cmap=”gray”) axes[1].set_title(”Gamma Corrected Image”) axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output Following is the output of the above code − Using Interactive Gamma Correction Slider An interactive gamma correction slider is a GUI element that allows users to adjust the gamma value dynamically. Users can increase or decrease the gamma value by dragging the slider, providing real−time feedback on the display. We can increase gamma correction using interactive gamma correction slider in mahotas, by first determining the desired gamma value to increase the correction. Then, apply the power−law transformation to the image by raising the pixel values to the power of the inverse gamma value. Syntax Following is the basic syntax to create an interactive slider − from matplotlib.widgets import Slider Slider(slider_axis, name, min_value, max_value, valint) where, slider_axis − It is a list that defines the position and dimensions of the slider. name − It is the name of the slider. mini_value − It is the minimum value that the slider can go to. max_value − It is the maximum value that the slider can go to. valint − It is the starting value of the slider. Example Here, we are trying to increase gamma correction using interactive gamma correction slider − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt from matplotlib.widgets import Slider # Loading the image image = mh.imread(”tree.tiff”) # Converting it to grayscale image = mh.colors.rgb2gray(image) # Creating a figure and axes for the plot fig, axis = mtplt.subplots() # Displaying the original image axis.imshow(image, cmap=”gray”) axis.set_title(”Gamma Correction”) axis.set_axis_off() # Creating a slider for gamma adjustment slider_axis = mtplt.axes([0.2, 0.05, 0.6, 0.03]) gamma_slider = Slider(slider_axis, ”Gamma”, 0.1, 5.0, valinit=1.0) # Updating the gamma correction and plot on change of slider value def update_gamma(val): gamma = gamma_slider.val corrected_image = np.power(image, gamma) axis.imshow(corrected_image, cmap=”gray”) fig.canvas.draw_idle() gamma_slider.on_changed(update_gamma) # Showing the figure mtplt.show() Output Output of the above code is as follows. First we are trying to increase the gamma correction using the slider as shown below − Now, decreasing gamma correction using the slider − Using Batch Gamma correction The batch gamma correction applies multiple gamma values to a single image. This helps in comparing the original image side−by−side at different gamma values to see the impact of increasing gamma correction. In Mahotas, we can adjust the brightness of an image using batch gamma correction by first iterating over a list of predetermined gamma values. Then applying the power−law transformation on the input image with different gamma values. Example Now, we are trying to increase gamma value using batch gamma correction − import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt from matplotlib.widgets import Slider # Loading the image image = mh.imread(”sun.png”) # Converting it to grayscale image = mh.colors.rgb2gray(image) # Defining a list of gamma values gamma_values = [0.21, 0.82, 2, 5] # Creating subplots to display images for each gamma value fig, axes = mtplt.subplots(1, len(gamma_values) + 1) axes[0].imshow(image, cmap=”gray”) axes[0].set_title(”Original Image”) axes[0].set_axis_off() # Applying gamma correction for each gamma value for i, gamma in enumerate(gamma_values): corrected_image = np.power(image, gamma) axes[i + 1].imshow(corrected_image, cmap=”gray”) axes[i + 1].set_title(f”Gamma={gamma}”) axes[i + 1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show() Output Following is the output of the above code −