-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedgedetection.m
More file actions
130 lines (109 loc) · 3.02 KB
/
edgedetection.m
File metadata and controls
130 lines (109 loc) · 3.02 KB
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
118
119
120
121
122
123
124
125
126
127
128
%% Scan The Input image
inp = imread('callender.jpg');
inp2 = imread('calender2.png');
inp_bw = rgb2gray(inp);
%% Trying Different filters on the image.
% Laplacian
h = fspecial('laplacian');
filteredout = imfilter(inp_bw,h);
imshow(filteredout);
% Sobel
[~, threshold] = edge(inp_bw, 'sobel');
BWs = edge(inp_bw,'sobel', threshold*0.5);
imshow(BWs);
% Canny edge
filteredout = inp_bw;
canny = edge(filteredout,'canny');
imshow(canny)
% sobel + Laplacian
h = fspecial('laplacian')';
a = imfilter(inp_bw,h);
b = imbinarize(a);
c = edge(b,'sobel');
imshow(c)
% Hough transform on laplacian + sobel
h = fspecial('laplacian');
a = imfilter(inp_bw,h');
b = imbinarize(a);
imshow(b)
BW = edge(b,'sobel');
imshow(BW)
[H,T,R] = hough(BWs);
imshow(H,[],'XData',T,'YData',R,...
'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
P = houghpeaks(H,100,'threshold',ceil(0.0000001*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
y = y(x==-90|x==0);
x = x(x==-90|x==0);
plot(x,y,'s','color','white');
lines = houghlines(BW,T,R,P,'FillGap',1,'MinLength',1);
figure, imshow(inp), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
%
% Hough transform on sobel + sobel
h = fspecial('laplacian');
a = imfilter(inp_bw,h');
b = imbinarize(a);
[~, threshold] = edge(inp_bw, 'sobel');
BWs = edge(inp_bw,'sobel', threshold*0.5);
imshow(BWs)
BW = edge(BWs,'sobel');
imshow(BW)
[H,T,R] = hough(BW);
imshow(H,[],'XData',T,'YData',R,...
'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
P = houghpeaks(H,60,'threshold',ceil(0.001*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
y = y(x==-90|x==0);
x = x(x==-90|x==0);
plot(x,y,'s','color','white');
lines = houghlines(BW,T,R,P,'FillGap',2,'MinLength',5);
figure, imshow(inp), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
%
%
binarizedout = imbinarize(filteredout);
labels = bwlabel(binarizedout);
% labels = bwlabel(imfilter(rgb2gray(imread('callender.jpg')),h));
[r,c] = size(labels);
out = zeros(r,c);
for i=1:r
for j=1:c
if(labels(i,j)==0)
out(i,j) = 1;
else
out(i,j) =0;
end
end
end
ocr_out = ocr(inp);