![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 - Fraction of Zeros in an Image
The fraction of zeros in an image refers to the proportion of zero−valued pixels compared to the total number of pixels in the image. In an image, each pixel typically represents a point in a grid, and the pixel values can range from 0 to a maximum value, depending on the image's color depth or intensity range.
A high fraction of zeros suggests that a significant portion of the image contains empty or background regions, while a low fraction of zeros indicates a denser distribution of nonzero pixel values, implying more detailed or complex content.
Fraction of Zeros in an Image in Mahotas
To obtain the fraction of zeroes in an image in Mahotas, we need to iterate through the entire image and divide the count of zero pixels by the total number of pixels in the image.
The total number of pixels is equal to the number of rows multiplied by the number of columns in the image.
Mahotas does not have direct functions for calculating fraction of zeros. However, you can use numpy and mahotas libraries to calculate it.
Example
In the following example, we are calculating the fraction of zero−valued pixels in the image by comparing the image array to zero, summing the True values, and dividing by the total number of pixels −
import mahotas as mh import numpy as np image = mh.imread('sun.png') # Calculating the fraction of zeros fraction_of_zeros = np.sum(image == 0) / np.prod(image.shape) print(f"Fraction of zeros: {fraction_of_zeros}")
Output
After executing the above code, we get the following output −
Fraction of zeros: 0.009496713127718466
Using the count_nonzero() Function
We can also calculate the fraction of zeros in an image using the count_nonzero() function in mahotas. The count_nonzero() function is used to count the number of non−zero elements in an array. It takes an array as input and returns the total count of elements that are non−zero.
Syntax
Following is the basic syntax of the count_nonzero() function in mahotas −
count_nonzero(arr, axis=None)
Where,
arr − It is the input array for which non−zero elements need to be counted.
axis (optional) − It is the axis or axes along which the non−zero elements are counted. If axis is not specified, all elements of the input array are considered.
Example
In here, we are counting the number of pixels of the image 'nature.jpeg' using the the np.count_nonzero() function −
import mahotas as mh import numpy as np image = mh.imread('nature.jpeg') # Counting the number of zero pixels zero_count = np.count_nonzero(image == 0) # Calculating the fraction of zeros total_pixels = image.size fraction_of_zeros = zero_count / total_pixels print("The fraction of zeros in the image is:", {fraction_of_zeros})
Output
Output of the above code is as follows −
The fraction of zeros in the image is: {0.010734258862206976}
Using Numpy
The NumPy library provides efficient data structures and functions for working with arrays and matrices. It is widely used for tasks such as mathematical operations, data manipulation, and scientific computations due to its high performance and extensive functionality.
We can also calculate the fraction of zeros using the numpy operation −
- Firstly, the NumPy array is compared to zero to convert the image to binary form.
- This comparison generates a boolean array where each element is True if the corresponding pixel value is greater than zero, and False otherwise.
The boolean array is then cast to the 'np.uint8' data type resulting in a binary image where white pixels are represented by ones and black pixels by zeros.
To calculate the fraction of zeros, the number of zero−valued elements in the binary image is computed. This count is divided by the total number of elements in the binary image to obtain the fraction.
Example
Here, we are first converting the image to a binary representation. We are then calculating the fraction of zeros of the binary image −
import mahotas as mh import numpy as np image = mh.imread('tree.tiff') # Convert the image to binary image_binary = (image > 0).astype(np.uint8) # Calculate the fraction of zeros fraction_of_zeros = np.sum(image_binary == 0) / np.prod(image_binary.shape) print("Fraction of zeros:", fraction_of_zeros)
Output
Output of the above code is as follows −
Fraction of zeros: 0.014683837192681532