-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVM_detection.m
96 lines (85 loc) · 2.82 KB
/
SVM_detection.m
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
84
85
86
87
88
89
90
91
92
93
94
95
96
counter = 0;
for i=1:100
detection = load(sprintf('../data/Detection/img%i/img%i_detection.mat', i, i));
detection = detection.detection;
counter = counter + size(detection, 1);
for k = 1:5
detection = load(sprintf('../data/Detection/img%i/img%i_%i_detection.mat', i, i, k));
detection = detection.detection;
counter = counter + size(detection, 1);
end
end
fprintf('Total cells: # %i\n',counter);
traningFeature = zeros(1,900);
counter = 1;
for i=1:100
fprintf('Extracting feature of image # %i\n',i);
img = imread(sprintf('../data/Detection/img%i/img%i.bmp', i, i));
detection = load(sprintf('../data/Detection/img%i/img%i_detection.mat', i, i));
detection = detection.detection;
for j = 1:size(detection, 1)
c = detection(j, :);
x = round(c(1));
y = round(c(2));
x1 = max(1, x-6);
x2 = min(500, x+6);
y1 = max(1, y-6);
y2 = min(500, y+6);
if x1 == 1
x2 = 13;
end
if x2 == 500
x1 = 488;
end
if y1 == 1
y2 = 13;
end
if y2 == 500
y1 = 488;
end
patch = img(y1:y2, x1:x2,:);
[featureVector,hogVisualization] = extractHOGFeatures(patch, 'CellSize', [2 2]);
traningFeature(counter,:) = featureVector;
counter = counter + 1;
end
for k = 1:5
img = imread(sprintf('../data/Detection/img%i/img%i_%i.bmp', i, i, k));
detection = load(sprintf('../data/Detection/img%i/img%i_%i_detection.mat', i, i, k));
detection = detection.detection;
for j = 1:size(detection, 1)
c = detection(j, :);
x = round(c(1));
y = round(c(2));
x1 = max(1, x-6);
x2 = min(500, x+6);
y1 = max(1, y-6);
y2 = min(500, y+6);
if x1 == 1
x2 = 13;
end
if x2 == 500
x1 = 488;
end
if y1 == 1
y2 = 13;
end
if y2 == 500
y1 = 488;
end
patch = img(y1:y2, x1:x2,:);
[featureVector,hogVisualization] = extractHOGFeatures(patch, 'CellSize', [2 2]);
traningFeature(counter,:) = featureVector;
counter = counter + 1;
end
end
end
trainingLabels = zeros(counter-1, 1);
for i = 1:counter-1
trainingLabels(i) = 1;
end
% Here I train a linear support vector machine (SVM) classifier.
svmmdl = fitcsvm(trainingFeatures ,trainingLabels);
save('SVMmodel1.mat', 'svmmdl');
% Perform cross-validation and check accuracy
cvmdl = crossval(svmmdl,'KFold',10);
fprintf('kFold CV accuracy: %2.2f\n',1-cvmdl.kfoldLoss)