Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified cbires.fig
Binary file not shown.
63 changes: 54 additions & 9 deletions cbires.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

% Edit the above text to modify the response to help cbires

% Last Modified by GUIDE v2.5 23-May-2013 22:01:15
% Last Modified by GUIDE v2.5 18-May-2014 21:53:08

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
Expand Down Expand Up @@ -283,16 +283,22 @@ function btnPlotPrecisionRecall_Callback(hObject, eventdata, handles)
end

% set variables
numOfReturnedImgs = 20;
database = handles.imageDataset.dataset;
metric = get(handles.popupmenu_DistanceFunctions, 'Value');

precAndRecall = zeros(2, 10);
N = 15;
precAndRecall = zeros(2, N);
% sensitivitySpecificity = zeros(2, N);
% fpr_fnr = zeros(2, N);
finPrecRecall = zeros(2, M);

for k = 1:15

for k = 1:N
fprintf('---------------------- loop %d ----------------------\n', k);
randImgName = randi([0 999], 1);
randStrName = int2str(randImgName);
randStrName = strcat('images\', randStrName, '.jpg');
randStrName = strcat(handles.folder_name, '\', randStrName, handles.img_ext);
imgInfo = imfinfo(randStrName);
randQueryImg = imread(randStrName);

% extract query image features
Expand All @@ -307,17 +313,56 @@ function btnPlotPrecisionRecall_Callback(hObject, eventdata, handles)
% construct the queryImage feature vector
queryImageFeature = [hsvHist autoCorrelogram color_moments meanAmplitude msEnergy wavelet_moments randImgName];

disp(['Random Image = ', num2str(randImgName), '.jpg']);
[precision, recall] = svm(numOfReturnedImgs, database, queryImageFeature, metric);
disp(['Random Image = ', num2str(randImgName), handles.img_ext]);
[~, ~, cmat] = svm(handles.numOfReturnedImages, database, queryImageFeature, metric, handles.folder_name, handles.img_ext);
label = checkImageLabel(randImgName);
fprintf('label = %d\n', label);
% conf_table = [tp, fp;
% fn, tn]
conf_table = convertConfusionTable(cmat, label);
% precision = tp/tp+fp
% recall = tp/tp+fn
precision = conf_table(1, 1)/(conf_table(1, 1) + conf_table(1, 2));
recall = conf_table(1, 1)/(conf_table(1, 1) + conf_table(2, 1));
% sensitivity = recall;
% specificity = conf_table(2, 2)/(conf_table(1, 2) + conf_table(2, 2));
% fpr = 1-specificity;
% fnr = 1-sensitivity;
precAndRecall(1, k) = precision;
precAndRecall(2, k) = recall;
% sensitivitySpecificity(1, k) = sensitivity;
% sensitivitySpecificity(2, k) = specificity;
% fpr_fnr(1, k) = fpr;
% fpr_fnr(2, k) = fnr;
fprintf('---------------------- loop %d ----------------------\n', k);
end
finPrecRecall(1, m) = mean(precAndRecall(1, :), 2);
finPrecRecall(2, m) = mean(precAndRecall(2, :), 2);

figure;
plot(precAndRecall(2, :), precAndRecall(1, :), '--mo');
plot(finPrecRecall(1, :), '--co');
hold on;
plot(finPrecRecall(2, :), '--r*');
xlabel('Recall'), ylabel('Precision');
title('Precision and Recall');
legend('Recall & Precision', 'Location', 'NorthWest');
legend('Precision', 'Recal', 'Location', 'NorthWest');
% axis([0 N 0 1]);

% figure;
% plot(sensitivitySpecificity(1, :), '--co')
% hold on;
% plot(sensitivitySpecificity(2, :), '--r*');
% xlabel('Specificity'), ylabel('Sensitivity');
% title('Sensitivity and Specificity');
% legend('Sensitivity', 'Specificity', 'Location', 'NorthWest');
%
% figure;
% plot(fpr_fnr(1, :), '--co')
% hold on;
% plot(fpr_fnr(2, :), '--r*');
% xlabel('False negative rate'), ylabel('False positive rate');
% title('Fpr and Fnr');
% legend('Fpr', 'Fnr', 'Location', 'NorthWest');


% --- Executes on button press in btnSelectImageDirectory.
Expand Down
26 changes: 26 additions & 0 deletions checkImageLabel.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function label = checkImageLabel(imageName)
% return the equivalent label of each image

if (imageName >= 0 && imageName <= 99)
label = 1;
elseif (imageName > 99 && imageName <= 199)
label = 2;
elseif (imageName > 199 && imageName <= 299)
label = 3;
elseif (imageName > 299 && imageName <= 399)
label = 4;
elseif (imageName > 399 && imageName <= 499)
label = 5;
elseif (imageName > 499 && imageName <= 599)
label = 6;
elseif (imageName > 599 && imageName <= 699)
label = 7;
elseif (imageName > 699 && imageName <= 799)
label = 8;
elseif (imageName > 799 && imageName <= 899)
label = 9;
elseif (imageName > 899 && imageName <= 999)
label = 10;
end

end
21 changes: 21 additions & 0 deletions convertConfusionTable.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
% convert a confusion matrix into a confusion table of true/false positives/negatives
function conf_table = convertConfusionTable(cfm, label)
% Returns a confusion table in the following format:
% [true positives, false positives, true+false positives;
% false negatives, true negatives, false+true negatives;
% true+false positives, false+true positives, true positives+false positives+false negatives+true negatives]
% for the given label index in the confusion matrix.

predicted = cfm(:, label);
actual = cfm(label, :);
% for ii = 1:length(cfm)
% actual(1, ii) = cfm(ii, label);
% end
true_pos = predicted(label);
false_pos = sum(actual) - true_pos;
false_neg = sum(predicted) - true_pos;
total = sum(sum(cfm, 2)); %sum column values
true_neg = total - true_pos - false_pos - false_neg;

conf_table = [true_pos, false_pos; false_neg, true_neg];
end