Advertisement
Guest User

Untitled

a guest
Feb 26th, 2025
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. # Load the image in grayscale
  6. image_path = "dark.tif"
  7. img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
  8.  
  9. # Check if the image is loaded correctly
  10. if img is None:
  11. print("Error: Image not loaded. Please check the file path.")
  12. else:
  13. # Apply Histogram Equalization
  14. equalized_img = cv2.equalizeHist(img)
  15.  
  16. # Compute histograms
  17. hist_original = cv2.calcHist([img], [0], None, [256], [0, 256])
  18. hist_equalized = cv2.calcHist([equalized_img], [0], None, [256], [0, 256])
  19.  
  20. # Flatten the histograms for plotting
  21. hist_original = hist_original.flatten()
  22. hist_equalized = hist_equalized.flatten()
  23.  
  24. # Display images and histograms
  25. plt.figure(figsize=(10, 5))
  26.  
  27. # Original Image
  28. plt.subplot(2, 2, 1)
  29. plt.imshow(img, cmap='gray')
  30. plt.title("Original Image")
  31. plt.axis('off')
  32.  
  33. # Histogram of Original Image
  34. plt.subplot(2, 2, 2)
  35. plt.plot(hist_original, color='black')
  36. plt.title("Histogram of Original Image")
  37. plt.xlim(0, 256) # Ensure x-axis is from 0 to 255
  38.  
  39. # Equalized Image
  40. plt.subplot(2, 2, 3)
  41. plt.imshow(equalized_img, cmap='gray')
  42. plt.title("Equalized Image")
  43. plt.axis('off')
  44.  
  45. # Histogram of Equalized Image
  46. plt.subplot(2, 2, 4)
  47. plt.plot(hist_equalized, color='black')
  48. plt.title("Histogram of Equalized Image")
  49. plt.xlim(0, 256) # Ensure x-axis is from 0 to 255
  50.  
  51. plt.tight_layout()
  52. plt.show()
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement