-
Notifications
You must be signed in to change notification settings - Fork 0
/
AugmentDataset.m
138 lines (99 loc) · 4.58 KB
/
AugmentDataset.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
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
%
% This script will create an augmented image dataset by flipping and
% rotating images. It will multiply the number of images in the dataset
% by four.
%
% Options
DataType = 'RGB'; % RGB, HSI
%
ImagesDir = 'images';
LabelDir = 'labels';
HsiDataFIleExt = '.dat';
LblDataFileExt = '.png';
% Get the dataset folder
dataSetDir = uigetdir();
dirList = dir(dataSetDir);
for nFile = 1: length(dirList)
if contains(ImagesDir, dirList(nFile).name)
imagesDir = fullfile(dataSetDir, dirList(nFile).name);
elseif contains(LabelDir, dirList(nFile).name)
labelsDir = fullfile(dataSetDir, dirList(nFile).name);
end
end
% Augment images
if strcmp(DataType, 'RGB')
rgbDatFilesList = dir(imagesDir);
noOfRgbFiles = length(rgbDatFilesList) - 2;
rgbFiles = strings(noOfRgbFiles, 1);
rgbFileCount = 1;
for nRgbImg = 1: length(rgbDatFilesList)
if contains(rgbDatFilesList(nRgbImg).name, LblDataFileExt)
rgbFiles(rgbFileCount, 1) = fullfile(imagesDir, rgbDatFilesList(nRgbImg).name);
rgbFileCount = rgbFileCount + 1;
end
end
for nRgbImgFile = 1: length(rgbFiles)
rgbImgTemp = imread(rgbFiles(nRgbImgFile, 1));
fNameWoExtRgb = erase(rgbFiles(nRgbImgFile, 1), LblDataFileExt);
%rotatedClkFileNameLbl = strcat(fNameWoExtLbl, '_clk', '.png');
FlpdFileNameRgb = strcat(fNameWoExtRgb, '_flp', '.png');
%rotatedClkFlpFileNameLbl = strcat(fNameWoExtLbl, '_clk_flp', '.png');
%rotateClkLbl = imrotate(labelTemp, -90);
flippedRgbImg = flip(rgbImgTemp, 2);
%rotateClkFlpdLbl = flip(rotateClkLbl, 1);
%imwrite(rotateClkLbl, rotatedClkFileNameLbl);
imwrite(flippedRgbImg, FlpdFileNameRgb);
%imwrite(rotateClkFlpdLbl, rotatedClkFlpFileNameLbl);
end
elseif strcmp(DataType, 'HSI')
hdrDatFilesList = dir(imagesDir);
noOfFiles = (length(hdrDatFilesList) - 2) / 2;
hsiFiles = strings(noOfFiles, 1);
fileCount = 1;
for nHsi = 1: length(hdrDatFilesList)
if contains(hdrDatFilesList(nHsi).name, HsiDataFIleExt)
hsiFiles(fileCount, 1) = fullfile(imagesDir, hdrDatFilesList(nHsi).name);
fileCount = fileCount + 1;
end
end
for nHsiFile = 1: length(hsiFiles)
hypercubeTemp = hypercube(hsiFiles(nHsiFile, 1));
fNameWoExt = erase(hsiFiles(nHsiFile, 1), HsiDataFIleExt);
%rotatedClkFileName = strcat(fNameWoExt, '_clk');
FlpdFileName = strcat(fNameWoExt, '_flp');
%rotatedClkFlpFileName = strcat(fNameWoExt, '_clk_flp');
%rotateClkHsi = RotateHsiImage(hypercubeTemp.DataCube, -90);
flippedHsi = FlipHsiImage(hypercubeTemp.DataCube, 'H');
%rotateClkFlpdHsi = FlipHsiImage(rotateClkHsi, 'H');
%hypercubeTemp = assignData(hypercubeTemp, ':', ':', ':', rotateClkHsi);
%enviwrite(hypercubeTemp, rotatedClkFileName, 'Interleave','bil');
hypercubeTemp = assignData(hypercubeTemp, ':', ':', ':', flippedHsi);
enviwrite(hypercubeTemp, FlpdFileName, 'Interleave','bil');
%hypercubeTemp = assignData(hypercubeTemp, ':', ':', ':', rotateClkFlpdHsi);
%enviwrite(hypercubeTemp, rotatedClkFlpFileName, 'Interleave','bil');
end
end
%% Augment labels
pngLblFilesList = dir(labelsDir);
noOfLblFiles = length(pngLblFilesList) - 2;
lblFiles = strings(noOfLblFiles, 1);
lblFileCount = 1;
for nLblImg = 1: length(pngLblFilesList)
if contains(pngLblFilesList(nLblImg).name, LblDataFileExt)
lblFiles(lblFileCount, 1) = fullfile(labelsDir, pngLblFilesList(nLblImg).name);
lblFileCount = lblFileCount + 1;
end
end
for nLabelFile = 1: length(lblFiles)
labelTemp = imread(lblFiles(nLabelFile, 1));
fNameWoExtLbl = erase(lblFiles(nLabelFile, 1), LblDataFileExt);
%rotatedClkFileNameLbl = strcat(fNameWoExtLbl, '_clk', '.png');
FlpdFileNameLbl = strcat(fNameWoExtLbl, '_flp', '.png');
%rotatedClkFlpFileNameLbl = strcat(fNameWoExtLbl, '_clk_flp', '.png');
%rotateClkLbl = imrotate(labelTemp, -90);
flippedLbl = flip(labelTemp, 2);
%rotateClkFlpdLbl = flip(rotateClkLbl, 1);
%imwrite(rotateClkLbl, rotatedClkFileNameLbl);
imwrite(flippedLbl, FlpdFileNameLbl);
%imwrite(rotateClkFlpdLbl, rotatedClkFlpFileNameLbl);
end