Learn Mahotas – Mean Value of Image work project make money

Mahotas – Mean Value of Image



Mean value of an image refers to the average brightness of all the pixels of an image. Brightness is a property of an image that determines how light or dark an image appears to the human eye.

It is determined by the pixel intensity value; higher pixel intensity values represent brighter areas, while lower pixel intensity values represent darker areas.

The mean value of an image is widely used in image segmentation, which involves dividing an image into distinct regions.

It can also be used in image thresholding which refers to converting an image into binary image consisting of foreground and background pixels.

Mean Value of Image in Mahotas

Mahotas does not have a built-in function to find the mean of value of an image. However, we can find the mean value of an image by using mahotas and numpy library together.

We can use the mean() function in the numpy library to find the mean pixel intensity value of an image.

The mean() function works by iteratively going over each pixel and summing its intensity value. Once all the pixels have been traversed, it divides the sum by the total number of pixels.

The mean pixel intensity value of an image can be calculated using the following formula −

Mean = Sum of all pixel values / Total number of pixels

For example, let”s assume that an image is composed of 2 pixels each with an intensity value of 5. Then the mean can be calculated as follows −

Mean = 10 / 2
Mean = 5

The numpy.mean() function

The numpy.mean() function takes an image as input and returns the average brightness of all its pixels as a decimal number. The mean function works on any type of input image such as RGB, grayscale or labeled.

Syntax

Following is the basic syntax of the mean() function in numpy −

numpy.mean(image)

Where,

  • image − It is the input image.

Example

In the following example, we are finding the average pixel intensity value of an image using the np.mean() function.

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread(''sun.png'')
# Finding the mean value
mean_value = np.mean(image)
# Printing the mean value
print(''Mean value of the image is ='', mean_value)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 1)
# Displaying the original image
axes.imshow(image)
axes.set_title(''Original Image'')
axes.set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
Output

Following is the output of the above code −

Mean value of the image is = 105.32921300415184

The image obtained is as shown below −

Mean Value Image

Mean Value of each Channel

We can also find the mean value of each channel of an RGB image in Mahotas. RGB images to refer to images having three−color channels − Red, Green, and Blue.

Each pixel in an RGB image has three intensity values, one for each color channel.

The channel value of red is 0, green is 1 and blue is 2. These values can be used to separate an RGB image into its individual color components.

In mahotas, to find the mean pixel intensity value of each channel of an RGB image, we first separate the RGB image into separate channels. This is achieved by specifying the channel value. Once the channels are separated, we can find their mean value individually.

Example

In the example mentioned below, we are finding the mean pixel intensity value of each channel of an RGB image.

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread(''nature.jpeg'')
# Getting the red channel.
red_channel = image[:, :, 0]
# Getting the green channel.
green_channel = image[:, :, 1]
# Getting the blue channel.
blue_channel = image[:, :, 2]
# Finding the mean value of each channel
mean_red = np.mean(red_channel)
mean_green = np.mean(green_channel)
mean_blue = np.mean(blue_channel)
# Printing the mean value of each channel
print(''Mean value of the Red channel is ='', mean_red)
print(''Mean value of the Green channel is ='', mean_green)
print(''Mean value of the Blue channel is ='', mean_blue)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 1)
# Displaying the original image
axes.imshow(image)
axes.set_title(''Original Image'')
axes.set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

Output

Output of the above code is as follows −

Mean value of the Red channel is = 135.4501688464837
Mean value of the Green channel is = 139.46532482847343
Mean value of the Blue channel is = 109.7802007397084

The image produced is as follows −

Mean Value Channel

Mean Value of Grayscale Image

We can find the mean value of a grayscale image as well. Grayscale images refer to the image having only a single−color channel.

Each pixel of a grayscale image is represented by a single intensity value.

The intensity value of a grayscale image can range from 0 (black) to 255 (white). Any value between 0 and 255 will produce a shade of gray. Lower values will produce darker shades while higher values will produce lighter shades.

In mahotas, we first convert an input RGB image to grayscale using the mh.colors.rgb2gray() function. Then, we find its mean pixel intensity value using the mean() function.

Example

In this example, we are finding the mean pixel intensity value of a grayscale image.

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread(''tree.tiff'')
# Converting it to grayscale
grayscale_image = mh.colors.rgb2gray(image)
# Finding the mean value of the grayscale image
mean_value = np.mean(grayscale_image)
# Printing the mean value of the image
print(''Mean value of the grayscale image is ='', mean_value)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 1)
# Displaying the grayscale image
axes.imshow(grayscale_image, cmap=''gray'')
axes.set_title(''Grayscale Image'')
axes.set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

Output

After executing the above code, we get the following output −

Mean value of the grayscale image is = 113.21928107579335

Following is the image obtained −

Mean Value Channel1

Leave a Reply

Your email address will not be published. Required fields are marked *