颜色聚合向量(color coherence vector,CCV)是一种颜色特征,它包含了颜色分布的空间信息。克服了颜色直方图无法表达图像色彩的空间位置的缺点。
颜色聚合向量是一种更复杂颜色直方图。它将每个像素分类为聚合的(coherence pixels)或非聚合的(incoherence pixels)。聚合的像素指的是,该像素属于一个大的连通区域;而非聚合像素指的是,像素位于一个小的连通区域。连通区域大小的标准有我们自己来定,通常为整幅图像像素的1%。大于1%,是大的连通区域。
特征提取算法:
第一步:平滑滤波
用3*3的模板进行平滑滤波。
第二步:量化
把0-255的颜色区间量化为n个颜色区间,通常采用均匀量化。
第三步:划分连通区域(每一个连通区域只有一个灰度值)。
第四步:计算颜色聚合向量
每一个灰度级i(也就是直方图的每一个bin)有两部分组成(Ci and Ni);
Ci是聚合像素的个数(灰度值为i的所有大连通区域像素之和);
Ni是非聚合像素的个数(灰度值为i的所有小连通区域像素之和);
<Ci+Ni>是灰度级为i的像素之和。
该图像的颜色聚合向量可表示为:CCV=<(C1,N1)、(C2,N2)、(C3,N3)、.....(Cn,Nn)>
该图像的颜色直方图可表示为:HIST=<(C1+N1)、(C2+N2)、(C3+N3)、.....(Cn+Nn)>
可以这样理解,CCV有两个直方图组成:一个直方图统计聚合的像素。一个直方图统计非聚合的像素。
匹配函数(Matching function)
例如,比较图像IMG1,img2的相似度。
图像IMG1的颜色聚合向量:CCV1=<(C1,N1)、(C2,N2)、(C3,N3)、.....(Cn,Nn)>
图像img2的颜色聚合向量:ccv2=<(c1,n1)、(c2,n2)、(c3,n3)、.....(cn,nn)>
那么图像IMG1与图像img2的距离:
matlab code
%% 颜色聚合向量 主函数
clc;
clear all;
%% 初始化
bin=50; %量化级数
coherentPrec=1;%聚合像素阈值
%% 读取图像
Img1=imread('1.jpg');
Img2=imread('2.jpg');
%% 颜色聚合向量
CCV1=getCCV(Img1,coherentPrec,bin);
CCV2=getCCV(Img2,coherentPrec,bin);
%% 计算两幅图像的距离
D=0; %两幅图像的距离
for i=1:bin
C=abs(CCV1(1,i)-CCV2(1,i));
N=abs(CCV1(2,i)-CCV2(2,i));
d=C+N;
D=D+d;
end
函数getCCV()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%function CCV = getCCV(img,coherentPrec, numberOfColors)
%颜色聚合向量
%====================
%颜色聚合向量是一种基于颜色的图像检索
%Parallel implementation based on this paper : Comparing Images Using Color Coherence Vectors (1996) - http://goo.gl/LkWkbi -
%CCV = getCCV(img, coherentThreshold, numberOfPixels)
%getCCV function takes an image and return the Color Coherence Vector that describe this Image. You can compare images using this vector.
%
%Input:
%img : The Image (3-channel Image)
%
%Optional Input:
%coherentPrec: 设定一个阈值(一般取为图像总像素的1%)
%numberOfColors:量化,将0~255的区间量化为numberOfColors个颜色小区间 (default = 27 colors).
% Note it'll be changed a little bit to ensure the same different values for RGB channel
%
%Output :
%CCV: a (2*numberOfColors) matrix represents your image. This can be used for matching.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function CCV = getCCV(img,coherentPrec, numberOfColors)
if ~exist('coherentPrec','var')
coherentPrec = 1;
end
if ~exist('numberOfColors','var')
numberOfColors = 27;
end
CCV = zeros(2,numberOfColors);
%高斯滤波
Gaus = fspecial('gaussian',[5 5],2);
img = imfilter(img,Gaus,'same');
[img, updNumOfPix]= discretizeColors(img,numberOfColors); %量化
imgSize = (size(img,1)*size(img,2));
thresh = int32((coherentPrec/100) *imgSize);
parfor i=0:updNumOfPix-1
BW = img==i;
CC = bwconncomp(BW);
compsSize = cellfun(@numel,CC.PixelIdxList);
incoherent = sum(compsSize(compsSize>=thresh));
CCV(:,i+1) = [incoherent; ...
sum(compsSize) - incoherent];
end
end
量化函数discretizeColors(img,numColors)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function discretize the color to numColors and converts the RGB Image to one channel image
% The idea is to find equal number of unique colors in each channel so that the total conbination
% becomes ~numColors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [oneChannel, updatedNumC] = discretizeColors(img,numColors)
width = size(img,2);
height = size(img,1);
oneChannel = zeros(height,width);
% We have 3 channels. For each channel we have V unique values.
% So we want to find the value of V given that V x V x V ~= numColors
numOfBins = floor(pow2(log2(numColors)/3));
numOfBinsSQ = numOfBins*numOfBins;
img = floor((img/(256/numOfBins)));
for i=1:height
for j=1:width
oneChannel(i,j) = img(i,j,1)*numOfBinsSQ ...
+ img(i,j,2)*numOfBins + img(i,j,3);
end
end
updatedNumC = power(numOfBins,3);
end
参考:http://hubpages.com/technology/Image-Retrieval-Color-Coherence-Vector
matlab源码下载:http://www.mathworks.com/matlabcentral/fileexchange/47935-color-coherence-vector