-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSobel.m
67 lines (48 loc) · 1.3 KB
/
Sobel.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
function [res] = Sobel( frame )
%make grayscale image
gray = rgb2gray(frame);
%make shapes for dilation
SEvert = strel('line', 3, 90);
SEhor = strel('line', 5, 0);
SEd = strel('diamond', 3);
%sobel edge detection
ed = edge(gray, 'sobel');
%vertical dilation
dil = imdilate(ed, SEvert);
%horizontal dilation
dil2 = imdilate(dil, SEhor);
%remove objects smaller than 100 pixels
dil2 = bwareaopen(dil2, 100);
%clear the border
dil2 = imclearborder(dil2);
%closing
dil2 = double(bclosing(dil2, 2));
%fill in the holes
fill = imfill(dil2, 'holes');
%clear border
er2 = imclearborder(fill);
%remove objects smaller than 2500 pixels
er2 = bwareaopen(er2, 2500);
%label objects
c = label(er2);
%Measure perimeter of objects
per = measure(c,gray,{'Perimeter'},[],Inf,0,0);
%Meausure area of objects
area = measure(c,gray,{'size'},[],Inf,0,0);
%Make a blank image
res = gray&0;
%loop through the data
n = length(per);
for i=1:1:n
%If the perimeter is below the threshold
if(double(per(i))/double(area(i)) < 0.2)
mm = gray&0 | c == i;
bb = getfield(regionprops(double(mm), 'BoundingBox'), 'BoundingBox');
if(double(area(i))/ (bb(2)*bb(3)) < 0.85)
%Add the object to te result
res = res | c == i;
end
end
end
res = imdilate(double(res), SEd);
end