-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathElicioDom__Progetto_Oil_Spill_Detection.m
603 lines (496 loc) · 31.7 KB
/
ElicioDom__Progetto_Oil_Spill_Detection.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
close all
clear all
clc
%To count the program's execution time: (tic/toc)
tic
choice=99;
fileidx=1;
%Continue running program until user input is "2"
while choice~=2
%Set the path to load images and labels
extension_img='*.jpg';
extension_labels='*.png';
imagesDir = 'C:\Users\Oil Spill Detection Dataset\train\images';
landImagesDir='C:\Users\Oil Spill Detection Dataset\train\images_with_land';
labelsDir = 'C:\Users\Oil Spill Detection Dataset\train\labels';
landLabelsImagesDir='C:\Users\Oil Spill Detection Dataset\train\labels_with_land';
imagesDircontent=dir(fullfile(imagesDir,extension_img));
landImagesDircontent=dir(fullfile(landImagesDir,extension_img));
labelsDircontent=dir(fullfile(labelsDir,extension_labels));
landLabelsImagesDircontent=dir(fullfile(landLabelsImagesDir,extension_labels));
%Check that the path is correct
assert(numel(imagesDircontent) > 0, 'No file was found');
assert(numel(labelsDircontent) > 0, 'No file was found');
assert(numel(landImagesDircontent) > 0, 'No file was found');
assert(numel(landLabelsImagesDircontent) > 0, 'No file was found');
%preallocation of the structures:
my_img = struct('img', cell(size(imagesDircontent)));
my_landImg = struct('img', cell(size(landImagesDircontent)));
my_label = struct('img', cell(size(labelsDircontent)));
my_landLabel = struct('img', cell(size(landLabelsImagesDircontent)));
%% Change "fileidx" value to change the image to use
%Put a flag to know if you're going to use ONLY SEA images or LAND & SEA
%images (ONLY SEA--> flag=true(1)) :
fprintf("\n------------------------------------------------------------------------------------------------")
fprintf("\nThere are 2 types of image you can analyze: \n0- LAND + SEA images\n1- ONLY SEA images\n2- EXIT the program\n \n");
choice=input("Make your choice! ---> ");
if choice==0
close all,tic
flag=0;
fprintf("\nThere are 219 images of this type, please enter a number to analyze the image you want: \n")
choice2=input("Make your choice! ---> ");
if choice2<1 || choice2>=220
fprintf("\nWRONG NUMBER!\nThere are 219 images of this type, please enter a new number: \n")
choice2=input("Make your choice! ---> ");
else
fileidx=choice2;
end
elseif choice==1
close all,tic
flag=1;
fprintf("\nThere are 783 images of this type, please enter a number to analyze the image you want: \n")
choice2=input("Make your choice! ---> ");
if choice2<1 || choice2>=784
fprintf("\nWRONG NUMBER!\nThere are 783 images of this type, please enter a new number: \n")
choice2=input("Make your choice! ---> ");
else
fileidx=choice2;
end
elseif choice==2
close all
fprintf("\nThanks for using this segmentation program!\n")
return
else
close all
fprintf("\n------------------------------------------------------------------------------------------------")
fprintf("\nINPUT ERROR!\nYou can choose only ""0"" ""1"" ""2"" values, please run the program again.\n");
return
end
%If flag==true you're considering ONLY SEA images, otherwise LAND+SEA images
if flag==true
%Only sea with oil spills images
my_img(fileidx).img=imread(fullfile(imagesDir,imagesDircontent(fileidx).name));
my_label(fileidx).img=imread(fullfile(labelsDir,labelsDircontent(fileidx).name));
I=im2double(my_img(fileidx).img);
label=my_label(fileidx).img;
else
%Land & oil spills images
my_LandImg(fileidx).img=imread(fullfile(landImagesDir,landImagesDircontent(fileidx).name));
my_LandLabel(fileidx).img=imread(fullfile(landLabelsImagesDir,landLabelsImagesDircontent(fileidx).name));
I=im2double(my_LandImg(fileidx).img);
label=my_LandLabel(fileidx).img;
end
%Load and show image and label to use
groundTruth1=im2double(label);
%User has chosen land+sea images
if flag==false
%You have to extract the two labels (cyan and green color (that have 0.7 and 0.35 values of gray intensity)) from the label images in the dataset:
groundTruth=imbinarize(rgb2gray(groundTruth1),0.7);
groundTruth2=imbinarize(rgb2gray(groundTruth1),0.35);
%If you have an image with land, you have to evaluate the complete segmentation (land + oil spill):
groundTruth=or(groundTruth,groundTruth2);
else
%Else if user has chosen ONLY SEA images you have to extract only cyan color from the labelled image in the dataset
groundTruth=imbinarize(rgb2gray(groundTruth1),0.7);
end
figure('WindowState', 'maximized');
subplot(1,4,[1 2]),imshow(I),title('Original Image');
subplot(1,4,[3 4]),imshow(groundTruth1),title('Ground truth');
%RGB to gray conversion
grayImg=rgb2gray(I);
% Gray image histogram:
%figure,imhist(grayImg),title('Gray Image Histogram')
if choice==1
fprintf("\n\nThere are different segmentation method you can use, please choose a number!\n")
fprintf("1 - MANUAL THRESHOLDING SEGM.\n")
fprintf("2 - AUTOMATIC THRESHOLDING SEGM.\n")
fprintf("3 - LOCAL ADAPTIVE THRESHOLDING SEGM.\n")
fprintf("4 - SUPERPIXEL + OTSU THRESHOLDING SEGM.\n")
fprintf("5 - FUZZY LOGIC EDGE DETECTION & SEGM.\n")
fprintf("6 - K-MEANS CLUSTERING & SEGM.\n")
choice3=input("Make your choice! ---> ");
switch choice3
case 1 % --------------- MANUAL THRESHOLDING ----------------------- %
%Default value for thresholds and minimum area of the blobs
addThreshval=0.06;
areaToConsider=45;
medianFilter=3;
[BWmanual,max_value]=manual_thresholding(grayImg,groundTruth1,addThreshval,areaToConsider,medianFilter);
figure('WindowState', 'maximized');
segmentation_evaluation(groundTruth,BWmanual);
%User can manually improve segmentation results by changing some parameters
fprintf("\nWould you like to improve segmentation results? (Median filter size, Tresh values and blob areas) [y/n]")
yesOrNo=input("Press ""y"" or ""n"" --->",'s');
while strcmpi(yesOrNo,'y')
close all
%User can modify median filter's window size
fprintf("\nYou can modify the segmentation results by setting a new value for the noise removal filter (median filter)\n If you don't make a choice, default value is 3x3 \n")
medianFilter=input("-- FILTER VALUE-- \nMake your choice or just press Enter ---> ");
if isempty(medianFilter)
medianFilter=3;
else
while medianFilter<3 || medianFilter>243 || rem(medianFilter,2)==0
fprintf('The value of the filter should be a number >3 and <243 and it has to be an ODD number. Please insert a valid value\n ')
medianFilter=input("-- FILTER VALUE -- \nMake your choice! ---> ");
end
end
%User can modify threshold to recognize oil spill (increase or decrease previous threshold)
fprintf("\nYou can adjust the segmentation results by increasing or decreasing the thresold value of the mask\n Admitted values are 0<x<1. If you don't make a choice, DEFAULT VALUE is 0.15\n")
%Maximum threshold value at the last analysis done:
fprintf('\n\nMaximum threshold value NOW: %f\n',max_value)
addThreshval=input("-- THRESHOLD VALUE -- \nMake your choice or just press Enter ---> ");
if isempty(addThreshval)
addThreshval=0;
end
%User canmodify the minimum area to recognize an oil spill
fprintf("\nYou can also adjust the AREA value of the blobs to recognize it as oil spill. (DEFAULT VALUE: 45px)\n")
areaToConsider=input("-- AREA VALUE -- \nMake your choice or just press Enter ---> ");
if isempty(areaToConsider)
areaToConsider=45;
else
while areaToConsider<=0 || areaToConsider>422500
fprintf('The value of the area should be a number >0 and <422500m^2 (650*650px). Please insert a valid value\n ')
areaToConsider=input("-- AREA TO EXPLORE -- \nMake your choice! ---> ");
end
end
[BWmanual,max_value]=manual_thresholding(grayImg,groundTruth1,max_value+addThreshval,areaToConsider,medianFilter);
figure('WindowState', 'maximized');
segmentation_evaluation(groundTruth,BWmanual);
fprintf("\nWould you like to improve segmentation results? [y/n]")
yesOrNo=input("Press ""y"" or ""n"" --->",'s');
end
case 2 % --------------- AUTOMATIC THRESHOLDING ----------------------- %
%default value for area of the blobs
areaToExplore=45;
medFilter=3;
[automatic_thresh,BWautomatic]=automatic_threshold(grayImg,groundTruth1,areaToExplore,medFilter);
figure('WindowState', 'maximized');
segmentation_evaluation(groundTruth,BWautomatic);
%User can manually improve segmentation results by changing some parameters
fprintf("\nWould you like to improve segmentation results? (Median filter size / Blob area values) [y/n]")
yesOrNo=input("Press ""y"" or ""n"" --->",'s');
while strcmpi(yesOrNo,'y')
close all
%User can modify median filter's window size
fprintf("\nYou can modify the segmentation results by setting a new value for the noise removal filter (median filter)\n If you don't make a choice, default value is 3x3 \n")
medFilter=input("-- FILTER VALUE-- \nMake your choice or just press Enter ---> ");
if isempty(medFilter)
medFilter=3;
else
while medFilter<3 || medFilter>243 || rem(medFilter,2)==0
fprintf('The value of the filter should be a number >3 and <243 and it has to be an ODD number. Please insert a valid value\n ')
medFilter=input("-- FILTER VALUE -- \nMake your choice! ---> ");
end
end
%User can set the minimum area to recognize an oil spill
fprintf("\nYou can filter the segmentation results by setting the value of the vastness of the area to be considered\n If you don't make a choice, default area is 45px(~450 m^2) around the main oil spill found \n")
areaToExplore=input("-- AREA TO CONSIDER -- \nMake your choice or just press Enter ---> ");
if isempty(areaToExplore)
areaToExplore=450;
else
while areaToExplore<=0 || areaToExplore>422500
fprintf('The value of the area should be a number >0 and <422500m^2 (650*650px). Please insert a valid value\n ')
areaToExplore=input("-- AREA TO EXPLORE -- \nMake your choice! ---> ");
end
end
[automatic_thresh,BWautomatic]=automatic_threshold(grayImg,groundTruth1,areaToExplore,medFilter);
figure('WindowState', 'maximized');
segmentation_evaluation(groundTruth,BWautomatic);
fprintf("\nWould you like to improve segmentation results? [y/n]")
yesOrNo=input("Press ""y"" or ""n"" --->",'s');
end
case 3 % --------------- LOCAL ADAPTIVE THRESHOLDING ----------------------- %
%defaut filter values
noiseFilter=5;
SharpThresh=0.5;
GaussianFilter=5;
[BWlocal]=local_threshold(grayImg,groundTruth1,noiseFilter,SharpThresh,GaussianFilter);
figure('WindowState', 'maximized');
segmentation_evaluation(groundTruth,BWlocal);
%User can manually improve segmentation results by changing some parameters
fprintf("\nWould you like to improve segmentation results? (Noise filter width) [y/n]")
yesOrNo=input("Press ""y"" or ""n"" --->",'s');
while strcmpi(yesOrNo,'y')
close all
%User can modify Wiener filter's window size
fprintf("\nYou can modify the segmentation results by setting a new value for the noise removal filter (Wiener filter)\n If you don't make a choice, default value is 5x5 \n")
noiseFilter=input("-- FILTER VALUE-- \nMake your choice or just press Enter ---> ");
if isempty(noiseFilter)
noiseFilter=5;
else
while noiseFilter<3 || noiseFilter>243 || rem(noiseFilter,2)==0
fprintf('The value of the filter should be a number >3 and <243 and it has to be an ODD number. Please insert a valid value\n ')
noiseFilter=input("-- FILTER VALUE -- \nMake your choice! ---> ");
end
end
%User can modify threshold of unsharping mask
fprintf("\nYou can modify the segmentation results by setting a new value for the threshold of UNSHARPING MASK\n If you don't make a choice, DEFAULT VALUE is 0.5. Values can be 0<x<1 \n")
SharpThresh=input("-- UNSHARPING MASK THRESHOLD -- \nMake your choice or just press Enter ---> ");
if isempty(SharpThresh)
SharpThresh=0.5;
else
while SharpThresh<0 || SharpThresh>1
fprintf('The value of the threshold should be a number >0 and <1. Please insert a valid value\n ')
SharpThresh=input("-- UNSHARPING MASK THRESHOLD -- \nMake your choice! ---> ");
end
end
%User can modify Gaussian filter's window size
fprintf("\nYou can modify the segmentation results by setting a new value for the GAUSSIAN FILTER\n If you don't make a choice, DEFAULT VALUE is 5*5 \n")
GaussianFilter=input("-- GAUSSIAN FILTER -- \nMake your choice or just press Enter ---> ");
if isempty(GaussianFilter)
GaussianFilter=5;
else
while GaussianFilter<3 || GaussianFilter>243 || rem(GaussianFilter,2)==0
fprintf('The value of the filter should be a number >3 and <243(MAX value) and it needs to be an ODD number. Please insert a valid value\n ')
GaussianFilter=input("-- GAUSSIAN FILTER -- \nMake your choice! ---> ");
end
end
[BWlocal]=local_threshold(grayImg,groundTruth1,noiseFilter,SharpThresh,GaussianFilter);
figure('WindowState', 'maximized');
segmentation_evaluation(groundTruth,BWlocal);
fprintf("\nWould you like to improve segmentation results? [y/n]")
yesOrNo=input("Press ""y"" or ""n"" --->",'s');
end
case 4 % --------------- SUPERPIXEL APPROACH ----------------------- %
%Default values of superpixels & minimun distance from principal blob
spNum=25000;
minDist=450;
[BWSuperpixel]=superpixel(grayImg,groundTruth1,spNum,minDist);
figure('WindowState', 'maximized');
segmentation_evaluation(groundTruth,BWSuperpixel);
%User can manually improve segmentation results by changing some parameters
fprintf("\nWould you like to improve segmentation results? (Number of superpixels/distance value from main blob) [y/n]")
yesOrNo=input("Press ""y"" or ""n"" --->",'s');
while strcmpi(yesOrNo,'y')
close all
%User can modify the number of superpixel to segment the image
fprintf("\nYou can modify the segmentation results by setting a new value for the number of superpixels or rhe distance value from the principal blob\n If you don't make a choice, default values are [25.000 superpx] & [450px] \n")
spNum=input("-- SUPERPIXELS NUMBER -- \nMake your choice or just press Enter ---> ");
if isempty(spNum)
spNum=25000;
else
while spNum<500 || spNum>812500
fprintf('The number of superpixels should be a number >=500 and <812500 (MAX value). Please insert a valid value\n ')
spNum=input("-- SUPERPIXEL NUMBER -- \nMake your choice! ---> ");
end
end
%User can change the distance value from the principal blob
fprintf("\nYou can modify the segmentation results by setting the value of the distance from the main oil spill found to show other spills\n If you don't make a choice, default values is 450m \n")
minDist=input("-- DISTANCE VALUE -- \nMake your choice or just press Enter ---> ");
if isempty(minDist)
minDist=450;
else
while minDist<=0 || minDist>650
fprintf('The distance value should be >0 and <650(max value). Please insert a valid value\n ')
minDist=input("-- DISTANCE VALUE-- \nMake your choice! ---> ");
end
end
[BWSuperpixel]=superpixel(grayImg,groundTruth1,spNum,minDist);
figure('WindowState', 'maximized');
segmentation_evaluation(groundTruth,BWSuperpixel);
fprintf("\nWould you like to improve segmentation results? [y/n]")
yesOrNo=input("Press ""y"" or ""n"" --->",'s');
end
case 5 % --------------- FUZZY LOGIC APPROACH ----------------------- %
%Default value of Lee filter's window size
filterSize=5;
[BWfuzzyEdge]=fuzzy_edgeDetect(grayImg,groundTruth1,filterSize);
figure('WindowState', 'maximized');
segmentation_evaluation(groundTruth,BWfuzzyEdge);
%User can manually improve segmentation results by changing some parameters
fprintf("\nWould you like to improve segmentation results? (Lee Filter size) [y/n]")
yesOrNo=input("Press ""y"" or ""n"" --->",'s');
while strcmpi(yesOrNo,'y')
close all
%User can cheange the Lee Filter's window size
fprintf("\nYou can modify the segmentation results by setting a new value for the Lee filter size\n If you don't make a choice, DEFAULT VALUE is 5x5 \n")
filterSize=input("-- LEE FILTER SIZE -- \nMake your choice or just press Enter ---> ");
if isempty(filterSize)
filterSize=5;
else
while filterSize<3 || filterSize>243 || rem(filterSize,2)==0
fprintf('The size of the filter should be a number >=3 and <243 (MAX value) and must be adn ODD number. Please insert a valid value\n ')
filterSize=input("-- LEE FILTER SIZE -- \nMake your choice! ---> ");
end
end
[BWfuzzyEdge]=fuzzy_edgeDetect(grayImg,groundTruth1,filterSize);
figure('WindowState', 'maximized');
segmentation_evaluation(groundTruth,BWfuzzyEdge);
fprintf("\nWould you like to improve segmentation results? [y/n]")
yesOrNo=input("Press ""y"" or ""n"" --->",'s');
end
case 6 % --------------- K-MEANS APPROACH ----------------------- %
%Default value of number of clusters and no. of blobs to extract
filterSize=2*ceil(2*0.5)+1.; % =3 Default MATLAB value. 0.5 is SIGMA =std dev of Gaussian distribution
numClusters=5;
numBlobs=1;
[kMeansSegm,BW_kMeans]=kmeansSegment(grayImg,groundTruth1,filterSize,numClusters,numBlobs);
figure('WindowState', 'maximized');
segmentation_evaluation(groundTruth,BW_kMeans);
%User can manually improve segmentation results by changing some parameters
fprintf("\nWould you like to improve segmentation results? (Gaussian filter size / Number of clusters / Number of blobs) [y/n]")
yesOrNo=input("Press ""y"" or ""n"" --->",'s');
while strcmpi(yesOrNo,'y')
close all
%User can change the Gaussian filter size
fprintf("\nYou can choose the Gaussian filter size\n If you don't make a choice, default number is 3\n")
filterSize=input("-- GAUSSIAN FILTER SIZE -- \nMake your choice or just press Enter ---> ");
if isempty(filterSize)
filterSize=2*ceil(2*0.5)+1.;
else
while filterSize<1 || rem(filterSize,2)==0
fprintf('The number of clusters should be a value >1 and an ODD number. Please insert a valid value\n ')
filterSize=input("-- NUMBER OF CLUSTERS -- \nMake your choice! ---> ");
end
end
%User can change the number of clusters
fprintf("\nYou can choose the number of clusters to divide the image\n If you don't make a choice, default number is 5\n")
numClusters=input("-- NUMBER OF CLUSTERS -- \nMake your choice or just press Enter ---> ");
if isempty(numClusters)
numClusters=5;
else
while numClusters<1
fprintf('The number of clusters should be a value >1. Please insert a valid value\n ')
numClusters=input("-- NUMBER OF CLUSTERS -- \nMake your choice! ---> ");
end
end
%User can change the number of blobs to extract
fprintf("\nYou can change the number of oil spills to extract. Default number is 1\n")
numBlobs=input("-- NUMBER OF OIL SPILLS TO EXTRACT -- \nMake your choice or just press Enter ---> ");
if isempty(numBlobs)
numBlobs=1;
else
while numBlobs<1 || numBlobs>numClusters
fprintf('The number of oil spills to extract should be a value >1 and < of the total number of clusters. Please insert a valid value \n')
numBlobs=input("-- NUMBER OF OIL SPILLS TO EXTRACT -- \nMake your choice! ---> \n");
end
end
[kMeansSegm,BW_kMeans]=kmeansSegment(grayImg,groundTruth1,filterSize,numClusters,numBlobs);
figure('WindowState', 'maximized');
segmentation_evaluation(groundTruth,BW_kMeans);
fprintf("\nWould you like to improve segmentation results? [y/n]")
yesOrNo=input("Press ""y"" or ""n"" --->",'s');
end
otherwise
fprintf("\nWRONG VALUE! Please enter another number.")
choice3=input("\nMake your choice! ---> ");
end
% If user chooses images with SEA & LAND:
elseif choice==0
fprintf("\n\nThere are different segmentation method you can use, please choose a number!\n")
fprintf("1 - AUTOMATIC THRESHOLDING SEGM.\n")
fprintf("2 - K-MEANS CLUSTERING & SEGM.\n")
choice3=input("Make your choice! ---> ");
switch choice3
case 1 % --------------- AUTOMATIC THRESHOLDING ----------------------- %
%Default values of minimum area to consider for an oil spill and threshold value to recognize land
areaToExplore=45;
landThreshold=0.5;
medFilt=5;
[oilMask,BWland]=land_mask(grayImg,groundTruth1,areaToExplore,landThreshold,medFilt);
figure('WindowState', 'maximized')
segmentation_evaluation(groundTruth,BWland);
%User can manually improve segmentation results by changing some parameters
fprintf("\nWould you like to improve segmentation results? (Blob area values/Land threshold) [y/n]")
yesOrNo=input("\nPress ""y"" or ""n"" --->",'s');
while strcmpi(yesOrNo,'y')
close all
%User can modify median filter's window size
fprintf("\nYou can modify the segmentation results by setting a new value for the noise removal filter (median filter)\n If you don't make a choice, default value is 5x5 \n")
medFilt=input("-- FILTER VALUE-- \nMake your choice or just press Enter ---> ");
if isempty(medFilt)
medFilt=5;
else
while medFilt<3 || medFilt>243 || rem(medFilt,2)==0
fprintf('The value of the filter should be a number >3 and <243 and it has to be an ODD number. Please insert a valid value\n ')
medFilt=input("-- FILTER VALUE -- \nMake your choice! ---> ");
end
end
%User can change the area of blobs to consider in the segmentation phase
fprintf("\nYou can filter the segmentation results by setting the value of the vastness of the area to be considered\n If you don't make a choice, default area is 450px(~450 m^2) around the main oil spill found \n")
areaToExplore=input("-- BLOB AREA VALUE -- \nMake your choice or just press Enter ---> ");
if isempty(areaToExplore)
areaToExplore=45;
else
while areaToExplore<=0 || areaToExplore>422500
fprintf('The value of the area should be a number >0 and <422500 m^2 (650*650px). Please insert a valid value\n ')
areaToExplore=input("-- AREA TO EXPLORE -- \nMake your choice! ---> ");
end
end
%User can change threshold value to recognize land
fprintf("\nYou can filter the LAND segmentation results by setting the value of the THRESHOLD to identify the land\n If you don't make a choice, DEFAULT VALUE is 0.5 \n")
landThreshold=input("-- LAND THRESHOLD VALUE -- \nMake your choice or just press Enter ---> ");
if isempty(landThreshold)
landThreshold=0.5;
else
while landThreshold<0 || landThreshold>1
fprintf('The value of the threshold should be a number >0 and <1. Please insert a valid value\n ')
areaToExplore=input("-- LAND THRESHOLD VALUE -- \nMake your choice! ---> ");
end
end
[landMask,BWland]=land_mask(grayImg,groundTruth1,areaToExplore,landThreshold,medFilt);
figure('WindowState', 'maximized')
segmentation_evaluation(groundTruth,BWland);
fprintf("\nWould you like to improve segmentation results? (Blob area values) [y/n]")
yesOrNo=input("Press ""y"" or ""n"" --->",'s');
end
case 2 % --------------- K-MEANS APPROACH ----------------------- %
%Default value of number of clusters and no. of blobs to extract
gaussFilt=3;
numClusters=5;
numBlobs=1;
[BW_kMeans]=kmeansSegment_for_land(grayImg,groundTruth1,gaussFilt,numClusters,numBlobs);
figure('WindowState', 'maximized')
segmentation_evaluation(groundTruth,BW_kMeans);
%User can manually improve segmentation results by changing some parameters
fprintf("\nWould you like to improve segmentation results? (Guassian filter size/Number of clusters/Number of blobs to extract) [y/n]")
yesOrNo=input("Press ""y"" or ""n"" --->",'s');
while strcmpi(yesOrNo,'y')
close all
%User can change the Gaussian filter size
fprintf("\nYou can choose the Gaussian filter size\n If you don't make a choice, default number is 3\n")
gaussFilt=input("-- GAUSSIAN FILTER SIZE -- \nMake your choice or just press Enter ---> ");
if isempty(gaussFilt)
gaussFilt=2*ceil(2*0.5)+1.; % =3
else
while gaussFilt<1 || rem(gaussFilt,2)==0
fprintf('The number of clusters should be a value >1 and an ODD number. Please insert a valid value\n ')
gaussFilt=input("-- NUMBER OF CLUSTERS -- \nMake your choice! ---> ");
end
end
%User can change the number of clusters
fprintf("\nYou can choose the number of clusters to divide the image\n If you don't make a choice, default number is 5\n")
numClusters=input("-- NUMBER OF CLUSTERS -- \nMake your choice or just press Enter ---> ");
if isempty(numClusters)
numClusters=5;
else
while numClusters==0 || numClusters==1
fprintf('The number of clusters should be a value >1 and < of the total number of clusters. Please insert a valid value\n ')
numClusters=input("-- NUMBER OF CLUSTERS -- \nMake your choice! ---> ");
end
end
%User can change number of blobs to extract
fprintf("\nYou can change the number of oil spills to extract. Default number is 1\n")
numBlobs=input("-- NUMBER OF OIL SPILLS TO EXTRACT -- \nMake your choice or just press Enter ---> ");
if isempty(numBlobs)
numBlobs=1;
else
while numBlobs<1 || numBlobs>numClusters
fprintf('The number of oil spills to extract should be a value >1. Please insert a valid value \n')
numBlobs=input("-- NUMBER OF OIL SPILLS TO EXTRACT -- \nMake your choice! ---> \n");
end
end
[BW_kMeans]=kmeansSegment_for_land(grayImg,groundTruth1,gaussFilt,numClusters,numBlobs);
figure('WindowState', 'maximized')
segmentation_evaluation(groundTruth,BW_kMeans);
fprintf("\nWould you like to improve segmentation results? (Blob area values) [y/n]")
yesOrNo=input("Press ""y"" or ""n"" --->",'s');
end
otherwise
fprintf("\nWRONG VALUE! Please enter another number.")
choice3=input("\nMake your choice! ---> ");
end
end
%% ----------- Code finished -----------
toc
end