-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.m
More file actions
83 lines (66 loc) · 2.96 KB
/
main.m
File metadata and controls
83 lines (66 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
% shady nikooei
function main
clc;
clear;
close all;
reference_folder = 'E:\shady\term6\Image Process\06_Nikooei_WB_2_403\ex22\source_image';
query_image_path = '6.jpg';
% feature weights for the 8-element feature vector
% [Area, Eccentricity, AspectRatio, Solidity, Perimeter, Circularity, Extent, EulerNumber]
weights = [1, 2, 2, 1, 1, 3, 2, 1];
% build the reference feature library
image_files = dir(fullfile(reference_folder, '*.jpg'));
if isempty(image_files)
error('No reference images found in the specified folder: %s', reference_folder);
end
% preallocation
num_references = length(image_files);
ref_features = zeros(num_references, 8); % Updated to hold 8 features
labels = cell(1, num_references);
% save name of source object
for i = 1:num_references
current_image_path = fullfile(reference_folder, image_files(i).name);
[~, filename, ~] = fileparts(image_files(i).name);
labels{i} = filename;
% save extracted features
img = imread(current_image_path);
ref_features(i, :) = extract_shape_features(img);
end
q_img = imread(query_image_path);
b_qimg = preprocess_image(q_img);
% extract all properties of query objects
properties_to_extract = {'Area', 'BoundingBox', 'Eccentricity', 'Solidity', 'Centroid', 'Perimeter', 'Circularity', 'Extent', 'EulerNumber'};
q_info = regionprops(b_qimg, properties_to_extract);
% classify and display results
figure;
imshow(q_img);
title('Object Detection Results (Enhanced Features)');
hold on;
for i = 1:length(q_info)
% extract all 8 features for the current object
area_i = q_info(i).Area;
ecc_i = q_info(i).Eccentricity;
asp_i = q_info(i).BoundingBox(3) / q_info(i).BoundingBox(4);
sol_i = q_info(i).Solidity;
perim_i = q_info(i).Perimeter;
circ_i = q_info(i).Circularity;
ext_i = q_info(i).Extent;
eul_i = q_info(i).EulerNumber;
obj_feat = [area_i, ecc_i, asp_i, sol_i, perim_i, circ_i, ext_i, eul_i];
% compare with reference library
errors = abs(ref_features - obj_feat);
% normalize size-dependent features (Area and Perimeter)
errors(:, 1) = errors(:, 1) / max(ref_features(:, 1));
errors(:, 5) = errors(:, 5) / max(ref_features(:, 5));
score = sum(errors .* weights, 2);
[~, best_match_idx] = min(score);
detected_label = labels{best_match_idx};
% display the label and bounding box
rectangle('Position', q_info(i).BoundingBox, 'EdgeColor', 'r', 'LineWidth', 2);
centroid = q_info(i).Centroid;
text(centroid(1), centroid(2), detected_label, ...
'Color', 'cyan', 'FontSize', 14, 'FontWeight', 'bold', ...
'HorizontalAlignment', 'center', 'BackgroundColor', 'black');
end
hold off;
end