-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGen_TrainingSet.m
114 lines (79 loc) · 2.83 KB
/
Gen_TrainingSet.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
% Google Maps image data directories.
satImgDir = fullfile('Test_GMapsImgs\satellite\');
mapImgDir = fullfile('Test_GMapsImgs\map');
% Instantiate image data store.
satImgs = imageDatastore(satImgDir);
satImgs.ReadFcn = @rgbImgReader;
mapImgs = imageDatastore(mapImgDir);
trainingSet_Size = numel(satImgs.Files);
% Generate Low/Med/High Disaster Rating Training Sets
for i = 1:trainingSet_Size
sat_raw = Crop_Icon(readimage(satImgs, i));
map_raw = Crop_Icon(readimage(mapImgs, i));
disaster_level = mod(i,3) + 1;
nominal_sat_roads = Isolate_Roads(sat_raw, map_raw);
disaster_sat_roads = Gaussian_Noise(nominal_sat_roads, disaster_level);
disaster_impact = nominal_sat_roads - disaster_sat_roads;
imwrite(disaster_impact, [int2str(disaster_level),'_Level', '\', 'data', int2str(i), '.png'])
end
%% Simulated Training Data Example:
n = 5;
sat_raw = Crop_Icon(readimage(satImgs, n));
map_raw = Crop_Icon(readimage(mapImgs, n));
nominal_sat_roads = Isolate_Roads(sat_raw, map_raw);
disaster_sat_roads = Gaussian_Noise(nominal_sat_roads, 3);
disaster_impact = nominal_sat_roads - disaster_sat_roads;
fig = figure;
imshow(sat_raw)
title('Satellite Image')
subplot(2,2,2);
imshow(map_raw > min(map_raw(:)))
title('Road Map Mask')
subplot(2,2,3);
imshow(nominal_sat_roads)
title('Satellite Road Isolation')
subplot(2,2,4);
imshow(disaster_sat_roads)
title('Simulated Disaster Effects - Gaussian Noise')
function roads = Isolate_Roads(sat_raw, map_raw)
% Isolates Roads by applying a thresholded map view image mask on the
% satellite view image.
road_binary = double(repmat(Road_Mask(map_raw),1,1,3));
roads = sat_raw.*road_binary;
end
function mask = Road_Mask(map_raw)
% Generate road binary from Google Maps map view image.
% Extract the individual red, green, and blue color channels.
redChannel = map_raw(:, :, 1);
%greenChannel = map_raw(:, :, 2);
%blueChannel = map_raw(:, :, 3);
% Find white, where each color channel is more than 250
thresholdValue = min(map_raw(:));
mask = redChannel > thresholdValue;
%mask = redChannel > thresholdValue & greenChannel > thresholdValue & blueChannel > thresholdValue;
end
% Add gaussian noise to the roads based on the disaster intensity
% classification.
function noisy_road = Gaussian_Noise(road_nominal, disaster_rating)
m = mean(road_nominal(:));
% Blur variance determined based on disaster level
switch (disaster_rating)
case 1
gauss_var = m;
case 2
gauss_var = m*10;
case 3
gauss_var = m*100;
end
% Apply gaussian noise to image
noise = double(imnoise(zeros(size(road_nominal)), 'gaussian', 1, gauss_var));
noisy_road = road_nominal.*noise;
end
function cImg = Crop_Icon(img)
c = 50;
cImg = img(1:end-c,:,:);
end
function data = rgbImgReader(filename)
[im,cmap] = imread(filename);
data = ind2rgb(im,cmap);
end