Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import matplotlib.pyplot as plt
- # Load the image in grayscale
- image_path = "dark.tif"
- img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
- # Check if the image is loaded correctly
- if img is None:
- print("Error: Image not loaded. Please check the file path.")
- else:
- # Apply Histogram Equalization
- equalized_img = cv2.equalizeHist(img)
- # Compute histograms
- hist_original = cv2.calcHist([img], [0], None, [256], [0, 256])
- hist_equalized = cv2.calcHist([equalized_img], [0], None, [256], [0, 256])
- # Flatten the histograms for plotting
- hist_original = hist_original.flatten()
- hist_equalized = hist_equalized.flatten()
- # Display images and histograms
- plt.figure(figsize=(10, 5))
- # Original Image
- plt.subplot(2, 2, 1)
- plt.imshow(img, cmap='gray')
- plt.title("Original Image")
- plt.axis('off')
- # Histogram of Original Image
- plt.subplot(2, 2, 2)
- plt.plot(hist_original, color='black')
- plt.title("Histogram of Original Image")
- plt.xlim(0, 256) # Ensure x-axis is from 0 to 255
- # Equalized Image
- plt.subplot(2, 2, 3)
- plt.imshow(equalized_img, cmap='gray')
- plt.title("Equalized Image")
- plt.axis('off')
- # Histogram of Equalized Image
- plt.subplot(2, 2, 4)
- plt.plot(hist_equalized, color='black')
- plt.title("Histogram of Equalized Image")
- plt.xlim(0, 256) # Ensure x-axis is from 0 to 255
- plt.tight_layout()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement