iT邦幫忙

0

「 python 」不呼叫早就包好的 histogram equalization 函式,而是用公式推導

  • 分享至 

  • xImage
  •  
# -*- coding: utf-8 -*-
import numpy as np
import cv2

path = "photo.jpg"
img = cv2.imread(path,0)

#To display image before equalization
cv2.imshow('image',img)
cv2.waitKey(0)


a = np.zeros((256,),dtype=np.float16)
b = np.zeros((256,),dtype=np.float16)
height,width=img.shape
#finding histogram
for i in range(width):
    for j in range(height):
        g = img[j,i]
        a[g] = a[g]+1
tmp = 1.0/(height*width)
b = np.zeros((256,),dtype=np.float16)
for i in range(256):
    for j in range(i+1):
        b[i] += a[j] * tmp;
    b[i] = round(b[i] * 255);
# b now contains the equalized histogram
b=b.astype(np.uint8)
#Re-map values from equalized histogram into the image
for i in range(width):
    for j in range(height):
        g = img[j,i]
        img[j,i]= b[g]
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言