-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.m
84 lines (68 loc) · 1.84 KB
/
main.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
%% Main function for water logged area detection %%
%%
clc;
clear all;
close all;
% Read mean from training images
[redMean, greenMean, blueMean] = meansOfFlood();
%% Read image
img = imread('test6.jpg');
[row col dim] = size(img);
im = double(img);
red = im(:, :, 1);
green = im(:, :, 2);
blue = im(:, :, 3);
%% Color analysis
for x=1:1:row
for y=1:1:col
redVal = abs(red(x,y) - redMean);
greenVal = abs(green(x,y) - greenMean);
blueVal = abs(blue(x,y) - blueMean);
sd = 48;
if((redVal <= sd) && (greenVal <= sd) && (blueVal <= sd))
biIm(x,y) = 1;
else
biIm(x,y) = 0;
end
end
end
%% Remove noise effect and narrow connection
sedisk = strel('disk',2);
openedIm = imopen(biIm, sedisk);
%% Filling small holes inside detected region
closedIm = imclose(openedIm, sedisk);
%% Delete small objects
numberOfPixels = numel(biIm);
removeTh = round(numberOfPixels - numberOfPixels * 90 / 100); % remove when pixel number 8% less than total pixel;
filteredIm = bwareaopen(closedIm, removeTh);
%% If water logged area found
numberOfTruePixels = sum(filteredIm(:));
if(numberOfTruePixels > 0)
disp('Warning: Water logged area! ');
% Making output image if it contain water
for x=1:1:row
for y=1:1:col
if(filteredIm(x,y) > 0)
red(x,y) = 255;
green(x,y) = 0;
blue(x,y) = 0;
else
red(x,y) = red(x,y);
green(x,y) = green(x,y);
blue(x,y) = blue(x,y);
end
end
end
% Make RGB image from individual R, G, B plane
newIm = cat(3, red, green, blue);
%% No water logged area
else
disp('No Water');
newIm = img;
end
subplot(1,2,1);
imshow(uint8(img));
title('Input');
subplot(1,2,2);
imshow(uint8(newIm));
title('Output');