之前的一篇博客里介绍了CN特征的提取,在实际应用中,很多算法为了提高计算效率,都会将CN特征进行降维,比如
Danelljan M, Shahbaz Khan F, Felsberg M, et al. Adaptive color attributes for real-time visual
tracking[C]//Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition. 2014: 1090-1097
即ColorTracking跟踪算法就采用了PCA降维,将CN特征降为了2维。
这里就在上一篇的基础上加上PCA降维的部分:
im2c.m
function out=im2c(im,w2c,color)
% input im should be DOUBLE !
% color=0 is color names out
% color=-1 is colored image with color names out
% color=1-11 is prob of colorname=color out;
% order of color names: black , blue , brown , grey , green , orange , pink , purple , red , white , yellow
color_values = { [0 0 0] , [0 0 1] , [.5 .4 .25] , [.5 .5 .5] , [0 1 0] , [1 .8 0] , [1 .5 1] , [1 0 1] , [1 0 0] , [1 1 1 ] , [ 1 1 0 ] };
if(nargin<3)
color=0;
end
RR=im(:,:,1);GG=im(:,:,2);BB=im(:,:,3);
index_im = 1+floor(RR(:)/8)+32*floor(GG(:)/8)+32*32*floor(BB(:)/8);
if(color==0)
[max1,w2cM]=max(w2c,[],2); %max第1维度为列,第2维度为行 max1为相应最大值,w2cM为索引
out=reshape(w2cM(index_im(:)),size(im,1),size(im,2));
end
if(color>0 && color < 12)
w2cM=w2c(:,color);
out=reshape(w2cM(index_im(:)),size(im,1),size(im,2));
end
if(color==-1)
out=im;
[max1,w2cM]=max(w2c,[],2);
out2=reshape(w2cM(index_im(:)),size(im,1),size(im,2));
for jj=1:size(im,1)
for ii=1:size(im,2)
out(jj,ii,:)=color_values{out2(jj,ii)}'*255;
end
end
end
% 添加的部分代码
% color=-2 return probabilities
if(color==-2)
out=reshape(w2c(index_im,:),size(im,1),size(im,2),size(w2c,2));
end
example_color_naming.m
% load the word to color names matrix. The words are a 32x32x32 grid on the sRGB space.
load('w2c.mat');
dim_final = 3; %降维后的维数
im=double(imread('car.jpg')); % load test image
sz = size(im);
dim_1 = sz(1)*sz(2);
% compute the color name assignment for all pixels in image im:
cn_feature=im2c(im,w2c,-2);
cn_reshaped = reshape(cn_feature, [dim_1, size(cn_feature, 3)]);
%下面是使用PCA提取主成分的步骤
% compute the mean appearance
data_mean = mean(cn_reshaped, 1);
% substract the mean from the appearance to get the data matrix
data_matrix = bsxfun(@minus, cn_reshaped, data_mean);
% calculate the covariance matrix 协方差矩阵
cov_matrix = 1/(dim_1 - 1) * (data_matrix' * data_matrix);
[pca_basis, pca_variances, ~] = svd(cov_matrix); %奇异值分解
% calculate the projection matrix as the first principal
% components and extract their corresponding variances 投影矩阵projection_matrix是第一主成成分
projection_matrix = pca_basis(:, 1:dim_final); %这里得到了新的投影矩阵
projection_variances = pca_variances(1:dim_final, 1:dim_final);
% projection matrix and variances
old_cov_matrix = projection_matrix * projection_variances * projection_matrix';
[num_pca_in, num_pca_out] = size(projection_matrix);
cn_pca = reshape(cn_reshaped * projection_matrix, [sz(1), sz(2), num_pca_out]);
imshow(cn_pca);