Color Names 特征提取并進行PCA降維 (Learning Color Names for Real-World Applications)
之前的一篇部落格裡介紹了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函數
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函數添加部分代碼
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);
這裡是将圖像的CN特征降維成3維,并且顯示出來,結果如圖: