![Mahotas](/mahotas/images/mahotas-mini-logo.jpg)
- 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 - 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](/mahotas/images/finding_image_mean.jpg)
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