-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind_checkers.m
117 lines (89 loc) · 2.93 KB
/
find_checkers.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
function [BW,maskedRGBImage,sq_dims] = find_checkers(RGB,Rside,show)
%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 25-May-2020
%------------------------------------------------------
if ~exist('show', 'var')
show = 0;
end
if ~exist('Rside', 'var')
Rside = [0,0];
end
% Convert RGB image to chosen color space
sz = size(RGB);
RGB1 = RGB(:,(round(sz(2)/2):end),:);
I = rgb2lab(RGB1);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 45;
channel1Max = 100;
% Define thresholds for channel 2 based on histogram settings
channel2Min = -60;
channel2Max = 70;
% Define thresholds for channel 3 based on histogram settings
channel3Min = -20;
channel3Max = 70;
% 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;
BW = imclose(BW, strel('disk',3));
BW = bwareaopen(BW,100);
BWw = bwareaopen(BW,1500);
BW = xor(BW, BWw);
reg_props = regionprops(BW,'BoundingBox','PixelIdxList', 'Area');
BB = [];
for i = 1:length(reg_props)
bb = reg_props(i).BoundingBox;
BB = [BB;bb];
end
Area = ([reg_props.Area])';
[N,edges] = histcounts(BB(:,1));
[~,mx] = max(N);
try
lim = [edges(mx-1),edges(mx+1)];
catch
lim = [edges(mx),edges(mx+2)];
end
fx2 = find((abs(BB(:,3)-BB(:,4))<10)&...
((BB(:,1)>lim(1))&(BB(:,1)<lim(2)))); % bounding box size and location
[N1,edges1] = histcounts(Area);
[~,mx1] = max(N1);
try
lim1 = [edges1(mx1-1),edges1(mx1+1)];
catch
lim1 = [edges1(mx1),edges1(mx1+2)];
end
fx3 = find(Area>lim1(1) & Area<lim1(2)); % Area
fx = intersect(fx2,fx3);
nBB = BB(fx,:);
h = levcook(nBB(:,1),nBB(:,2));
out_idx = find(h>2.5*std(h));
fx(out_idx) = [];
nBB = BB(fx,:);
reg_props1 = reg_props(fx);
if show == 1
figure(20);
clf;
imshow(RGB1); hold on;
end
BBw = false(size(BW));
for ii = 1:length(fx)
if show == 1
rectangle('Position',nBB(ii,:),'EdgeColor','g','LineWidth',2)
hold on;
end
BBw(reg_props1(ii).PixelIdxList) = true;
end
sq_dims = median(nBB(:,3:4));
BW = true(sz(1:2));
BW(:,(round(sz(2)/2):end),:) = not(imdilate(BBw,strel('disk',3)));
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end