1、彩色直方图
def ImgHist(image,type): color = (255,255,255) windowName='gray' if type==31: color=(255,0,0) windowName='B' elif type==32: color=(0,255,0) windowName='G' elif type==33: color=(0,0,255) windowName='R' #[0]通道 hist=cv2.calcHist([image],[0],None,[256],[0.0,255.0]) minV,maxV,minL,maxL=cv2.minMaxLoc(hist) print('minV,maxV,minL,maxL',minV,maxV,minL,maxL) histImg=np.zeros([256,256,3],np.uint8) for i in range(256): intenNormal=int(hist[i]*256/maxV) print(hist[i],hist[i]*256/maxV) cv2.line(histImg,(i,256),(i,256-intenNormal),color) cv2.imshow(windowName,histImg) return histImg img=cv2.imread('b.png',1) channels=cv2.split(img)#RGB--->R G B for i in range(3): ImgHist(channels[i],31+i) cv2.waitKey(0)
结果:
2、灰度化
img = cv2.imread('b.png',1) gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#原图 dst=cv2.equalizeHist(gray)#均衡化 cv2.imshow('dst',dst) cv2.imshow('gray',gray) cv2.waitKey(0)
结果:;
3、彩色
分别将各个通道进行均衡化,然后组合
img=cv2.imread('b.png',1) b,g,r=cv2.split(img) bH=cv2.equalizeHist(b) gH=cv2.equalizeHist(g) rH=cv2.equalizeHist(r) dst=cv2.merge((bH,gH,rH)) cv2.imshow('dst',dst) cv2.waitKey(0)
结果:
4、YUV 亮度与色度分离
imgyuv=cv2.cvtColor(img,cv2.COLOR_BGR2YCrCb) channels=cv2.split(imgyuv) channels[0]=cv2.equalizeHist(channels[0]) channels[1]=cv2.equalizeHist(channels[1]) channels[2]=cv2.equalizeHist(channels[2]) dst=cv2.merge(channels)#融合通道 cv2.imshow('dst',dst) cv2.waitKey(0)
结果:
5、滤波(双边滤波,高斯滤波)
# 2 双边滤波器 cv2.imshow('src',img) dst=cv2.bilateralFilter(img,100,200,160) cv2.imshow('shangbian',dst) # cv2.waitKey(0) # 高斯中值滤波 dst=np.zeros(img.shape,np.uint8) height=img.shape[0] width=img.shape[1] for i in range(3,height-3): for j in range(3,width-3): sum_b=int(0) sum_g=int(0) sum_r=int(0) for m in range(-3,3): for n in range(-3,3): (b,g,r)=img[i+m,j+n] sum_b=sum_b+int(b) sum_g=sum_g+int(g) sum_r=sum_r+int(r) b=np.uint8(sum_b/36) g=np.uint8(sum_g/36) r=np.uint8(sum_r/36) dst[i,j]=(b,g,r) cv2.imshow('gaosi',dst) cv2.waitKey(0)
结果: