Histogram is an important quantity in image processing. This post looks at white and black images and their histograms.
Histogram is the distribution of intensities or pixel values. In other words, for a grayscale image, the histogram of the image is the distribution of intensities from 0(black) to 255(white). Each bin in the histogram represents a range of pixel values and the height of the bin indicates the number of pixels that fall within that range. Hence, a uniform gray colored image has just one value for the histogram and one with a gradient has a triangular distribution, and so on.
Python can be used to calculate the histogram of an image. Python uses the libraries related to images like PIL and OpenCV.
Calculating the histogram [calcHist()] using OpenCV:
At first, let us calculate the histogram of a grayscale image. I have a image all white and let us find its histogram.
using the PyCharm IDE,
histogram = cv2.calcHist([image], [0], None, [256], [0, 256])
• [image]: This is a list (or a single image) containing the image to be analyzed. In this case, we are passing a single grayscale image.
• [0]: This specifies that we want to calculate the histogram for the first channel of the image. For grayscale images, there is only one channel.
• None: This parameter is used for specifying a mask. In this case, we are not using a mask, so we pass None.
• [256]: This specifies the number of bins in the histogram. A value of 256 is commonly used for grayscale images, as it corresponds to the possible range of pixel values (0-255).
• [0, 256]: This specifies the range of pixel values for which the histogram is calculated. In this case, we are calculating the histogram for all pixel values in the image.
Note that histogram does not refer to the spatial distribution of the pixel values.
This is the python code for a image all white.
----------------------------------------------
import cv2
image = cv2.imread(r'C:\Users\hoden\PycharmProjects\exploreImage\Images_3\white.png')
histogram = cv2.calcHist([image], [0], None, [256], [0, 256])
print(histogram)
----------------------------------------
The result is a single array all zeros except the last one.
[[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
.
.
.
.
[636804.]]
The intensity value for white pixels is 255.
Every pixel in the image has this intensity.
Given these facts, the histogram will show:
A peak at the intensity level 255.
All other intensity levels (0-254) will have a frequency of 0.
from PIL import Image
def calculate_histogram(image):
"""Calculates the histogram of an image.
Args:
image: A PIL Image object.
Returns:
A list containing the pixel counts for each intensity level.
"""
histogram = image.histogram()
return histogram
# Load the image
image = Image.open("your_image.jpg")
# Calculate the histogram
histogram = calculate_histogram(image)
# Print the histogram
print(histogram)
-----------------------------------
PIL is a simpler implementation than that of OpenCV. Both the data structure and the visualization are different. We will look at visualization in future posts.
Histogram are very useful in image recognition related aspects:
- Image segmentation: Identifying regions of interest
within an image based on their pixel intensity distribution.
- Image enhancement: Adjusting the contrast or
brightness of an image based on the histogram.
- Image comparison: Comparing the histograms of
different images to determine their similarity or dissimilarity.
No comments:
Post a Comment