计算机视觉:彩色图片的直方图和图片的均衡化
Posted il_持之以恒_li
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算机视觉:彩色图片的直方图和图片的均衡化相关的知识,希望对你有一定的参考价值。
1. 彩色图片的直方图
import cv2
import numpy as np
def ImageHist(image,type):
color = (255,255,255)
windowName = 'gray'
if type == 31:
color = (255,0,0)
windowName = 'blue'
elif type ==32:
color = (0,255,0)
windowName = 'green'
elif type == 33:
color = (0,0,255)
windowName = 'red'
hist = cv2.calcHist([image],[0],None,[256],[0.0,255.0])
# 像素的范围
# 大小
minV,maxV,minL,maxL = cv2.minMaxLoc(hist)
histImg = np.zeros([256,256,3],np.uint8)
for h in range(256):
intenNormal = int(hist[h]*256/maxV)
cv2.line(histImg,(h,256),(h,256-intenNormal),color)
cv2.imshow(windowName,histImg)
return histImg
img = cv2.imread(filename='../anqila.jpg',flags=1)
channels = cv2.split(img)
for i in range(0,3):
ImageHist(channels[i],31+i)
cv2.waitKey(0)
2. 图片的均衡化
2.1 灰度图片的均衡化
import cv2
# 灰度 直方图均衡化
img = cv2.imread(filename='anqila21.jpg',flags=1)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('img',gray)
dst = cv2.equalizeHist(gray)
cv2.imshow('dst',dst)
cv2.waitKey(0)
2.2 彩色图片的均衡化
import cv2
# 彩色图片的均衡化
img = cv2.imread(filename='anqila21.jpg',flags=1)
cv2.imshow('img',img)
(b,g,r) = cv2.split(img) # 通道分解
bH = cv2.equalizeHist(b)
gH = cv2.equalizeHist(g)
rH = cv2.equalizeHist(r)
result = cv2.merge((bH,gH,rH))
cv2.imshow('result',result)
cv2.waitKey(0)
2.3 YUV 直方图均衡化
import cv2
# yuv 直方图均衡化
img = cv2.imread(filename='anqila21.jpg',flags=1)
cv2.imshow('img',img)
imgYUV = cv2.cvtColor(img,cv2.COLOR_BGR2YCrCb)
channelYUV = cv2.split(imgYUV)
channelYUV[0] = cv2.equalizeHist(channelYUV[0])
channels = cv2.merge(channelYUV)
result = cv2.cvtColor(channels,cv2.COLOR_YCrCb2BGR)
cv2.imshow('result',result)
cv2.waitKey(0)
以上是关于计算机视觉:彩色图片的直方图和图片的均衡化的主要内容,如果未能解决你的问题,请参考以下文章
视觉项目day28.21号实验记录(手机固定高度15cm拍摄+直方图均衡化+模板匹配,模板12个,测试28个,效果十分差)