Advertisement
Pomah3

Untitled

Apr 13th, 2021
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. from skimage.io import imread, imsave, imshow
  2. from skimage import img_as_float, img_as_ubyte
  3. import numpy as np
  4.  
  5. img = imread("img.png")
  6.  
  7. counts = np.bincount(img.flat, minlength=256)
  8.  
  9. mn_cdf = -1
  10. cdf = [0] * 256
  11. for i in range(256):
  12.     if i != 0:
  13.         cdf[i] += cdf[i-1]
  14.        
  15.     cdf[i] += counts[i]
  16.     if mn_cdf == -1 and cdf[i] != 0:
  17.         mn_cdf = cdf[i]
  18.        
  19. def f(x):
  20.     return round((cdf[x] - mn_cdf) / (img.shape[0] * img.shape[1] - 1) * 255)
  21.  
  22. for i in range(img.shape[0]):
  23.     for j in range(img.shape[1]):
  24.         img[i, j] = f(img[i,j])
  25.  
  26. imsave("out_img.png", img)
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement