11、直方图反向投影
2019-01-04 本文已影响0人
BigBigGuy
根据样本的直方图,找到图像与样本相似的地方,即反向投影技术
反向投影多半在HSV色彩空间
2019_01_04_21:11:22.png2D直方图
原图def hist2d_function(image):
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
'''
channels:2d 即有两个通道
histSize:bin大小,Hue(色调):180;Saturation(饱和度):256
ranges:Hue(色调):0~180;Saturation(饱和度):0~256
'''
hist = cv.calcHist([hsv], channels=[0, 1], mask=None, histSize=[180, 256],ranges=[0, 180, 0, 256])
plt.imshow(hist,interpolation= 'nearest')
plt.title("2D histogram")
plt.show()
2D直方图
#bin值越大,每个像素细分得越厉害,导致反向处理产生碎片化, 可以改小一下bin值
hist = cv.calcHist([hsv], channels=[0, 1], mask=None, histSize=[36, 48],ranges=[0, 180, 0, 256])
减少bin的大小
反向投影
def back_projection(target, sample):
sample_hsv = cv.cvtColor(sample, cv.COLOR_BGR2HSV)
target_hsv = cv.cvtColor(target, cv.COLOR_BGR2HSV)
sample_hist = cv.calcHist([sample_hsv], channels=[0, 1], mask=None,
histSize=[180, 256], ranges=[0, 180, 0, 256])
cv.normalize(sample_hist, sample_hist, 0, 255, cv.NORM_MINMAX) #将sample的直方图归一化
dst = cv.calcBackProject([target_hsv], channels=[
0, 1], hist=sample_hist, ranges=[0, 180, 0, 256], scale=1) #反向投影
cv.imshow("backProjection", dst)
bin:180,256
bin:32,32
bin:25,25
bit_and = cv.bitwise_and(target,target,mask = dst)
cv.imshow("按位与",bit_and)
按位与