文章目录
一、图像像素值直方图
1.1 图像
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
def plot_demo(image):
'''统计每个像素值在图片中出现的次数'''
plt.hist(image.ravel(),256,[0,256])#参数:bin是256,范围【0,256】
plt.show()
src=cv.imread(r'D:\Project\Opencv\Learning01\angelababy.jpg')
cv.imshow("origin image",src)
plot_demo(src)
cv.waitKey(0)
1.2 图像通道cv.calcHist
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
'''绘制每个通道的像素值的直方图:既每个通道下,每个像素出现的频次'''
def image_hist_demo(image):
''''''
color=('blue','green','red')
for i ,color in enumerate(color):
hist=cv.calcHist(image,[i],None,[256],[0,255])#图像,通道,mask,bins(直方图size)、range[0,256]
plt.plot(hist,color=color)
plt.xlim([0,256])
plt.show()
src=cv.imread(r'D:\Project\Opencv\Learning01\angelababy.jpg')
cv.imshow("origin image",src)
image_hist_demo(src)
cv.waitKey(0)
二、直方图应用——均衡化
直方图是图像的统计学特征。直方图举例:像素值直方图,边缘、方向,梯度直方图等等。
直方图有很多应用:均衡化(增强对比度)、有颜色物体跟踪…
图像直方图均值化都是基于灰度图。直方图均值化自动调整图像对比度,使图像清晰,是图像增强的一个手段
2.1 全局均衡化
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
def equalHist_demo(image):
''''''
gray=cv.cvtColor(image,cv.COLOR_BGR2GRAY)#图像转灰度图
dst=cv.equalizeHist(gray)
cv.imshow('equalHist',dst)
src=cv.imread(r'D:\Project\Opencv\Learning01\angelababy.jpg')
cv.imshow("origin image",src)
equalHist_demo(src)
cv.waitKey(0)
2.2 自适应(局部)均值化
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
"""全局均衡化"""
def equalHist_demo(image):
''''''
gray=cv.cvtColor(image,cv.COLOR_BGR2GRAY)#图像转灰度图
dst=cv.equalizeHist(gray)
cv.imshow('equalHist',dst)
"""自适应(局部)均衡化"""
def clahe_demo(image):
''''''
gray=cv.cvtColor(image,cv.COLOR_BGR2GRAY)#图像转灰度图
clahe=cv.createCLAHE(clipLimit=2.0,tileGridSize=(8,8))
dst=clahe.apply(gray)
cv.imshow('clahe lHist',dst)
src=cv.imread(r'D:\Project\Opencv\Learning01\angelababy.jpg')
cv.imshow("origin image",src)
equalHist_demo(src)
clahe_demo(src)
cv.waitKey(0)
(左)原图、(中)全局均衡化、(右)自适应或局部均衡化
三、直方图反向投影
在HSV色彩空间进行反向投影。直方图反向投影是基于2D直方图。
3.1 2D直方图
import cv2 as cv
from matplotlib import pyplot as plt
"""计算图像的2D直方图"""
def hist2d_demo(image):
hsv=cv.cvtColor(image,cv.COLOR_BGR2HSV)
hist=cv.calcHist([hsv],[0,1],None,[750,500],[0,750,0,500])#计算两个通道的直方图:图像、通道、mask、binsize、范围
####方法1
cv.imshow('hist 2d',hist)
####方法2
# plt.imshow(hist,interpolation='nearest')
# plt.title('hist2D hist')
# plt.show()
src=cv.imread(r'D:\Project\Opencv\Learning01\angelababy.jpg')
print(src.shape)
cv.imshow("origin image",src)
hist2d_demo(src)
cv.waitKey(0)
3.2 直方图反向投影
import cv2 as cv
from matplotlib import pyplot as plt
"""计算图像的2D直方图"""
def hist2d_demo(image):
hsv=cv.cvtColor(image,cv.COLOR_BGR2HSV)
hist=cv.calcHist([hsv],[0,1],None,[750,500],[0,750,0,500])#计算两个通道的直方图:图像、通道、mask、binsize、范围
####方法1
cv.imshow('hist 2d',hist)
####方法2
# plt.imshow(hist,interpolation='nearest')
# plt.title('hist2D hist')
# plt.show()
"""直方图反向投影"""
def back_projection_demo():
target=cv.imread(r'D:\Project\Opencv\Learning01\target.png')
roi=cv.imread(r'D:\Project\Opencv\Learning01\kuai.png')
targev_hsc=cv.cvtColor(target,cv.COLOR_BGR2HSV)
roi_hsv = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
# 计算2d直方图
roiHist=cv.calcHist([roi_hsv],[0,1],None,[32,32],[0,180,0,256])#[32,32]代表直方图bin的个数,数越大,代表像素越细分,更加碎片化
cv.normalize(roiHist,roiHist,0,255,cv.NORM_MINMAX)
dst=cv.calcBackProject([targev_hsc],[0,1],roiHist,[0,180,0,256],1)
cv.imshow('backprojection',dst)
cv.imshow('roi',roi)
src=cv.imread(r'D:\Project\Opencv\Learning01\target.png')
print(src.shape)
cv.imshow("origin image",src)
back_projection_demo()
cv.waitKey(0)