📄 compilepyramid.m
字号:
function [ pyramid_all ] = CompilePyramid( imageFileList, dataBaseDir, textonSuffix, dictionarySize, pyramidLevels, canSkip )
%function [ pyramid_all ] = CompilePyramid( imageFileList, dataBaseDir, textonSuffix, dictionarySize, pyramidLevels, canSkip )
%
% Generate the pyramid from the texton lablels
%
% For each image the texton labels are loaded. Then the histograms are
% calculated for the finest level. The rest of the pyramid levels are
% generated by combining the histograms of the higher level.
%
% imageFileList: cell of file paths
% dataBaseDir: the base directory for the data files that are generated
% by the algorithm. If this dir is the same as imageBaseDir the files
% will be generated in the same location as the image file
% textonSuffix: this is the suffix appended to the image file name to
% denote the data file that contains the textons indices and coordinates.
% Its default value is '_texton_ind_%d.mat' where %d is the dictionary
% size.
% dictionarySize: size of descriptor dictionary (200 has been found to be
% a good size)
% pyramidLevels: number of levels of the pyramid to build
% canSkip: if true the calculation will be skipped if the appropriate data
% file is found in dataBaseDir. This is very useful if you just want to
% update some of the data or if you've added new images.
fprintf('Building Spatial Pyramid\n\n');
%% parameters
if(nargin<4)
dictionarySize = 200
end
if(nargin<5)
pyramidLevels = 4
end
if(nargin<6)
canSkip = 0
end
binsHigh = 2^(pyramidLevels-1);
pyramid_all = [];
for f = 1:size(imageFileList,1)
%% load image
imageFName = imageFileList{f};
[dirN base] = fileparts(imageFName);
baseFName = fullfile(dirN, base);
outFName = fullfile(dataBaseDir, sprintf('%s_pyramid_%d_%d.mat', baseFName, dictionarySize, pyramidLevels));
if(size(dir(outFName),1)~=0 && canSkip)
fprintf('Skipping %s\n', imageFName);
load(outFName, 'pyramid');
pyramid_all = [pyramid_all; pyramid];
continue;
end
%% load texton indices
in_fname = fullfile(dataBaseDir, sprintf('%s%s', baseFName, textonSuffix));
load(in_fname, 'texton_ind');
%% get width and height of input image
wid = texton_ind.wid;
hgt = texton_ind.hgt;
fprintf('Loaded %s: wid %d, hgt %d\n', ...
imageFName, wid, hgt);
%% compute histogram at the finest level
pyramid_cell = cell(pyramidLevels,1);
pyramid_cell{1} = zeros(binsHigh, binsHigh, dictionarySize);
for i=1:binsHigh
for j=1:binsHigh
% find the coordinates of the current bin
x_lo = floor(wid/binsHigh * (i-1));
x_hi = floor(wid/binsHigh * i);
y_lo = floor(hgt/binsHigh * (j-1));
y_hi = floor(hgt/binsHigh * j);
texton_patch = texton_ind.data( (texton_ind.x > x_lo) & (texton_ind.x <= x_hi) & ...
(texton_ind.y > y_lo) & (texton_ind.y <= y_hi));
% make histogram of features in bin
pyramid_cell{1}(i,j,:) = hist(texton_patch, 1:dictionarySize)./length(texton_ind.data);
end
end
%% compute histograms at the coarser levels
num_bins = binsHigh/2;
for l = 2:pyramidLevels
pyramid_cell{l} = zeros(num_bins, num_bins, dictionarySize);
for i=1:num_bins
for j=1:num_bins
pyramid_cell{l}(i,j,:) = ...
pyramid_cell{l-1}(2*i-1,2*j-1,:) + pyramid_cell{l-1}(2*i,2*j-1,:) + ...
pyramid_cell{l-1}(2*i-1,2*j,:) + pyramid_cell{l-1}(2*i,2*j,:);
end
end
num_bins = num_bins/2;
end
%% stack all the histograms with appropriate weights
pyramid = [];
for l = 1:pyramidLevels-1
pyramid = [pyramid pyramid_cell{l}(:)' .* 2^(-l)];
end
pyramid = [pyramid pyramid_cell{pyramidLevels}(:)' .* 2^(1-pyramidLevels)];
% save pyramid
save(outFName, 'pyramid');
pyramid_all = [pyramid_all; pyramid];
end % f
outFName = fullfile(dataBaseDir, sprintf('pyramids_all_%d_%d.mat', dictionarySize, pyramidLevels));
save(outFName, 'pyramid_all');
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -