-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmanual_thresholding.m
105 lines (76 loc) · 3.19 KB
/
manual_thresholding.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
function [BW,max_value] = manual_thresholding(img,ground,addValue,maxArea,medianFilter)
tic
%% Image Smoothing operation with median filter:
grayImg=medfilt2(img,[medianFilter medianFilter]);
%figure,
%subplot(1,4,[1 2]), imshow(img), title('Original Image')
%subplot(1,4,[3 4]),imshow(grayImg), title('Median filter smoothed image')
%% Contrast enhancement (Histogram equalization):
eqImg=histeq(grayImg,50);
%% Gray and equalized image histograms comparison:
%figure
%subplot(1,2,1), imhist(grayImg),title('Original Gray Image Histogram ')
%subplot(1,2,2), imhist(eqImg), title('Equalized Image Histogram')
%% MANUAL THRESHOLDING:
%Manually choose pixels to use for thresholding:
figure('WindowState', 'maximized')
pixel_values=impixel(eqImg);
%Computation of min and max values for the chosen pixels
min_value=min(pixel_values(:,1,1));
max_value=max(pixel_values(:,1,1));
if (min_value==0 && max_value==0) || (min_value==max_value)
max_value=min_value+addValue;
elseif max_value~=0
max_value=max_value+addValue;
end
%Binary mask creation:
% mask=imbinarize(eqImg,[min_value:max_value]);
%mask= ~mask;
mask=eqImg>=min_value & eqImg<=max_value;
%figure, imshow(mask),title('Maschera dopo manual thresholding')
%% Opening & Closing morphological operations to eliminate isolated white dots:
se=strel('disk',1);
mask= imopen(mask,se);
%mask=imclose(mask,se);
%figure, imshow(mask), title('Maschera dopo Opening & Closing')
%% "imfill" to fill blank areas
filledMask = imfill(mask, 'holes');
%figure, imshow(binaryImage), title('Maschera dopo imfill')
%% SPOT FEATURES EXTRACTIONS:
%Label connected components in 2-D binary image (useful to use regionprops later)
[labeledImage, ~] = bwlabel(filledMask, 4);
% 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 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=normalize(vertcat(stats.StandardDeviation),'range')*100;
% figure
% bar(1:numberOfBlobs,norm)
% xlabel('Region Label Number')
% ylabel('Standard Deviation')
%Find blobs with an area >45pixels^2 ~450m^2 in real world
idx=find([stats.Area]>maxArea);
BW=ismember(labelmatrix(cc),idx);
%figure, imshow(BW), title('maschera finale tenendo conto delle AREE')
%% Mask coloring and overlay on the original image
visualizeImages(img,BW,ground,'MANUAL THRESHOLDING');
toc
end