function [beta] = sc_approx_pooling(feaSet, B, pyramid, gamma, knn)
%================================================
%
% Usage:
% Compute the linear spatial pyramid feature using sparse coding.
%
% Inputss:
% feaSet -structure defining the feature set of an image
% .feaArr local feature array extracted from the
% image, column-wise
% .x x locations of each local feature, 2nd
% dimension of the matrix
% .y y locations of each local feature, 1st
% dimension of the matrix
% .width width of the image
% .height height of the image
% B -sparse dictionary, column-wise
% pyramid -defines structure of pyramid
% gamma -sparsity regularization parameter
% knn -k nearest neighbors selected for sparse coding
%
% Output:
% beta -multiscale max pooling feature
%
% Written by Jianchao Yang @ NEC Research Lab America (Cupertino)
% Mentor: Kai Yu
% July 2008
%
% Revised May. 2010
%===============================================
dSize = size(B, 2);
nSmp = size(feaSet.feaArr, 2);
img_width = feaSet.width;
img_height = feaSet.height;
idxBin = zeros(nSmp, 1);
sc_codes = zeros(dSize, nSmp);
% compute the local feature for each local feature
D = feaSet.feaArr'*B;
IDX = zeros(nSmp, knn);
for ii = 1:nSmp,
d = D(ii, :);
[dummy, idx] = sort(d, 'descend');
IDX(ii, :) = idx(1:knn);
end
for ii = 1:nSmp,
y = feaSet.feaArr(:, ii);
idx = IDX(ii, :);
BB = B(:, idx);
sc_codes(idx, ii) = feature_sign(BB, y, 2*gamma); %sc_codes:sparse coding coefficient(S) X=BS
end
sc_codes = abs(sc_codes);
% spatial levels
pLevels = length(pyramid);
% spatial bins on each level
pBins = pyramid.^2;
% total spatial bins
tBins = sum(pBins);
beta = zeros(dSize, tBins);
bId = 0;
for iter1 = 1:pLevels,
nBins = pBins(iter1); %[1 4 16]
wUnit = img_width / pyramid(iter1);
hUnit = img_height / pyramid(iter1);
% find to which spatial bin each local descriptor belongs
xBin = ceil(feaSet.x / wUnit); %feaSet.x 960*1 vector
yBin = ceil(feaSet.y / hUnit); %feaSet.y 960*1 vector
idxBin = (yBin - 1)*pyramid(iter1) + xBin;
for iter2 = 1:nBins,
bId = bId + 1;
sidxBin = find(idxBin == iter2)
if isempty(sidxBin),
continue;
end
beta(:, bId) = max(sc_codes(:, sidxBin), [], 2); %max(s, [], 2):max for each row; beta:have 1+4+16 columns, zj=max(|sj1|,|sj2|..|sjm|) X=BS where X:n*m B:n*k S:k*m
end
end
if bId ~= tBins,
error('Index number error!');
end
beta
Linear Spatial Pyramid Matching Using Sparse Coding for Image Classification代码解
最新推荐文章于 2021-01-30 07:22:16 发布
本文详细解析了Linear Spatial Pyramid Matching (SPM) 算法在图像分类中的应用,重点关注其代码实现。文章指出在求解beta过程中,代码采用B-S形式而非论文中的U-V形式,并解释了细胞划分与矩阵维度的关系。此外,文章指出了论文与代码中关于z和U列数不一致的地方,代码中z的列数是基于3个level的cell总数计算得出。
摘要由CSDN通过智能技术生成