学习产出:
import cv2
import numpy as np
import matplotlib. pyplot as plt
def origin_histogram ( image_matrix) :
histogram = { }
for i in range ( image_matrix. shape[ 0 ] ) :
for j in range ( image_matrix. shape[ 1 ] ) :
k = image_matrix[ i] [ j]
if k in histogram:
histogram[ k] += 1
else :
histogram[ k] = 1
sorted_list = sorted ( histogram)
sorted_histogram = { }
for j in range ( len ( sorted_list) ) :
sorted_histogram[ sorted_list[ j] ] = histogram[ sorted_list[ j] ]
return sorted_histogram
def equalization_histogram ( histogram, image) :
pr = { }
for i in histogram. keys( ) :
pr[ i] = histogram[ i] / ( image. shape[ 0 ] * image. shape[ 1 ] )
tmp = 0
for m in pr. keys( ) :
tmp += pr[ m]
pr[ m] = max ( histogram) * tmp
new_img = np. zeros( ( image. shape[ 0 ] , image. shape[ 1 ] ) , dtype = np. uint8 )
for k in range ( image. shape[ 0 ] ) :
for l in range ( image. shape[ 1 ] ) :
new_img[ k] [ l] = pr[ img[ k] [ l] ]
return new_img
def GrayHist ( image) :
row, column = image. shape[ 0 ] , image. shape[ 1 ]
grayHist = np. zeros( [ 256 ] , np. uint64)
for i in range ( row) :
for j in range ( column) :
grayHist[ image[ i] [ j] ] += 1
return grayHist
img = cv2. imread( 'cell.jpg' , cv2. IMREAD_GRAYSCALE)
origin_histogram = origin_histogram( img)