顏色矩原理及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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM