added maha dist

This commit is contained in:
aj 2019-11-29 19:33:07 +00:00
parent a2575085de
commit 521f52a397
7 changed files with 52 additions and 36 deletions

View File

@ -22,10 +22,10 @@ OUT_FOLDER = 'descriptors';
%% and within that folder, create another folder to hold these descriptors
%% the idea is all your descriptors are in individual folders - within
%% the folder specified as 'OUT_FOLDER'.
OUT_SUBFOLDER='avgRGB';
% OUT_SUBFOLDER='avgRGB';
% OUT_SUBFOLDER='globalRGBhisto';
% OUT_SUBFOLDER='spatialColour';
% OUT_SUBFOLDER='spatialColourTexture';
OUT_SUBFOLDER='spatialColourTexture';
allfiles=dir (fullfile([DATASET_FOLDER,'/Images/*.bmp']));
for filenum=1:length(allfiles)
@ -37,10 +37,10 @@ for filenum=1:length(allfiles)
fout=[OUT_FOLDER,'/',OUT_SUBFOLDER,'/',fname(1:end-4),'.mat'];%replace .bmp with .mat
%% EXTRACT FUNCTION
F=extractAvgRGB(img);
% F=extractAvgRGB(img);
% F=extractGlobalColHist(img);
% F=extractSpatialColour(img);
% F=extractSpatialColourTexture(img);
F=extractSpatialColourTexture(img);
save(fout,'F');
toc
end

View File

@ -27,10 +27,10 @@ DATASET_FOLDER = 'dataset';
DESCRIPTOR_FOLDER = 'descriptors';
%% and within that folder, another folder to hold the descriptors
%% we are interested in working with
DESCRIPTOR_SUBFOLDER='avgRGB';
% DESCRIPTOR_SUBFOLDER='avgRGB';
% DESCRIPTOR_SUBFOLDER='globalRGBhisto';
% DESCRIPTOR_SUBFOLDER='spatialColour';
% DESCRIPTOR_SUBFOLDER='spatialColourTexture';
DESCRIPTOR_SUBFOLDER='spatialColourTexture';
CATEGORIES = ["Farm Animal"
"Tree"
@ -95,23 +95,17 @@ for iterating_category=1:CAT_TOTAL
%% 2) Select descriptors for category and training data
category_training_descriptors = [];
test_descriptors = [];
test_categories = [];
for i=1:NIMG
if iterating_category == ALLCATs(i)
if (iterating_category == ALLCATs(i)) && (size(category_training_descriptors,1) < MODEL_SIZE)
category_training_descriptors = [ category_training_descriptors ; ALLFEAT(i,:) ];
else
test_descriptors = [ test_descriptors ; ALLFEAT(i,:) ];
test_categories = [ test_categories ; ALLCATs(i) ];
end
end
model_descriptors = category_training_descriptors(1:MODEL_SIZE, :);
model_mean = mean(model_descriptors);
model_data_min_mean = model_descriptors - repmat(model_mean, MODEL_SIZE, 1);
C = (model_data_min_mean' * model_data_min_mean) ./ MODEL_SIZE;
[eig_vct, eig_val] = eig(C);
[eig_vct, eig_val, model_mean] = getEigenModel(category_training_descriptors);
TEST_SIZE = size(test_descriptors,1);
@ -119,21 +113,19 @@ for iterating_category=1:CAT_TOTAL
dst=[];
for i=1:TEST_SIZE
candidate=test_descriptors(i,:);
query=ALLFEAT(queryimg,:);
category=ALLCATs(i);
category=test_categories(i);
%% COMPARE FUNCTION
thedst=compareEuclidean(query, candidate);
thedst=compareMahalanobis(eig_vct, eig_val, model_mean, candidate);
dst=[dst ; [thedst i category]];
end
dst=sortrows(dst,1); % sort the results
%% 4) Calculate PR
precision_values=zeros([1, NIMG]);
recall_values=zeros([1, NIMG]);
precision_values=zeros([1, TEST_SIZE]);
recall_values=zeros([1, TEST_SIZE]);
correct_at_n=zeros([1, NIMG]);
correct_at_n=zeros([1, TEST_SIZE]);
query_row = dst(1,:);
query_category = query_row(1,3);
@ -141,7 +133,7 @@ for iterating_category=1:CAT_TOTAL
%calculate PR for each n
for i=1:NIMG
for i=1:TEST_SIZE
rows = dst(1:i, :);
@ -182,8 +174,8 @@ for iterating_category=1:CAT_TOTAL
%% 5) calculate AP
P_rel_n = zeros([1, NIMG]);
for i = 1:NIMG
P_rel_n = zeros([1, TEST_SIZE]);
for i = 1:TEST_SIZE
precision = precision_values(i);
i_result_relevant = correct_at_n(i);

View File

@ -1,7 +1,7 @@
function F=extractSpatialColourTexture(img)
grid_rows = 10;
grid_columns = 10;
grid_rows = 4;
grid_columns = 4;
img_size = size(img);
img_rows = img_size(1);
@ -43,7 +43,7 @@ for i = 1:grid_rows
avg_vals = extractAvgRGB(img_cell);
[mag_img, angle_img] = getEdgeInfo(grey_img_cell);
edge_hist = getEdgeAngleHist(mag_img, angle_img);
edge_hist = getEdgeAngleHist(mag_img, angle_img, 6, 0.05);
%concatenate average values into vector
descriptor = [descriptor edge_hist avg_vals(1) avg_vals(2) avg_vals(3)];

View File

@ -0,0 +1,11 @@
function dst=compareMahalanobis(vct, val, mean, F2)
x_minus_mean = (F2 - mean)';
matrices = val' * vct' * x_minus_mean;
x=matrices.^2;
x=sum(x);
dst=sqrt(sqrt(x));
return;

View File

@ -1,2 +1,2 @@
getRandomCategoryImage(7)
loadDescriptors()

View File

@ -1,7 +1,4 @@
function F=getEdgeAngleHist(mag_img, angle_img)
bins = 8;
threshold = 0.05;
function F=getEdgeAngleHist(mag_img, angle_img, bins, threshold)
dimensions = size(angle_img);
rows = dimensions(1);
@ -13,7 +10,7 @@ for i = 1:rows
if mag_img(i, j) > threshold
bin_value = angle_img(i, j) / (2 * pi);
bin_value = bin_value * bins;
bin_value = floor(bin_value * bins);
vals = [vals bin_value];
@ -21,5 +18,9 @@ for i = 1:rows
end
end
F= histogram(vals, bins, 'Normalization', 'probability').Values;
if size(vals, 2) == 0
F = zeros(1, bins)
else
F= histogram(vals, bins, 'Normalization', 'probability').Values;
end
return;

12
util/getEigenModel.m Normal file
View File

@ -0,0 +1,12 @@
function [eig_vct, eig_val, model_mean]=getEigenModel(model_descriptors)
model_size = size(model_descriptors, 1);
model_mean = mean(model_descriptors);
model_data_min_mean = model_descriptors - repmat(model_mean, model_size, 1);
C = (model_data_min_mean' * model_data_min_mean) ./ model_size;
[eig_vct, eig_val] = eig(C);
return;