在那片宽广的宇宙中,藏着浩瀚星河,而我在这无限的长河中边浪边学,这篇是opencv笔记系列中的「灰度直方图均衡化」。
先统计灰度图像各个像素出现的概率,再根据概率对灰度图像进行均衡化。
原图没变,还是她
读入图像,转换成灰度格式,统计灰度像素概率,然后均衡化
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('test1.jpg',1)
cv2.imshow('src',img)
cv2.waitKey(0)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray',gray)
cv2.waitKey(0)
count = np.zeros(256,np.float)
for i in range(0,height):
for j in range(0,width):
pixel = gray[i,j]
index = int(pixel)
count[index] = count[index] + 1
for i in range(0,255):
count[i] = count[i] / (height * width)
# 计算累计概率
sum1 = float(0)
for i in range(0,256):
sum1 = s