-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbb_2.m
90 lines (68 loc) · 2.38 KB
/
bb_2.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
function [BW1,maskedRGBImage,bb] = bb_2(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 11-Jun-2020
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2lab(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0;
channel1Max = 60;
% Define thresholds for channel 2 based on histogram settings
channel2Min = -30.0;
channel2Max = 60.0;
% Define thresholds for channel 3 based on histogram settings
figure(22)
hist = histogram(I(:,:,3));
histo = [hist.BinEdges',[hist.Values';1]];
close(figure(22))
TF = find(islocalmin(histo(:,2)));
Tff = histo(TF,:);
range = [-30,0];
tf = and(Tff(:,1)>range(1), Tff(:,1)<range(2));
ttz = min(Tff(tf,2));
Ttz = Tff(find(Tff(:,2)==ttz),:);
channel3Min = -60.0;
try
channel3Max = Ttz(1);
catch
channel3Max = -20.0;
end
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
sz = size(BW);
if sz(1)/sz(2)>1.7
rr = round([0.22,0.79].*sz(2));
else
rr = round([0.18,0.85].*sz(2));
end
BW1 = zeros(sz);
BW1(:,rr(1):rr(2)) = BW(:,rr(1):rr(2));
BW = bwareaopen(BW1,100);
BW = imclose(BW,strel('disk',3));
BW1 = bwareafilt(BW,4);
BW2 = bwareaopen(BW1,500);
BBW = bwconvhull(BW2);
stats = regionprops(BBW,'BoundingBox');
bb = stats.BoundingBox;
BWzz = zeros(sz);
if sz(1)/sz(2)>1.7
bb1 = round([bb(1)*0.5,bb(3)*1.4]);
bb2 = round([0.045*sz(1),0.98*sz(1)]);
BWzz(bb2(1):bb2(2),bb1(1):(bb1(1)+bb1(2))) = 1;
else
bb1 = round([bb(1)*0.6,bb(3)*1.3]);
BWzz(:,bb1(1):(bb1(1)+bb1(2))) = 1;
end
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BWzz,[1 1 3])) = 0;
maskedRGBImage(repmat(BW,[1 1 3])) = 0;
end