-
Notifications
You must be signed in to change notification settings - Fork 8
/
kmeansSegment_for_land.m
65 lines (43 loc) · 1.96 KB
/
kmeansSegment_for_land.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
function [BW]=kmeansSegment_for_land(img,ground,gaussFilt,numClusters,numBlobs)
tic
%% IMAGE ENHANCEMENT
grayImg=imgaussfilt(img,'FilterSize',gaussFilt);
%% K-MEANS CLUSTERING
[rows, columns] = size(grayImg);
%experimentally has been seen that the best results can be obtained with 5 clusters
numberOfClusters = numClusters;
% Convert to column vector
grayLevels = double(grayImg(:));
%kmeans MATLAB function is used
[clusterIndexes, clusterCenters] = kmeans(grayLevels, numberOfClusters,'Replicates', 5,...
'MaxIter',350);
labeledImage = reshape(clusterIndexes, rows, columns);
%Every blob has a different color to visually show distinct blobs
caption = sprintf('k-means with %d clusters', numberOfClusters);
figure('WindowState', 'maximized');
imshow(label2rgb (labeledImage, 'hsv', 'k', 'shuffle'),[]),title(caption);
%% OIL SPILL DETECTION PHASE:
% We will assume the oil spill is the darkest class (between the clusters)
% The cluster center will be the mean gray level of the class.. so we now try to find the darkest class
[minValue, indexOfMinValue] = min(clusterCenters);
%We also assume that the land is the brightest class
[maxValue,indexOfMaxValue] = max(clusterCenters);
% Get pixels that are labeled as the oil spill and as the land
oilSpill = labeledImage == indexOfMinValue;
land = labeledImage == indexOfMaxValue;
% Extract the largest blobs;
blobsToExtract=numBlobs;
oilSpill=bwareafilt(oilSpill, blobsToExtract);
land=bwareafilt(land, blobsToExtract);
% Fill holes.
oilSpill=imfill(oilSpill, 'holes');
land=imfill(land, 'holes');
%% Mask coloring and overlay on the original image
background=zeros(size(img),'double');
mascheraSpill=imoverlay(background,oilSpill,'cyan');
%Call to function that shows mask and images
visualizeImages_for_land(img,land,ground,mascheraSpill,'K-MEANS CLUSTERING OIL + LAND SEGMENTATION');
%Used for the segmentation_evaluation function
BW=or(oilSpill,land);
toc
end