-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeeEyeClassification.m
More file actions
151 lines (131 loc) · 6.61 KB
/
beeEyeClassification.m
File metadata and controls
151 lines (131 loc) · 6.61 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
% DATASETS: Reshma Basak
% AUTHOR: Kambadur Ananthamurthy
% PURPOSE: Find the standard crop using "getTheEyeCrop.m" and then
% analyse for the "red-ness" of the pixels defining the eye.
% NOTES: Reshma studies bees and her data is in the form of .jpg images, manually sorted based on eye
% colour. The task is to automate loading the datasets, analysis to
% identify the eye and score it based on red/pink-ness.
tic %Start time for code run
clear
close all
%Operations
saveData = 0; % Binary switch. 0: No (for testing). 1: Yes (for production run).
manualMode = 0; % Binary switch. 0: No. 1: Yes.
HOME_DIR = '/home/ananth/Documents/beeEyeClassification';
DATA_DIR = '/home/ananth/Desktop/sorted';
sortedTitle = {'1_very light pink eyes'; ...
'2_light pink eyes'; ...
'3_orange eyes'; ...
'4_brown eyes'; ...
'5_dark brown eyes'; ...
'6_yellow body and dark eyes'; ...
'7_wings darkened and curled'; ...
'8_pigmented almost ready'}; % MATLAB Structure.
%nCaseStudies = length(sortedTitle); % Will pull out largent dimension. Alternatively, use size(sortedTitle, 1).
nCaseStudies = 1; %Test mode
% For MATLAB Structures, call indexed values using var{index}.
% The var(index) nomenclature will pull out a cell array instead of type string.
% Identifying Parameters for filename
hives = {'C1'; 'F'};
%nHives = length(hives);
nHives = 1; %Test mode
batches = {'1'; '2'; '3'};
%nBatches = length(batches);
nBatches = 3; %Test mode
%nPupae = 20; %total
nPupae = 1; %Test mode
pupae = string(1:nPupae); %string array. This would work with integers as well.
%nOrientations = 6; %total
nOrientations = 1; %Test mode
orientations = string(1:nOrientations); %string array
% Setting default parameters for image
% Setting default params for immage.
xmin0 = 1150; %please change to adapt for datasets. This is just the default
ymin0 = 150; %please change to adapt for datasets. This is just the default.
WIDTH = 600; % !! Don't change. Keep the same for all datasets, unless magnification is different
HEIGHT = 600; % !! Don't change. Keep the same for all datasets, unless magnification is different
defaultCrop = [xmin0 ymin0 WIDTH HEIGHT]; %[xmin ymin width height] of reference image - default.
missingDatasets = [];
for topFolderNumber = 1:nCaseStudies
filepath = strcat(DATA_DIR, '/', sortedTitle{topFolderNumber}); %horizontal string concatenation [IMPORTANT: be careful for "/"s. Keep to one format.]
fprintf('>>> [INFO] Now Analyzing Top Folder: %s/%s ...\n', DATA_DIR, sortedTitle{topFolderNumber}) %Prints to console
%File specifics. One should ideally avoid nested loops with more than 3 levels,
%but the following is foolproof.
for hive = 1:nHives
for batch = 1:nBatches
for pupa = 1:nPupae
for orientation = 1:nOrientations
filename = strtrim(sprintf('Hive%s_Batch%s_Pupa%s_%s\n', ...
hives{hive}, ...
batches{batch}, ...
pupae(pupa), ...
orientations(orientation)));
fprintf('>>> [INFO] Filename: %s ...\n', filename)
fullFilePath = strtrim(sprintf('%s/%s.jpg', filepath, filename));
% Analysis Section
%1. Load File
try
image = imread(fullFilePath);
%Alternatively, we could use exist(fullFilePath,
%'dir') to check (might even be faster)
catch
warning('Unable to find %s', fullFilePath) % Includes top directory
%warning('Unable to find %s\n', filepath)
missingDatasets = [missingDatasets; filename];
fprintf('>>> ... Skipping for now ...\n')
continue
end
%2. Crop for eye
if manualMode
disp('>>> [INFO] Manual Mode: Find and save crop coordinates ...')
arguments.DATA_DIR = DATA_DIR;
arguments.filepath = filepath;
arguments.filename = filename;
arguments.topFolderNumber = topFolderNumber;
arguments.sortedTitle = sortedTitle;
arguments.defaultCrop = defaultCrop;
arguments.saveData = saveData;
crop = getTheEyeCrop(arguments);
else
disp('>>> [INFO] Loading crop parameters directly ...')
%load crop parameters from preliminary manual analysis.
load(strtrim(sprintf('%s/%s-cropParams.mat', ...
filepath, ...
filename))); %Loads (conveniently) as a variable "crop"
end
%3. Intensity based binarization
%image = imread(fullFilePath);
fig3 = figure(3);
set(fig3,'Position', [0, 0, 1200, 800]);
clf
subplot(1, 2, 1)
croppedImage = imcrop(image, crop);
imshow(croppedImage)
title(sprintf('Cropped Original | xmin = %d, ymin = %d', crop(1), crop(2)))
%4. Classification metrics
%Convert to grayscale
croppedImage_grayscale = rgb2gray(croppedImage);
subplot(1, 2, 2)
imshow(croppedImage_grayscale)
title(sprintf('Cropped Grayscale | xmin = %d, ymin = %d', crop(1), crop(2)))
% %Split channels - just in case
[croppedImage_red, croppedImage_green, croppedImage_blue] = splitColorChannels(croppedImage, crop, 1);
%Binarize
fig5 = figure(5);
set(fig5, 'Position', [1000, 1000, 1200, 800])
clf
for sensitivityFactor = 0.1:0.1:1.0
subplot(5, 2, sensitivityFactor*10)
croppedImage_binarized = imbinarize(croppedImage_grayscale, ...
"adaptive", "ForegroundPolarity", "bright", "Sensitivity", sensitivityFactor);
imshow(croppedImage_binarized)
title(sprintf('Cropped + Binarized | Sensitivity = %d', sensitivityFactor))
end
disp('...... done!') %disp() is like a sprintf() but not as powerful.
end
end
end
end
disp('... All done!')
end
elapsedTime = toc;