-
Notifications
You must be signed in to change notification settings - Fork 8
/
automatic_threshold_for_land.m
108 lines (82 loc) · 3.11 KB
/
automatic_threshold_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
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
function [maschera,BW] = automatic_threshold_for_land(img,ground,areaToExplore,medFilt)
tic
%% Image Smoothing operation with median filter:
grayImg=medfilt2(img,[medFilt medFilt]);
%figure, imshow(grayImg), title('Original image')
%% Aumento contrasto (Histogram equalization):
eqImg=histeq(grayImg);
%% Gray and equalized image histograms comparison:
% subplot(1,2,1), imhist(grayImg)
% title('Istogramma Gray Image')
% subplot(1,2,2), imhist(eqImg);
% title('Istogramma immagine Equalizzata')
%% AUTOMATIC THRESHOLDING:
[counts,binLocations]=imhist(eqImg);
A=[counts,binLocations];
%% Finding two threshold values:
valueMax= max(max([counts,binLocations]));
valueMin= min(min([counts,binLocations]));
[row column]= find(A==valueMax,1,'first');
%Maximum threshold value:
Tmax=A(row,2);
[r c]= find(A==valueMin,1,'first');
%Minimum threshold value:
Tmin=A(r,2);
%If Tmin=0 means that there are no pixel with that value, so you have to use only Tmax
if Tmin==0
mask=imbinarize(eqImg,Tmax);
else
mask=imbinarize(eqImg,[Tmin:Tmax]);
end
mask=~mask;
%% Opening & Closing morphological operations to eliminate isolated white dots:
se=strel('disk',2);
openedImg= imopen(mask,se);
closedImg= imclose(openedImg,se);
%figure, imshow(mask), title('Maschera dopo Opening')
%% "imfill" to fill blank areas
filledMask = imfill(closedImg, 'holes');
%% SPOT FEATURES EXTRACTIONS:
[labeledImage, numberOfBlobs] = bwlabel(filledMask, 8);
% figure
% imshow(labeledImage, []); % Show the gray scale image.
% title('Labeled Image, from bwlabel()');
% Let's assign each blob a different color to visually show the user the distinct blobs.
%coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
%figure, imshow(coloredLabels);
% Get all the blob properties.
cc=bwconncomp(filledMask);
stats = regionprops(labeledImage, img, 'all');
%Computation of std deviation:
% for k = 1:numberOfBlobs
% stats(k).StandardDeviation = std(double(stats(k).PixelValues));
% text(stats(k).Centroid(1),stats(k).Centroid(2), ...
% sprintf('%2.1f', stats(k).StandardDeviation), ...
% 'EdgeColor','b','Color','r');
% end
%statsStd = [stats.StandardDeviation];
% lowStd = find(statsStd >11 && statsStd<42.91);
% for k = 1:length(lowStd)
% rectangle('Position',s(lowStd(k)).BoundingBox,'EdgeColor','y');
% end
%idx=find( statsStd >11 & statsStd<42.91);
% norm=rescale(vertcat(stats.StandardDeviation),0,100);
% figure
% bar(1:numberOfBlobs,norm)
% xlabel('Region Label Number')
% ylabel('Standard Deviation')
%GRAFICO AREA
% figure
% bar(1:numberOfBlobs,[stats.Area])
% xlabel('Region Label Number')
% ylabel('Area')
%Find blobs with an area >45pixels ~450m^2 in real world
%Default value: areaToExplore=45000;
idx=find([stats.Area]>areaToExplore); %| norm >=10 & norm<32.91);
BW=ismember(labelmatrix(cc),idx);
%figure, imshow(BW), title('maschera finale tenendo conto delle AREE')
%% Mask coloring and overlay on the original image
background=zeros(size(img),'double');
maschera=imoverlay(background,BW,'cyan');
toc
end