繁体   English   中英

Python,计算HSV图像的直方图,忽略背景

[英]Python, calculate histogram of an HSV image, ignoring the background

我正在尝试使用以下代码计算带有 openCV 的 HSV 图像的直方图:

def istogrammaHSV(image,histSize):
    hsv_planes= cv2.split(image)
    histSize= histSize  
    histRange= (0,256)
    accumulate=False
    
    h_hist = np.array(cv2.calcHist(hsv_planes, [0], None, [histSize], histRange, accumulate=accumulate),dtype='uint8' )
    s_hist = np.array(cv2.calcHist(hsv_planes, [1], None, [histSize], histRange, accumulate=accumulate),dtype='uint8' )
    v_hist = np.array(cv2.calcHist(hsv_planes, [2], None, [histSize], histRange, accumulate=accumulate),dtype='uint8' )
    
    #normalization
    hist = np.append(h_hist, s_hist, axis=0)
    hist = np.append(hist, v_hist, axis=0)
    hist = hist / np.sqrt(np.sum(hist**2))
    
    return hist

问题是,由于图像是 png,当我将它们转换为 HSV 时,我有很多黑色像素(曾经是透明背景)。 但我不希望在直方图中计算黑色像素,我该怎么办?

样本 HSV 图像:

样本 HSV 图像

它看起来像 RGBA 格式的原始图像。 使用图像的 Alpha 通道作为遮罩。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM