颜色矩原理及Python实现

原理 

  颜色矩(color moments)是由Stricker 和Orengo所提出的一种非常简单而有效的颜色特征。这种方法的数学基础在于图像中任何的颜色分布均可以用它的矩来表示。此外,由于颜色分布信息主要集中在低阶矩中,因此仅采用颜色的一阶矩(mean)、二阶矩(variance)和三阶矩(skewness)就足以表达图像的颜色分布。与颜色直方图相比,该方法的另一个好处在于无需对特征进行向量化。因此,图像的颜色矩一共只需要9个分量(3个颜色分量,每个分量上3个低阶矩),与其他的颜色特征相比是非常简洁的。在实际应用中,为避免低次矩较弱的分辨能力,颜色矩常和其它特征结合使用,而且一般在使用其它特征前,起到过滤缩小范围(narrow down)的作用。

 

注:

图中, N 表示图片中的总的像素数,pij表示第i个颜色通道在第j个图像像素值,Ei表示第i个颜色通道上所有像素的均值,σi表示第i个颜色通道上所有像素的标准差,si表示第i个颜色通道上所有像素的斜度(skewness)的3次方根。

Python 实现

import cv2
import numpy as np

# Compute low order moments(1,2,3)
def color_moments(filename):
    img = cv2.imread(filename)
    if img is None:
        return
    # Convert BGR to HSV colorspace
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # Split the channels - h,s,v
    h, s, v = cv2.split(hsv)
    # Initialize the color feature
    color_feature = []
    # N = h.shape[0] * h.shape[1]
    # The first central moment - average 
    h_mean = np.mean(h)  # np.sum(h)/float(N)
    s_mean = np.mean(s)  # np.sum(s)/float(N)
    v_mean = np.mean(v)  # np.sum(v)/float(N)
    color_feature.extend([h_mean, s_mean, v_mean])
    # The second central moment - standard deviation
    h_std = np.std(h)  # np.sqrt(np.mean(abs(h - h.mean())**2))
    s_std = np.std(s)  # np.sqrt(np.mean(abs(s - s.mean())**2))
    v_std = np.std(v)  # np.sqrt(np.mean(abs(v - v.mean())**2))
    color_feature.extend([h_std, s_std, v_std])
    # The third central moment - the third root of the skewness
    h_skewness = np.mean(abs(h - h.mean())**3)
    s_skewness = np.mean(abs(s - s.mean())**3)
    v_skewness = np.mean(abs(v - v.mean())**3)
    h_thirdMoment = h_skewness**(1./3)
    s_thirdMoment = s_skewness**(1./3)
    v_thirdMoment = v_skewness**(1./3)
    color_feature.extend([h_thirdMoment, s_thirdMoment, v_thirdMoment])

    return color_feature

参考资料

1、颜色特征的提取(转) http://blog.sina.com.cn/s/blog_66f17a900100w1iy.html

2、颜色矩 http://www.xuebuyuan.com/2199860.html

3、M. Stricker and M. Orengo, Similarity of Color Images, in Proc. SPIE Storage and Retrieval for Image and Video Databases, 1995.

posted @ 2017-03-22 11:48  klchang  阅读(9797)  评论(0编辑  收藏  举报