/
lineDetectorCartesianImp.m
85 lines (65 loc) · 1.98 KB
/
lineDetectorCartesianImp.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
% LINEDETECTORCARTESIANIMP.m - Line detector in Cartesian coordinate system
%
% This code implements a line detector using Cartesian coordinate system.
% This improved version works for all cases.
% It iterates over each pixel and determine all its
% prospective lines. The final line will be the one found in the
% accumulator with the maximum value.
%
% You can do with this code whatever you want. The main purpose is help
% people learning about this. Also, there is no warranty of any kind.
%
% Juan Miguel Valverde Martinez
% http://laid.delanover.com
clear;clc;
inputimage = double(rgb2gray(imread('edges2.png')));
%image size
[rows,columns]=size(inputimage);
%accumulator
acc1=zeros(rows,181);
acc2=zeros(columns,181);
% Walk through the image
for x=1:columns
for y=1:rows
if(inputimage(y,x)==0)
for m=-45:135
% change the coordinates system
yInv = rows-y;
b=round(yInv-tan((m*pi)/180)*x);
if(b<rows && b>0)
acc1(b,m+45+1)= acc1(b,m+45+1)+1;
end
% intersection in x-axis
b=round(x-yInv/tan((m*pi)/180));
if(b<columns && b>0)
acc2(b,m+45+1)= acc2(b,m+45+1)+1;
end
end
end
end
end
% Calculate the maxima to draw the line whose characteristics are found in
% the coordinates of that maxima
j1=max(max(acc1));
j2=max(max(acc2));
if j1>j2
[d,e]=find(acc1==j1);
e = e(round(size(e,1)/2));
d = d(round(size(d,1)/2));
e = (e-46);
point = round([rows-d 0]);
else
[d,e]=find(acc2==j2);
e = e(round(size(e,1)/2));
d = d(round(size(d,1)/2));
e = (e-46);
point = round([rows d]);
end
figure;imshow(inputimage);
hold on;
% Drawing the line
theta = e;
x = [0:.1:1000];
m = tan(theta*pi/180);
y = -m*(x-point(2))+point(1);
plot(x,y);