- Mahotas Tutorial
- Mahotas - Home
- Mahotas - Introduction
- Mahotas - Computer Vision
- Mahotas - History
- Mahotas - Features
- Mahotas - Installation
- Mahotas Handling Images
- Mahotas - Handling Images
- Mahotas - Loading an Image
- Mahotas - Loading Image as Grey
- Mahotas - Displaying an Image
- Mahotas - Displaying Shape of an Image
- Mahotas - Saving an Image
- Mahotas - Centre of Mass of an Image
- Mahotas - Convolution of Image
- Mahotas - Creating RGB Image
- Mahotas - Euler Number of an Image
- Mahotas - Fraction of Zeros in an Image
- Mahotas - Getting Image Moments
- Mahotas - Local Maxima in an Image
- Mahotas - Image Ellipse Axes
- Mahotas - Image Stretch RGB
- Mahotas Color-Space Conversion
- Mahotas - Color-Space Conversion
- Mahotas - RGB to Gray Conversion
- Mahotas - RGB to LAB Conversion
- Mahotas - RGB to Sepia
- Mahotas - RGB to XYZ Conversion
- Mahotas - XYZ to LAB Conversion
- Mahotas - XYZ to RGB Conversion
- Mahotas - Increase Gamma Correction
- Mahotas - Stretching Gamma Correction
- Mahotas Labeled Image Functions
- Mahotas - Labeled Image Functions
- Mahotas - Labeling Images
- Mahotas - Filtering Regions
- Mahotas - Border Pixels
- Mahotas - Morphological Operations
- Mahotas - Morphological Operators
- Mahotas - Finding Image Mean
- Mahotas - Cropping an Image
- Mahotas - Eccentricity of an Image
- Mahotas - Overlaying Image
- Mahotas - Roundness of Image
- Mahotas - Resizing an Image
- Mahotas - Histogram of Image
- Mahotas - Dilating an Image
- Mahotas - Eroding Image
- Mahotas - Watershed
- Mahotas - Opening Process on Image
- Mahotas - Closing Process on Image
- Mahotas - Closing Holes in an Image
- Mahotas - Conditional Dilating Image
- Mahotas - Conditional Eroding Image
- Mahotas - Conditional Watershed of Image
- Mahotas - Local Minima in Image
- Mahotas - Regional Maxima of Image
- Mahotas - Regional Minima of Image
- Mahotas - Advanced Concepts
- Mahotas - Image Thresholding
- Mahotas - Setting Threshold
- Mahotas - Soft Threshold
- Mahotas - Bernsen Local Thresholding
- Mahotas - Wavelet Transforms
- Making Image Wavelet Center
- Mahotas - Distance Transform
- Mahotas - Polygon Utilities
- Mahotas - Local Binary Patterns
- Threshold Adjacency Statistics
- Mahotas - Haralic Features
- Weight of Labeled Region
- Mahotas - Zernike Features
- Mahotas - Zernike Moments
- Mahotas - Rank Filter
- Mahotas - 2D Laplacian Filter
- Mahotas - Majority Filter
- Mahotas - Mean Filter
- Mahotas - Median Filter
- Mahotas - Otsu's Method
- Mahotas - Gaussian Filtering
- Mahotas - Hit & Miss Transform
- Mahotas - Labeled Max Array
- Mahotas - Mean Value of Image
- Mahotas - SURF Dense Points
- Mahotas - SURF Integral
- Mahotas - Haar Transform
- Highlighting Image Maxima
- Computing Linear Binary Patterns
- Getting Border of Labels
- Reversing Haar Transform
- Riddler-Calvard Method
- Sizes of Labelled Region
- Mahotas - Template Matching
- Speeded-Up Robust Features
- Removing Bordered Labelled
- Mahotas - Daubechies Wavelet
- Mahotas - Sobel Edge Detection
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 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 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 −
To Continue Learning Please Login
Login with Google