import cv2 import numpy as np import math import time def get_entropy(img_): x, y = img_.shape[0:2] img_ = cv2.resize(img_, (100, 100)) # 缩小的目的是加快计算速度 tmp = [] for i in range(256): tmp.append(0) val = 0 k = 0 res = 0 img = np.array(img_) for i in range(len(img)): for j in range(len(img[i])): val = img[i][j] tmp[val] = float(tmp[val] + 1) k = float(k + 1) for i in range(len(tmp)): tmp[i] = float(tmp[i] / k) for i in range(len(tmp)): if(tmp[i] == 0): res = res else: res = float(res - tmp[i] * (math.log(tmp[i]) / math.log(2.0))) return res for path_ in open('list.txt'): t1 = time.time() path = path_[:-1] image = cv2.imread(path,0) t2 = time.time() res = get_entropy(image)