Skip to content

Instantly share code, notes, and snippets.

@gyk
Last active August 29, 2015 14:05
Show Gist options
  • Save gyk/5adf4b203902a6b1e34a to your computer and use it in GitHub Desktop.
Save gyk/5adf4b203902a6b1e34a to your computer and use it in GitHub Desktop.
Compute histogram of shape contexts (assuming that SC has already been calculated). Written for the stupid professor, who keeps failing at the FizzBuzz test. It may contain bugs, as most code written for the academic papers is really buggy.
% Generates test data
nPoints = 20;
nBins = 6;
nImages = 72;
shapeContexts = cell(nImages, 1);
for i = 1:nImages
sc = zeros(nPoints, nBins);
for j = 1:nPoints
sc(j, :) = histc(randi(nBins, [1, nPoints]), 1:nBins);
end
shapeContexts{i} = sc;
end
function [bow] = shapemeQuantize(shapeContexts, dCodebook, nSamplings)
% shapeContexts: nImages-by-1 cell array,
% each element of which is an nPoints-by-nBin array;
nImages = size(shapeContexts, 1);
if ~exist('nSamplings', 'var')
nSamplings = min(600, nImages);
end
[nPoints, nBins] = size(shapeContexts{1});
% Preparing
SCSampled = zeros(nSamplings * nPoints, nBins);
idx = sort(randsample(nImages, nSamplings))';
indices = (1:nPoints)';
for i = idx
SCSampled(indices, :) = shapeContexts{i};
indices = indices + nPoints;
end
% Clustering
[~, shapemes] = kmeans(SCSampled, dCodebook, 'EmptyAction', 'singleton');
knnObj = ExhaustiveSearcher(shapemes);
bow = zeros(nImages, dCodebook);
for i = 1:nImages
nearestInd = knnsearch(knnObj, shapeContexts{i});
bow(i, :) = histc(nearestInd', 1:dCodebook);
end
% Normalization
bow = bow / nPoints;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment