Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
HobbySingh authored Nov 21, 2017
1 parent bf48348 commit 848c699
Show file tree
Hide file tree
Showing 21 changed files with 1,145 additions and 0 deletions.
120 changes: 120 additions & 0 deletions bag_of_hogs.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
function [image_features] = bag_of_hogs(vocabulary,type_of_dataset)


%Step size for SIFT detection
step_size=4;
vocab_size = size(vocabulary, 1);

if(type_of_dataset)
plane_source = 'D:\Mandeep\Summer\BTP\Tracking\Hog\tight_dataset\';
non_plane_source = 'D:\Mandeep\Summer\BTP\Tracking\Hog\Negatives2\';
fprintf('Will find features for training datasets\n');
total_images_positive = 1000;
total_images_negative = 1000;
random_numbers=randi([1 2000],1,total_images_negative);
% image_features = zeros(total_images, vocab_size);
else
plane_source = 'D:\Mandeep\Summer\BTP\Tracking\Hog\positives_test_dataset\';
non_plane_source = 'D:\Mandeep\Summer\BTP\Tracking\Hog\negatives_test_dataset\';
fprintf('Will find features for testing datasets \n');
total_images_positive = 5;
total_images_negative = 5;
random_numbers=randi([1 20],1,total_images_negative);
% image_features = zeros(total_images, vocab_size);
end
%generating histograms of 2000 bins , where value of bin tells most no. of
%matches with different cluster centres


%for plane dataset

counter = 1;
image_counter = 1;
fprintf('Computing Bag of words : Positives\n');
while(image_counter <= total_images_positive)
filename = strcat(plane_source,num2str(counter),'.jpg');
if exist(filename,'file')
if (mod(image_counter,100)==0)
fprintf(' ..Processed %d Images\n',image_counter);
%fprintf(' Size of hog features : \n');
%size(hog_features)
end
try
img = im2single(imread(filename));
catch
fprintf('Probelm in file %d',counter);
counter = counter + 1;
continue
end
%gives 128 x num_of_features with one descriptor per column
hog = vl_hog(img,step_size);
[x,y,z] = size(hog);
hog_features = (reshape(hog,[x*y,z]))';
%to reduce precision to 32
features = single(hog_features);
size(features)
[indices, distances] = knnsearch(vocabulary, features');

%generating histogram and then normalizing it
imhist=histc(indices, 1:vocab_size);
imhist_norm=imhist./numel(imhist);
image_features(image_counter,:) = imhist_norm';
image_counter = image_counter + 1;
counter = counter + 1;
else
counter = counter + 1;
end
end

fprintf('Computing Bag of words : Negatives\n');
counter2 = 1;

% for non_vehicle_dataset
image_counter = 1;
while(image_counter <= total_images_negative)
filename = strcat(non_plane_source,num2str(random_numbers(counter2)),'.jpg');
if exist(filename,'file')
if (mod(image_counter,100)==0)
fprintf(' ..Processed %d Images\n',image_counter);
%fprintf(' Size of hog features : \n');
%size(hog_features)
end
try
img = im2single(imread(filename));
% img = imresize(img,0.2);
catch
fprintf('Probelm in file %d',counter2);
counter2 = counter2 + 1;
continue
end
%fprintf('Processed %d Images\n',image_counter);
hog = vl_hog(img,step_size);
[x,y,z] = size(hog);
hog_features = (reshape(hog,[x*y,z]))';
%to reduce precision to 32
features = single(hog_features);

%Find the nearest cluster center in vocabulary for each local feature
%in the image based on the Euclidean distance
%[indices, distances] = KNNSEARCH(vocabulary,features) returns a vector
% distances containing the distances between each row of features and its
% closest point in vocabulary. Each row in 'indices' contains the index of
% the nearest neighbor in vocabulary for the corresponding row in features.
[indices, distances] = knnsearch(vocabulary, features');

%generating histogram and then normalizing it
hist=histc(indices, 1:vocab_size);
hist_norm=hist./numel(hist);
image_features2(image_counter,:) = hist_norm';

image_counter = image_counter + 1;
counter2 = counter2 + 1;
else
counter2 = counter2 + 1;
end
end
fprintf('Size image features : %d\n', size(image_features,1));
fprintf('Size image features2 : %d\n', size(image_features2,1));
image_features = [image_features; image_features2];
fprintf('Size image features again: %d\n', size(image_features,1));
end
123 changes: 123 additions & 0 deletions bag_of_sifts.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
function [image_features] = bag_of_sifts(vocabulary,type_of_dataset)


%Step size for SIFT detection
step_size=4;
size(vocabulary);
vocab_size = size(vocabulary, 1);

if(type_of_dataset)
plane_source = 'D:\Mandeep\Summer\BTP\Tracking\Hog\positives_training_data\';
non_plane_source = 'D:\Mandeep\Summer\BTP\Tracking\Hog\negatives_training_data\';
fprintf('Will find features for training datasets\n');
total_images_positive = 2000;
total_images_negative = 5107; %3000
random_numbers=randi([1 5000],1,total_images_negative);
% image_features = zeros(total_images, vocab_size);
else
plane_source = 'D:\Mandeep\Summer\BTP\Tracking\Hog\positives_test_dataset\';
non_plane_source = 'D:\Mandeep\Summer\BTP\Tracking\Hog\negatives_test_dataset\';
fprintf('Will find features for testing datasets \n');
total_images_positive = 5;
total_images_negative = 5;
random_numbers=randi([1 20],1,total_images_negative);
% image_features = zeros(total_images, vocab_size);
end
%generating histograms of 2000 bins , where value of bin tells most no. of
%matches with different cluster centres


%for plane dataset

counter = 1;
image_counter = 1;
fprintf('Computing Bag of words : Positives\n');
while(image_counter <= total_images_positive)
filename = strcat(plane_source,num2str(counter),'.jpg');
if exist(filename,'file')
if (mod(image_counter,100)==0)
fprintf(' ..Processed %d Images\n',image_counter);
%fprintf(' Size of hog features : \n');
%size(hog_features)
end
try
img = im2single(rgb2gray(imread(filename)));
catch
fprintf('Probelm in file %d',counter);
counter = counter + 1;
continue
end
%gives 128 x num_of_features with one descriptor per column
%hog = vl_hog(img,step_size);
%[x,y,z] = size(hog);
%hog_features = (reshape(hog,[x*y,z]))';
[~, features] = vl_dsift(img, 'Fast', 'Step', step_size);
%to reduce precision to 32
features = single(features);
size(features);
[indices, distances] = knnsearch(vocabulary, features');

%generating histogram and then normalizing it
imhist=histc(indices, 1:vocab_size);
imhist_norm=imhist./numel(imhist);
image_features(image_counter,:) = imhist_norm';
image_counter = image_counter + 1;
counter = counter + 1;
else
counter = counter + 1;
end
end

fprintf('Computing Bag of words : Negatives\n');
counter2 = 1;

% for non_vehicle_dataset
image_counter = 1;
while(image_counter <= total_images_negative)
filename = strcat(non_plane_source,num2str(random_numbers(counter2)),'.jpg');
if exist(filename,'file')
if (mod(image_counter,100)==0)
fprintf(' ..Processed %d Images\n',image_counter);
%fprintf(' Size of hog features : \n');
%size(hog_features)
end
try
img = im2single(rgb2gray(imread(filename)));
% img = imresize(img,0.2);
catch
fprintf('Probelm in file %d',counter2);
counter2 = counter2 + 1;
continue
end
%fprintf('Processed %d Images\n',image_counter);
%hog = vl_hog(img,step_size);
%[x,y,z] = size(hog);
%hog_features = (reshape(hog,[x*y,z]))';
%to reduce precision to 32
[~, features] = vl_dsift(img, 'Fast', 'Step', step_size);
features = single(features);

%Find the nearest cluster center in vocabulary for each local feature
%in the image based on the Euclidean distance
%[indices, distances] = KNNSEARCH(vocabulary,features) returns a vector
% distances containing the distances between each row of features and its
% closest point in vocabulary. Each row in 'indices' contains the index of
% the nearest neighbor in vocabulary for the corresponding row in features.
[indices, distances] = knnsearch(vocabulary, features');

%generating histogram and then normalizing it
hist=histc(indices, 1:vocab_size);
hist_norm=hist./numel(hist);
image_features2(image_counter,:) = hist_norm';

image_counter = image_counter + 1;
counter2 = counter2 + 1;
else
counter2 = counter2 + 1;
end
end
fprintf('Size image features : %d\n', size(image_features,1));
fprintf('Size image features2 : %d\n', size(image_features2,1));
image_features = [image_features; image_features2];
fprintf('Size image features again: %d\n', size(image_features,1));
end
94 changes: 94 additions & 0 deletions detection.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
clc
clear
close all

run('D:/Softwares/vlfeat-0.9.20/toolbox/vl_setup');
files = dir('D:\Mandeep\Summer\BTP\Tracking\negatives_training_data\*.jpg');
count = length(files);
files2 = dir('D:\Mandeep\Summer\BTP\Tracking\HOG\negatives_training_data\*.jpg');
count2 = length(files2);

load('weights.mat');
load('offsets.mat');
load('vocabulary.mat');

filename = '77.jpg';%1 | 2 | 352| 447 |654 | 77 |
img = imread(filename);
original = img;
%imshow(img);


%i - traversing left to right || j - traversing top to bottom || i_n -
%image number
i = 1; j = 1;
% while( (i <= max_y - s_w) && (j <= max_x - s_h))
i_n = 1;
bbox ={};
scores = [];
images_tried = 0;
for scale = 1:-0.1:0.6
img = imresize(original,scale);
%max_y - width of image || max_X - height of image
max_y = size(img,2); max_x = size(img,1);
%s_h - scale_height || s_w - scale_width
s_h = 20;s_w = 45;
for i = max_x/2:10:(max_x/2 + max_x/4)
for j = 1:10:max_y - s_w
%c_i - cropped_image
crop = [j,i,s_w,s_h] ;
c_i = imcrop(img, crop);
%c_i = imread('D:\Mandeep\Summer\BTP\Tracking\Hog\test_data3\1762.jpg');
features = extract_features(c_i,vocabulary);
[plane,score] = classify(features,weights,offsets);
if(plane)
i_n
scores = [scores;score];
crop = [(crop(1)/scale), (crop(2)/scale), (crop(3)/scale),(crop(4)/scale)];
bbox{i_n,1} = crop;
i_n = i_n + 1;
count2 = count2 + 1;
filename = strcat('D:\Mandeep\Summer\BTP\Tracking\Hog\negatives_training_data\',num2str(count2),'.jpg');
imwrite(c_i,filename);
end
plane = 0;
images_tried = images_tried + 1;
end
end
images_tried
images_tried = 0;
end
bbox = cell2mat(bbox);

[selectedBbox,selectedScore] = selectStrongestBbox(bbox,scores,'RatioType','Min','OverlapThreshold',0.2);
img = original;
for i = 1:size(selectedBbox,1)
img = insertShape(img,'rectangle',selectedBbox(i,:),'LineWidth',1);

end
imshow(img);

function [features] = extract_features(img,vocabulary)
img = im2single(rgb2gray(img));
vocab_size = size(vocabulary, 1);
[~, features] = vl_dsift(img, 'Fast', 'Step', 4);
features = single(features);
[indices] = knnsearch(vocabulary, features');
imhist=histc(indices, 1:vocab_size);
imhist_norm=imhist./numel(imhist);
features = imhist_norm';
end

function [plane,score] = classify(features,weights,offsets)
training_score = [];
for i = 1:2
training_score = [training_score; weights{i}'*features' + offsets{i}];
end
[~,label_indices] = max(training_score);
if (label_indices == 2)
score = training_score(label_indices);
plane = 1;
else
plane = 0;
score = 0;
end
end
54 changes: 54 additions & 0 deletions evaluation.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
clc
clear
close all

run('D:/Softwares/vlfeat-0.9.20/toolbox/vl_setup');
files = dir('D:\Mandeep\Summer\BTP\Tracking\HOG\negatives_test_data\*.jpg');
files2 = dir('D:\Mandeep\Summer\BTP\Tracking\HOG\negatives_training_data\*.jpg');
count = length(files);
count2 = length(files2)

load('weights.mat');
load('offsets.mat');
load('vocabulary.mat');
i_n = 0;
for i = 1:6000
filename = strcat('D:\Mandeep\Summer\BTP\Tracking\Hog\negatives_test_data\',num2str(i),'.jpg');
img = imread(filename);
features = extract_features(img,vocabulary);
[plane,score] = classify(features,weights,offsets);
if(plane)
i
i_n = i_n + 1;
count2 = count2 + 1;
filename = strcat('D:\Mandeep\Summer\BTP\Tracking\Hog\negatives_training_data\',num2str(count2),'.jpg');
imwrite(img,filename);
end
end


function [features] = extract_features(img,vocabulary)
img = im2single(rgb2gray(img));
vocab_size = size(vocabulary, 1);
[~, features] = vl_dsift(img, 'Fast', 'Step', 4);
features = single(features);
[indices] = knnsearch(vocabulary, features');
imhist=histc(indices, 1:vocab_size);
imhist_norm=imhist./numel(imhist);
features = imhist_norm';
end

function [plane,score] = classify(features,weights,offsets)
training_score = [];
for i = 1:2
training_score = [training_score; weights{i}'*features' + offsets{i}];
end
[~,label_indices] = max(training_score);
if (label_indices == 2)
score = training_score(label_indices);
plane = 1;
else
plane = 0;
score = 0;
end
end
Loading

0 comments on commit 848c699

Please sign in to comment.