#灰度图片均衡化 img = cv2.imread('yqk.jpg',0) img_info = img.shape height = img_info[0] width = img_info[1] count_g = np.zeros(256,np.float) #每个像素出现的次数 for i in range(0,height): for j in range(0,width): g = img[i,j] index_g = int(g) count_g[index_g] = count_g[index_g] + 1 #每个像素的概率 for i in range(0,255): count_g[i] = count_g[i] / (height * width) sum1 = float(0) #计算累加概率 for i in range(0,255): sum1+=count_g[i] count_g[i] = sum1 print(count_g) #计算一个映射表 map1 = np.zeros(256,np.uint16) for i in range(0,256): map1[i] = np.uint16(count_g[i]*255) #将像素根据映射表映射一个新的图像 for i in range(0,height): for j in range(0,width): pixle = img[i,j] img[i,j] = map1[pixle] cv2.imshow('dst',img) cv2.waitKey(0)
1.图像宽高
2.图像各像素值出现次数
3.各像素出现累加概率
4.映射表
5.映射到新的图像