Before we dive into histogram processing, let's quickly discuss what a histogram is. In image processing, a histogram represents the frequency distribution of pixel intensity values in an image. This means that it shows how many pixels there are at each intensity level, ranging from 0 (black) to 255 (white) for grayscale images. Here’s a simple way to visualize it:
Keeping this in mind, you can better understand how manipulating histograms can lead to enhanced images.
Histogram equalization is a method aimed at improving the contrast of an image. When an image's histogram is concentrated in a narrow range of intensity levels, the image often appears washed out. Histogram equalization redistributes the intensity levels so that the histogram covers a wider range, thereby enhancing the contrast.
Here’s how you can perform histogram equalization using OpenCV:
import cv2 import numpy as np from matplotlib import pyplot as plt # Load an image in grayscale image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE) # Apply histogram equalization equalized_image = cv2.equalizeHist(image) # Plot the results plt.figure(figsize=(10, 5)) plt.subplot(1, 2, 1) plt.title('Original Image') plt.imshow(image, cmap='gray') plt.axis('off') plt.subplot(1, 2, 2) plt.title('Equalized Image') plt.imshow(equalized_image, cmap='gray') plt.axis('off') plt.show()
In this example, after reading an image in grayscale, we apply cv2.equalizeHist()
, which performs the histogram equalization. The result is typically a clearer image with improved contrast.
Histogram stretching (or linear contrast stretching) is another effective technique to improve image quality. Unlike histogram equalization—where we redistribute pixel intensities across the established histogram—histogram stretching functions by mapping the original pixel values to a wider range.
def stretch_histogram(image): # Finding the min and max pixel values min_pixel = np.min(image) max_pixel = np.max(image) # Apply histogram stretching formula stretched_image = (image - min_pixel) * (255 / (max_pixel - min_pixel)) return np.uint8(stretched_image) # Load an image in grayscale image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE) # Stretch the histogram stretched_image = stretch_histogram(image) # Plot the original and stretched images plt.figure(figsize=(10, 5)) plt.subplot(1, 2, 1) plt.title('Original Image') plt.imshow(image, cmap='gray') plt.axis('off') plt.subplot(1, 2, 2) plt.title('Stretched Image') plt.imshow(stretched_image, cmap='gray') plt.axis('off') plt.show()
In the code above, stretch_histogram()
takes an image and applies the histogram stretching technique to enhance the visibility of details. It maps the pixel intensity values so that the entire range (0-255) is used, resulting in a clearer image.
Both histogram equalization and stretching are practical techniques used extensively in applications like:
One important aspect of histogram processing is visualizing the original and processed histograms. This helps to understand how our transformations affect the image quality.
# Plot histogram def plot_histogram(image): histogram, bins = np.histogram(image.flatten(), 256, [0, 256]) plt.figure() plt.title('Histogram') plt.xlabel('Pixel Intensity') plt.ylabel('Frequency') plt.xlim([0, 256]) plt.plot(histogram) plt.show() # Load an image in grayscale image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE) # Plot the histogram of the original image plot_histogram(image) # Plot histogram of the equalized image plot_histogram(equalized_image)
By plotting the histograms, we can see how our image processing techniques have redistributed the pixel intensity values.
With these techniques at your disposal, you can significantly enhance the quality of images using histogram processing with Python and OpenCV. Whether you are dealing with everyday photos or specialized medical imagery, having these tools in your arsenal will empower your computer vision projects.
08/11/2024 | Python
06/12/2024 | Python
06/10/2024 | Python
22/11/2024 | Python
14/11/2024 | Python
06/12/2024 | Python
21/09/2024 | Python
21/09/2024 | Python
22/11/2024 | Python
06/12/2024 | Python
08/11/2024 | Python
08/12/2024 | Python