-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessImage.m
More file actions
158 lines (154 loc) · 5.88 KB
/
Copy pathprocessImage.m
File metadata and controls
158 lines (154 loc) · 5.88 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
function [str,score] = processImage(net, path,net2)
% net = neural network that takes a 224x224x3 image and classifies how
% infected the image is, If the result is a binary result, then it will ask
% net2 how infected the sample is.
%
% path = full path to the full image to be classified
%
% net2 = network that specifies how infected a sample is, used if net is a
% binary classification
%
%
f = waitbar(0,'Loading Image');
showIntStep = true; % set true to see the results as they are claculated
index = reshape(1:36*24,[36,24]);
figure;
h = axes;
curImage = im2uint8(imread(path));
imshow(curImage);
subSizeX = 224;
subSizeY = 224;
str = "";
for i = 1:numel(index)
waitbar(i/(numel(index)+1),f,sprintf('Analyizing Image %d out of %d',i, numel(index)));
[row,col,~] = find(index==i,1,'first');
rectPosBbox = [((row-1)*subSizeY)+1,((col-1)*subSizeX)+1,subSizeY-1,subSizeX-1];
if showIntStep
rectangle(h,'Position', rectPosBbox); %#ok<UNRCH>
end
CurSubImage = imcrop(curImage,rectPosBbox);
drawnow;
% test if the image contains a sample
[isSample, ~ ] = isSampleMask(CurSubImage);
if isSample == false
choice = "0";
score{i} = 1;
else
[pred,score{i}] = classify(net,CurSubImage);
switch lower(char(pred))
case 'none'
if nargin == 3
choice = "1";
else
choice = "1";
end
case 'low'
choice = "2";
case 'moderate'
choice = "3";
case 'high'
choice = "4";
case 'outoffocus'
choice = "5";
case 'infection'
if nargin == 3
[pred2,score{i}] = classify(net2,CurSubImage);
switch lower(char(pred2))
case 'none'
choice = "1";
case 'low'
choice = "2";
case 'moderate'
choice = "3";
case 'high'
choice = "4";
case 'outoffocus'
choice = "5";
otherwise
warn('Image returned a classification not listed above');
keyboard
end
else
choice = "6";
end
otherwise
warn('Image returned a classification not listed above');
keyboard
end
end
str = strcat(str,choice);
[row,col,~] = find(index<=strlength(str));
if ~isempty(row) && showIntStep
p1 = ((row-1)*subSizeY)+1;
p2 = ((col-1)*subSizeX)+1;
p4 = ones(size(p1))*subSizeX-1;
p3 = ones(size(p1))*subSizeY-1;
points = bbox2points([p1,p2,p3,p4]);
points = permute(points,[2,1,3]);
vert = reshape(points,[2,numel(points)/2])';
ind = 1:length(vert);
ind = reshape(ind,[4,length(ind)/4])';
textLoc = squeeze(mean(points(:,[1,3],:),2))';
textLab = cellfun(@(x) sprintf('%0.2g',max(x)),score,'UniformOutput',false);
exp = {'0','1','2','3','4','5'};
rep = {'k','b','g','y','r','c'};
c = regexprep(char(str),exp,rep);
color = c(:);
if exist('p','var')&&~isempty(p)
delete(p{1});
delete(p{2});
delete(p{3});
delete(p{4});
delete(p{5});
delete(p{6});
end
p{1} = patch(h,'Faces',ind(color=='k',:),'vertices',vert,'FaceColor','k','FaceAlpha',.2);
p{2} = patch(h,'Faces',ind(color=='b',:),'vertices',vert,'FaceColor','b','FaceAlpha',.2);
p{3} = patch(h,'Faces',ind(color=='r',:),'vertices',vert,'FaceColor','r','FaceAlpha',.2);
p{4} = patch(h,'Faces',ind(color=='g',:),'vertices',vert,'FaceColor','g','FaceAlpha',.2);
p{5} = patch(h,'Faces',ind(color=='y',:),'vertices',vert,'FaceColor','y','FaceAlpha',.2);
p{6} = patch(h,'Faces',ind(color=='c',:),'vertices',vert,'FaceColor','c','FaceAlpha',.2);
if strlength(str) == 1
t = text(h,textLoc(:,1),textLoc(:,2),textLab,'HorizontalAlignment','center');
else
t(end+1) = text(h,textLoc(end,1),textLoc(end,2),textLab(end),'HorizontalAlignment','center');
end
end
end
waitbar(i+1/(numel(index)+1),f,'Plotting Results');
[row,col,~] = find(index<=strlength(str));
if ~isempty(row)
p1 = ((row-1)*subSizeY)+1;
p2 = ((col-1)*subSizeX)+1;
p4 = ones(size(p1))*subSizeX-1;
p3 = ones(size(p1))*subSizeY-1;
points = bbox2points([p1,p2,p3,p4]);
points = permute(points,[2,1,3]);
vert = reshape(points,[2,numel(points)/2])';
ind = 1:length(vert);
ind = reshape(ind,[4,length(ind)/4])';
textLoc = squeeze(mean(points(:,[1,3],:),2))';
textLab = cellfun(@(x) sprintf('%0.2g',max(x)),score,'UniformOutput',false);
exp = {'0','1','2','3','4','5','6'};
rep = {'k','b','g','y','r','c','g'};
c = regexprep(char(str),exp,rep);
color = c(:);
if exist('p','var')&&~isempty(p)
delete(p{1});
delete(p{2});
delete(p{3});
delete(p{4});
delete(p{5});
delete(p{6});
delete(t);
end
patch(h,'Faces',ind(color=='k',:),'vertices',vert,'FaceColor','k','FaceAlpha',.2);
patch(h,'Faces',ind(color=='b',:),'vertices',vert,'FaceColor','b','FaceAlpha',.2);
patch(h,'Faces',ind(color=='r',:),'vertices',vert,'FaceColor','r','FaceAlpha',.2);
patch(h,'Faces',ind(color=='g',:),'vertices',vert,'FaceColor','g','FaceAlpha',.2);
patch(h,'Faces',ind(color=='y',:),'vertices',vert,'FaceColor','y','FaceAlpha',.2);
patch(h,'Faces',ind(color=='c',:),'vertices',vert,'FaceColor','c','FaceAlpha',.2);
text(h,textLoc(:,1),textLoc(:,2),textLab,'HorizontalAlignment','center');
end
delete(f);
end