% This code generates 128-bit hamming code "bs" for SIFT features %128-D % "bs": binary signature % % Bug fixed by Liang Zheng on 01/26/2015. %% load previous data feat_file = '100000.siftgeo'; % folder of holidays features %holidays特征 word_file = '100000_word.mat'; % holidays visual word %visual_word:3*1125 double P = importdata('projection_matrix_128bits.mat');% load projection matrix %映射矩阵 %P:128*128 double %mat文件怎么计算来的?? thres = importdata('thres_20k_128bits.mat');% load median matrix中位数矩阵 %thres:128*20000 double % a matrix for converting 01 strings into 0-255 uint8 data %转换01串-> 0~255 % 将二进制转为uint8的十进制 % uint8 data nbits = 128; %nbits:number of bits for SIFT feature m = 2.^(0:7); %m = [1 2 4 8 ... 128] % (0:7)是个向量 [0, 1, 2, 3, 4, 5, 6, 7] M = m; % [1 2 4 8 ... 128] for i = 2:nbits/8 %2~128/8 % 2~16 % 循环15次,最后得到的矩阵大小是 <16,(8*16)>即<16, 128> % 斜对角串联,每次增加一行,增加size(m, 2)=8列,每次斜对角方向增加一个m,其他部位填充0 M = blkdiag (M, m); %Block diagonal concatenation of matrix input arguments end bin2compactbin = M; % for each image, calculate the HE signatures of its features 计算特征的HE % signature [data, meta] = siftgeo_read(feat_file); % load features %feat_file = '100000.siftgeo'; data: n*128 data = data'; %转置后,data:128*n % rootSIFT sum_val = sum(data); %sum_val: 1*128 %1、求和 for i = 1:128 data(i, :) = data(i, :)./sum_val; %2、 ./ 点除 end data = single(sqrt(data)); %3、根方,并转为single类型 visual_word = importdata(word_file); % load visual word %word_file = '100000_word.mat'; %visual_word:3*1125 %对应descriptors的visual words nsift = size(visual_word, 2); %nsift = 1125, 1125个sift keypoints,size(visual_word, 1)是MA(kNN),size(visual_word, 2)是sift descriptors的个数 %% feature projection and binarization %特征映射与二值化 temp_data = P*data; % projected feature %data是RootSIFT特征 %% P = importdata('projection_matrix_128bits.mat'); kNN = 3; % number of visual words assigned to each SIFT feature %visual word就是index,赋给每个SIFT feature的visual words hamming_data = zeros(128, nsift*kNN);% note, multiple assignment is implied here. kNN = 3. %注意 MA = kNN = 3,即每个feature对应3个visual words, hamming_data:128*(nsift*kNN) if ~isempty(visual_word) %非空 for i = 1:kNN %1~3 vw_index = visual_word(i, :); %visual_word:3*1125, 取每一行i,vw_index: 1*1125 thresh = thres(:,vw_index); %thres = importdata('thres_20k_128bits.mat'); thres:128*20000,截取thres矩阵,新得到的thres:128*1125 IDX = i:3:((nsift-1)*3+i); %nsift是rootSFIT个数,1125, i:3:(1124*3+i) hamming_data(:, IDX) = temp_data - thresh; % projected data minus median matrix end hamming_data(hamming_data > 0) = 1; %hamming_data > 0的部分置为1,其余部分置为0 hamming_data(hamming_data <=0 ) = 0; % bs: binary signature bs = uint8 (bin2compactbin*hamming_data); % convert 01 strings into uint8 data %bin2compactbin = M; else %visual_word为空 bs = []; end % save file. bs is used as the binary signature of SIFT features write_file = '100000_he.txt'; fid = fopen(write_file, 'w'); %创建100000_he.txt文件 fwrite(fid, bs, 'uint8'); %将binary signature写入100000_he.txt fclose(fid);