PCANet_output.m
<span style="font-family:Times New Roman;">function [OutImg OutImgIdx] = PCA_output(InImg, InImgIdx, PatchSize, NumFilters, V)
% Computing PCA filter outputs 减去均值样本矩阵 * 特征向量就是PCA的输出
% ======== INPUT ============
% InImg Input images (cell structure); each cell can be either a matrix (Gray) or a 3D tensor (RGB)
% InImgIdx Image index for InImg (column vector) 对应输入是ones(sum(OutImgIndex),1)
% PatchSize Patch size (or filter size); the patch is set to be sqaure
% NumFilters Number of filters at the stage right before the output layer % 输出层之前的阶段的过滤器的数量
% V PCA filter banks (cell structure); V{i} for filter bank in the ith stage
% ======== OUTPUT ===========
% OutImg filter output (cell structure)
% OutImgIdx Image index for OutImg (column vector)
% OutImgIND Indices of input patches that generate "OutImg"
% ===========================
addpath('./Utils')
ImgZ = length(InImg);
mag = (PatchSize-1)/2; % 相对于InImg的size,卷积后的size行和列都增加了PatchSize-1,这里为了实现对输入样本的边缘补零。
OutImg = cell(NumFilters*ImgZ,1);
cnt = 0;
for i = 1:ImgZ
[ImgX, ImgY, NumChls] = size(InImg{i});% 分别返回InImg的行数、列数和通道数
img = zeros(ImgX+PatchSize-1,ImgY+PatchSize-1, NumChls);
% 这里对应下面的im2col_mean_removal函数的输出大小
img((mag+1):end-mag,(mag+1):end-mag,:) = InImg{i}; %在计算卷积映射之前需要实现对样本进行边缘补零操作,以保证映射结果与原图像的尺寸相同
im = im2col_mean_removal(img,[PatchSize PatchSize]); % collect all the patches of the ith image in a matrix, and perform patch mean removal
% 出入image为m*n, 经过im2col_mean_removal后的输出为行数:(k1*k2);列数:[(m-k1+1)*(n-k2+1)]
for j = 1:NumFilters %有NumFilters个滤波器就输出NumFilters个特征,这个循环输出每个特征
cnt = cnt + 1;
OutImg{cnt} = reshape(V(:,j)'*im,ImgX,ImgY); % convolution output
%注意V(:,j)'后面有转置符号即k1k2*1维转换成1*k1k2维。每一个OutImg{cnt}的大小为m*n与初始样本大小相同,而cnt总共有NumFilters个
% 这里每行代表一个Filter的输出,及每行代表一个特征。
end
InImg{i} = []; %%释放内存空间
end
OutImgIdx = kron(InImgIdx,ones(NumFilters,1)); %InImgIdx=ImgIdx = (1:NumImg)',表示有NumImg个样本图片,经过NumFilters个滤波器滤波后总共有NumFilters个对应的输出
% OutImgIdx大小为(NumFilters*NumImg)*1,是一个列向量,
% 经过第一层PCA后输出为(L1*NumImg)*1,经过第二层PCA后输出(L2*L1*NumImg)*1
%,[1...1(共L1*L2个),2...2,...,N,,,N]'</span>