赞
踩
由于RGB模型不够直观,不符合人类视觉习惯,因此在进行颜色特征提取前,需要将照片从RGB颜色模型转换为更符合人类视觉的HSV模型。在提取颜色特征时,最常用的方法之一为颜色直方图法,但一张图片中出现的颜色一般特别多,导致直方图矢量的维数较高,因此需要对HSV空间进行量化色量化的具体编码如下:
构造二维矩阵L = 9H + 3S + V,这样L中元素的取值范围为0~71,获得L的72bin的直方图作为颜色特征
'''获得图像的颜色直方图''' def colorHist(self, imgHSV): ''' opencv hsv 范围: h(0,180) s(0,255) v(0,255) ''' height, width, _ = imgHSV.shape H = np.zeros((height, width), dtype=np.uint8) S = np.zeros((height, width), dtype=np.uint8) V = np.zeros((height, width), dtype=np.uint8) h = imgHSV[..., 0] s = imgHSV[..., 1] v = imgHSV[..., 2] h = 2*h H[(h > 315) | (h <= 20)] = 0 H[(h > 20) & (h <= 40)] = 1 H[(h > 40) & (h <= 75)] = 2 H[(h > 75) & (h <= 155)] = 3 H[(h > 155) & (h <= 190)] = 4 H[(h > 190) & (h <= 270)] = 5 H[(h > 270) & (h <= 295)] = 6 H[(h > 295) & (h <= 315)] = 7 ''' 255*0.2 = 51 255*0.7 = 178 ''' S[s <= 51] = 0 S[(s > 51) & (s <= 178)] = 1 S[s > 178] = 2 V[v <= 51] = 0 V[(v > 51) & (v <= 178)] = 1 V[v > 178] = 2 g = 9*H + 3*S + V hist = cv2.calcHist([g], [0], None, [72], [0, 71]) return hist
使用opencv中的compareHist函数计算直方图的相似度
def likelihood(self, img, pl, objecthist,h,w):
hist = self.colorHist(img, pl,h,w)
score = cv2.compareHist(hist,objecthist,cv2.HISTCMP_CORREL)
#print('score=',score)
return score
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。