Advertisement
El_GEMMY

hist eq

Dec 29th, 2022
932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4.  
  5. def histo_eq(img, img_name):
  6.     """Histogram equalization"""
  7.     # convert from RGB color-space to YCrCb
  8.  
  9.     plt.hist(img.flatten(), 256, [0, 256], color='r')
  10.     plt.xlim([0, 256])
  11.     plt.show()
  12.  
  13.  
  14.     ycrcb_img = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
  15.  
  16.     # equalize the histogram of the Y channel
  17.     ycrcb_img[:, :, 0] = cv2.equalizeHist(ycrcb_img[:, :, 0])
  18.  
  19.     # convert back to RGB color-space from YCrCb
  20.     equalized_img = cv2.cvtColor(ycrcb_img, cv2.COLOR_YCrCb2BGR)
  21.  
  22.     plt.hist(equalized_img.flatten(), 256, [0, 256], color='r')
  23.     plt.xlim([0, 256])
  24.     plt.show()
  25.  
  26.     output_path = f'EditedImages/histo_equalization_{img_name}'
  27.     cv2.imwrite(output_path, equalized_img)
  28.     return equalized_img, output_path
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement