ColourMoments.py
import numpy as np
import cv2
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_featurecolorFeature.py
import numpy as np
import cv2
class ColorDescriptor:
def __init__(self, bins):
# 存储 3D 直方图的数量
self.bins = bins
def describe(self, image):
# 将图像转换为 HSV 色彩空间并初始化
# 用于量化图像的特征
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
features = []
# 获取尺寸并计算图像的中心
(h, w) = image.shape[:2]
(cX, cY) = (int(w * 0.5), int(h * 0.5))
# 将图像分成四份 rectangles/segments (top-left,top-right, bottom-right, bottom-left)
segments = [(0, cX, 0, cY), (cX, w, 0, cY), (cX, w, cY, h), (0, cX, cY, h)]
# 构建代表图像中心的椭圆蒙版
(axesX, axesY) = (int(w * 0.75) // 2, int(h * 0.75) // 2)
ellipMask = np.zeros(image.shape[:2], dtype="uint8")
cv2.ellipse(ellipMask, (cX, cY), (axesX, axesY), 0, 0, 360, 255, -1)
# loop over the segments
for (startX, endX, startY, endY) in segments:
# 为图像的每个角构建一个掩码,从中减去椭圆中心
cornerMask = np.zeros(image.shape[:2], dtype="uint8")
cv2.rectangle(cornerMask, (startX, startY), (endX, endY), 255, -1)
cv2.ellipse(ellipMask, (cX, cY), (axesX, axesY), 0, 0, 360, 255, -1)
# 从图像中提取颜色直方图,然后更新特征向量
hist = self.histogram(image, cornerMask)
features.extend(hist)
# 从椭圆区域提取颜色直方图并更新特征向量
hist = self.histogram(image, ellipMask)
features.extend(hist)
# 返回特征向量
return features
def histogram(self, image, mask):
# 使用提供的每个通道的 bin 数量,从图像的遮罩区域中提取 3D 颜色直方图
hist = cv2.calcHist([image], [0, 1, 2], mask, self.bins, [0, 180, 0, 256, 0, 256])
hist = cv2.normalize(hist, hist).flatten()
# 返回直方图
return hist#批量求一个文件夹下所有图片的各阶颜色矩:
from PIL import Image
import numpy as np
import os
"""
#r通道的一阶颜色矩
rd_1 = rd.mean()
#r通道的二阶颜色矩
rd_2 = rd.std()
"""
#定义一个求三阶颜色矩的函数
def var(x=None):
mid = np.mean(((x - x.mean()) ** 3))
return np.sign(mid) * abs(mid) ** (1/3)
#批量求一个文件夹下所有图片的各阶颜色矩:
def getimagedata(path):
filename = os.listdir(path)
n = len(filename)
data = np.zeros([n,9])
for i in range(n):
img = Image.open(path +'\\'+ filename[i])
M,N = img.size
r,g,b = img.split()
rd = np.asarray(r)
gd = np.asarray(g)
bd = np.asarray(b)
data[i,0] = rd.mean()
data[i,1] = gd.mean()
data[i,2] = bd.mean()
data[i,3] = rd.std()
data[i,4] = gd.std()
data[i,5] = bd.std()
data[i,6] = var(rd)
data[i,7] = var(gd)
data[i,8] = var(bd)
return data
if __name__=='__main__':
a = getimagedata("C:\\Users\\Admin\\Desktop\\666\\Project\\segmention\\train")
4706

被折叠的 条评论
为什么被折叠?



