function [qdata, qtable] = quantize(data, Ns, r, epsilon, dist2); % quantizing data (in descriptor space) % see "Creating effivient codebooks for visual recognition" F. Jurie and B. Triggs % % I assume the data is a martix of (d)x(N) where d is the dimension of desc % % the parameters are: % Ns - number of samples per mean_shift iteration % r - cell radius % epsilon - epsilon accuracy of mean shift % A function pointer % dist - distance measure between two descriptors - __RETURNS dist^2___ % % output: % qdata - the quantized data % qtable - table of quantized values % % % % Copyright (c) Bagon Shai % Department of Computer Science and Applied Mathmatics % Wiezmann Institute of Science % http://www.wisdom.weizmann.ac.il/ % % Permission is hereby granted, free of charge, to any person obtaining a copy % of this software and associated documentation files (the "Software"), to deal % in the Software without restriction, subject to the following conditions: % % The above copyright notice and this permission notice shall be included in % all copies or substantial portions of the Software. % % The Software is provided "as is", without warranty of any kind. % % Sep. 2006 % % fix the mean shift kernel width to be able to distinguish between two % nieghbouring instances. h = (r/2.0125).^2; % see gauss document... % magic numbers NUM_OF_BYTES = 2; Q_SIZE = 2^(8*NUM_OF_BYTES); convert = @uint16; % match the convert function to NUM_OF_BYETS [d N] = size(data); % init: population is all the possible indices qdata = convert( zeros(N,1) ); % allocate NUM_OF_BYTES bytes for quant qtable = zeros(d,Q_SIZE); ii = convert( 1); % count into qtable r2 = r.^2; % dist function returns dist^2 disp(sprintf('i=%5d',ii)); population = 1:N; lp = length(population); % while there's any zero value in qdata while ( ( ii < Q_SIZE-1 ) & ( lp > 0 ) ) % sample N data instances from non-clasified points samples = randsample(population, min(Ns, lp) ); % find the mode using mean shift - note that inpt to mean sift is % (d)x(N) mode = mean_shift(data(:,samples), h, epsilon, dist2); % remove all matching points from data (only where qdata = 0, i.e. in population) matches = find( dist2(data(:,population), repmat(mode,1,lp)) <= r2 ); lm = length(matches); if ( lm == 0 ) disp('no matches'); continue end % insert this mode to qtable qtable(:,ii)=mode; qdata( population(matches) ) = ii; population(matches) = []; lp = lp - lm; ii = ii + 1; disp(sprintf('\b\b\b\b\b\b\b\bi=%5d',ii)); end % resize qtable qtable = qtable(:,1:ii-1);