Mahotas - Finding Image Mean



When we talk about finding the image mean, we are referring to the calculation of the average intensity value across all the pixels in an image.

Each pixel in a digital image is represented by a numerical value that corresponds to its intensity or color information.

The range of intensity values depends on the image's color depth, such as 8−bit (0−255) for grayscale images or 24−bit (0−255 for each color channel) for color images.

Finding the image mean involves summing up the intensity values of all the pixels in the image and dividing it by the total number of pixels.

This process provides a single value that represents the average intensity of the image. It can be interpreted as the overall brightness or intensity level of the image.

Finding Image Mean in Mahotas

We can find the image mean in Mahotas using the mahotas.mean() function. This function accepts an image array and returns its mean value.

As we know that Mahotas can find the mean of only one channel at a time, therefore we need to convert our colored image to a single channel to find the mean of that channel.

The mean function returns a scalar value representing the mean of all the pixels in the image.

Syntax

Following is the basic syntax of the mean function −

Image_name.mean()

Example

In the following example, we are finding the mean of an image and displaying the image with mean intensity −

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)
find_mean = image.mean()
print("Mean of the image is:", find_mean)
imshow(image,cmap='gray')
show()

Output

Mean of the image is: 134.99541438411237

The image displayed is as shown below −

Finding Image mean

Image Mean of each Channel Individually

We can also find the mean of each channel in an RGB image using Mahotas. Firstly, calculate the mean for the entire image, and then calculate the mean for each channel individually using array slicing.

The slice image[:, :, 0] corresponds to Channel 0 (Red), image[:, :, 1] corresponds to Channel 1 (Green), and image[:, :, 2] corresponds to Channel 2 (Blue). It calculates the mean for each channel using the mean() function and prints the results.

Example

In this example, we are trying to find the mean value of an image for individual channels −

import mahotas as mh
import numpy as np
image = mh.imread('sun.png')
# Calculating the mean of the entire image
print("Mean of the image: {0}".format(image.mean()))
# Calculating the mean of Channel 0 (Red)
img0 = image[:, :, 0]
print('Mean of channel 0: {0}'.format(img0.mean()))
# Calculating the mean of Channel 1 (Green)
img1 = image[:, :, 1]
print('Mean of channel 1: {0}'.format(img1.mean()))
# Calculating the mean of Channel 2 (Blue)
img2 = image[:, :, 2]
print('Mean of channel 2: {0}'.format(img2.mean()))

Output

After executing the above code, we get the output as shown below −

Mean of the image: 105.32921300415184
Mean of channel 0: 126.04734671559905
Mean of channel 1: 106.04269535883749
Mean of channel 2: 83.89759693801898

Finding the Mean of an ROI in an Image

We can find the mean of a Region of Interest (ROI) within the image using the slice operations on the image array. After that, the mean value of all channels (if the image is in color) or the mean value of the grayscale values (if the image is in grayscale) within the ROI is calculated.

Following is the syntax to define an ROI of an image −

image[start_row:end_row, start_column:end_column]

Where, 'start_row' and 'end_row' represent the range of rows, and 'start_column' and 'end_column' represent the range of columns that define the ROI.

Hence, to specify the region of interest within the image, we select a subset of rows and columns.

Example

Here, we are fining the mean of a region of interet of an image −

import mahotas as mh
import numpy as np
image = mh.imread('tree.tiff')
# Defining a specific region of interest
roi = image[100:300, 200:400]
roi_mean = np.mean(roi)
print("Mean of the image is:", roi_mean)

Output

Output of the above code is as follows −

Mean of the image is: 98.556925
Advertisements