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
Advertisements