import cv2
import numpy as np
img = cv2.imread(input("Enter the image file path: "))
row, col, _ = img.shape
def df(img):
values = np.bincount(img[:,:,0].flatten(), minlength=256)
return values
def cdf(img):
freq = df(img)
cdf_values = np.cumsum(freq)
return cdf_values
def h(img):
cdf_values = cdf(img)
h = ((cdf_values - 1) / ((row * col) - 1)) * 255
return h.astype(np.uint8)
new_image = h(img)
cv2.imwrite('a.png', new_image)
In this the changes include:
Using np.bincount() in the df() function to count the frequency of each pixel value in a flattened array.
Removing the unnecessary shape variable by directly unpacking the shape in row, col, _.
Adjusting the cv2.imwrite() function call to save the new image correctly.
Note: This code assumes the input image is a grayscale image (single channel). If it's a colored image, further modifications may be necessary.