# 本质: 统计每个像素灰度 出现的概率 0-255 p# 累计概率# 1 0.2 0.2 第一个灰度等级它出现的概率是0.2# 2 0.3 0.5 第二个灰度等级它出现的概率是0.3# 3 0.1 0.6 第三个灰度等级它出现的概率是0.1# 256# 100 0.5 255*0.5 = new import cv2import numpy as npimport matplotlib.pyplot as pltimg = cv2.imread('image0.jpg',1)cv2.imshow('src',img)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]#gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#cv2.imshow('src',gray)count_b = np.zeros(256,np.float)count_g = np.zeros(256,np.float)count_r = 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 (b,g,r) = img[i,j] index_b = int(b) index_g = int(g) index_r = int(r) count_b[index_b] = count_b[index_b]+1 count_g[index_g] = count_g[index_g]+1 count_r[index_r] = count_r[index_r]+1for i in range(0,255): count_b[i] = count_b[i]/(height*width) count_g[i] = count_g[i]/(height*width) count_r[i] = count_r[i]/(height*width)# 计算累计概率sum_b = float(0)sum_g = float(0)sum_r = float(0)for i in range(0,256): sum_b = sum_b+count_b[i] sum_g = sum_g+count_g[i] sum_r = sum_r+count_r[i] count_b[i] = sum_b count_g[i] = sum_g count_r[i] = sum_r#print(count)# 计算映射表map_b = np.zeros(256,np.uint16)map_g = np.zeros(256,np.uint16)map_r = np.zeros(256,np.uint16)for i in range(0,256): map_b[i] = np.uint16(count_b[i]*255) map_g[i] = np.uint16(count_g[i]*255) map_r[i] = np.uint16(count_r[i]*255)# 映射dst = np.zeros((height,width,3),np.uint8)for i in range(0,height): for j in range(0,width): #pixel = gray[i,j]# 获取当前的像素值 #gray[i,j] = map1[pixel] (b,g,r) = img[i,j] b = map_b[b] g = map_g[g] r = map_r[r] dst[i,j] = (b,g,r)#cv2.imshow('dst',gray)cv2.imshow('dst',dst)cv2.waitKey(0)
# 本质: 统计每个像素灰度 出现的概率 0-255 p# 累计概率# 1 0.2 0.2 第一个灰度等级它出现的概率是0.2# 2 0.3 0.5 第二个灰度等级它出现的概率是0.3# 3 0.1 0.6 第三个灰度等级它出现的概率是0.1# 256# 100 0.5 255*0.5 = new import cv2import numpy as npimport matplotlib.pyplot as pltimg = cv2.imread('image0.jpg',1)cv2.imshow('src',img)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]#gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#cv2.imshow('src',gray)count_b = np.zeros(256,np.float)count_g = np.zeros(256,np.float)count_r = 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 (b,g,r) = img[i,j] index_b = int(b) index_g = int(g) index_r = int(r) count_b[index_b] = count_b[index_b]+1 count_g[index_g] = count_g[index_g]+1 count_r[index_r] = count_r[index_r]+1for i in range(0,256): count_b[i] = count_b[i]/(height*width) count_g[i] = count_g[i]/(height*width) count_r[i] = count_r[i]/(height*width)# 计算累计概率sum_b = float(0)sum_g = float(0)sum_r = float(0)for i in range(0,256): sum_b = sum_b+count_b[i] sum_g = sum_g+count_g[i] sum_r = sum_r+count_r[i] count_b[i] = sum_b count_g[i] = sum_g count_r[i] = sum_r#print(count)# 计算映射表map_b = np.zeros(256,np.uint16)map_g = np.zeros(256,np.uint16)map_r = np.zeros(256,np.uint16)for i in range(0,256): map_b[i] = np.uint16(count_b[i]*255) map_g[i] = np.uint16(count_g[i]*255) map_r[i] = np.uint16(count_r[i]*255)# 映射dst = np.zeros((height,width,3),np.uint8)for i in range(0,height): for j in range(0,width): #pixel = gray[i,j]# 获取当前的像素值 #gray[i,j] = map1[pixel] (b,g,r) = img[i,j] b = map_b[b] g = map_g[g] r = map_r[r] dst[i,j] = (b,g,r)#cv2.imshow('dst',gray)cv2.imshow('dst',dst)cv2.waitKey(0)
# 本质: 统计每个像素灰度 出现的概率 0-255 p# 累计概率# 1 0.2 0.2 第一个灰度等级它出现的概率是0.2# 2 0.3 0.5 第二个灰度等级它出现的概率是0.3# 3 0.1 0.6 第三个灰度等级它出现的概率是0.1# 256# 100 0.5 255*0.5 = new import cv2import numpy as npimport matplotlib.pyplot as pltimg = cv2.imread('image2.jpg',1)cv2.imshow('src',img)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]#gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#cv2.imshow('src',gray)count_b = np.zeros(256,np.float)count_g = np.zeros(256,np.float)count_r = 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 (b,g,r) = img[i,j] index_b = int(b) index_g = int(g) index_r = int(r) count_b[index_b] = count_b[index_b]+1 count_g[index_g] = count_g[index_g]+1 count_r[index_r] = count_r[index_r]+1for i in range(0,256): count_b[i] = count_b[i]/(height*width) count_g[i] = count_g[i]/(height*width) count_r[i] = count_r[i]/(height*width)# 计算累计概率sum_b = float(0)sum_g = float(0)sum_r = float(0)for i in range(0,256): sum_b = sum_b+count_b[i] sum_g = sum_g+count_g[i] sum_r = sum_r+count_r[i] count_b[i] = sum_b count_g[i] = sum_g count_r[i] = sum_r#print(count)# 计算映射表map_b = np.zeros(256,np.uint16)map_g = np.zeros(256,np.uint16)map_r = np.zeros(256,np.uint16)for i in range(0,256): map_b[i] = np.uint16(count_b[i]*255) map_g[i] = np.uint16(count_g[i]*255) map_r[i] = np.uint16(count_r[i]*255)# 映射dst = np.zeros((height,width,3),np.uint8)for i in range(0,height): for j in range(0,width): #pixel = gray[i,j]# 获取当前的像素值 #gray[i,j] = map1[pixel] (b,g,r) = img[i,j] b = map_b[b] g = map_g[g] r = map_r[r] dst[i,j] = (b,g,r)#cv2.imshow('dst',gray)cv2.imshow('dst',dst)cv2.waitKey(0)