Professional Documents
Culture Documents
On The Farey Sequence and Its Augmentation For Applications To Image Analysis
On The Farey Sequence and Its Augmentation For Applications To Image Analysis
3, 637–658
DOI: 10.1515/amcs-2017-0045
a
Department of Computer Science and Engineering
National Institute of Technology Meghalaya, Bijni Complex, Shillong, India
e-mail: sanjoy.pratihar@gmail.com
b
Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur, India
We introduce a novel concept of the augmented Farey table (AFT). Its purpose is to store the ranks of fractions of a Farey
sequence in an efficient manner so as to return the rank of any query fraction in constant time. As a result, computations
on the digital plane can be crafted down to simple integer operations; for example, the tasks like determining the extent of
collinearity of integer points or of parallelism of straight lines—often required to solve many image-analytic problems—
can be made fast and efficient through an appropriate AFT-based tool. We derive certain interesting characterizations of an
AFT for its efficient generation. We also show how, for a fraction not present in a Farey sequence, the rank of the nearest
fraction in that sequence can efficiently be obtained by the regula falsi method from the AFT concerned. To assert its merit,
we show its use in two applications—one in polygonal approximation of digital curves and the other in skew correction of
engineering drawings in document images. Experimental results indicate the potential of the AFT in such image-analytic
applications.
Keywords: Farey sequence, Farey table, fraction rank, theory of fractions, image analysis.
number of fractions less than or equal to x in Fn , i.e., (ii) When a fraction has a large denominator and hence
rn (x) = |{y : (y ≤ x) ∧ (y ∈ Fn )}|. Given the order n, does not belong to an AFT of a lower order, the
the rank problem is to find the rank of a given fraction in rank of its nearest fraction in that AFT can serve
Fn , whereas the order statistic problem deals with finding the requisite purpose in a computation involving
the fraction in Fn for some given rank. fraction ranks. For this purpose, in Section 3, we
In this paper, we introduce some elementary derive certain theoretical results that aid designing an
properties of the Farey sequence, which are particularly efficient technique for searching in an AFT for the
useful for digital-geometric techniques related to image closest fraction against any query fraction.
analysis. Based on these properties, we propose the
(iii) To emphasize the applicability of the AFT, we
concept of an augmented Farey table (AFT), from which
consider two well-known problems related to digital
the rank of a fraction in an augmented Farey sequence
image analysis. One is polygonal approximation
can be obtained by single memory access only. This,
of digital curves (Section 4), and the other is
in turn, provides definite advantages of using the AFT as a
skew correction of document images containing
tool while performing fraction-related computations in an
engineering drawings (Section 5). We show how an
image-analytic application.
AFT can be used while determining the approximate
An AFT is readily implementable as a 2D array
collinearity of a sequence of 2D points having
whose rows are indexed by numerators and columns by
integer coordinates; the procedure involves only
denominators, and whose array entries store the fraction
addition, comparison, and memory access, but no
ranks. It becomes a natural choice in our applications,
multiplication or division, thus reducing the overall
since the best-known algorithm can compute the rank of
runtime.
a fraction in no sooner than O(n2/3 log1/3 n) of time
(Pawlewicz and Pătraşcu, 2009). Using the AFT, the In essence, we show how the computation involved in
fraction ranks are fetched almost instantaneously as they the related algorithms can be crafted down to operations
are queried very frequently in different procedural steps, based on fraction ranks that are accessible from an AFT
such as comparing the slopes of two line segments with in real time. For the applications shown in the subsequent
integer endpoints, checking the collinearity of three or sections, extraction of straight edges is an important
more integer points, determining the type of turn (left or preprocessing task. For this purpose, we use the results of
right) for three non-collinear integer points, etc. (cf. Das Bhowmick and Bhattacharya (2007) when the input image
et al., 2010; Pratihar and Bhowmick, 2010; 2011). is a binary curve (Section 4), and Pratihar and Bhowmick
The motivation for our work on AFT owes to its (2009) when it is a gray-scale image (Section 5).
particular advantage as a precomputed look-up table to
support an application involving straight edge information 2. Farey sequence: Generation,
and running on a large image dataset. Let us clarify augmentation, representation
at this early point its typical applicability through a
simple example. Given two fractions ab ∈ Fn and dc ∈ Fn , their mediant is
Given three integer points (i.e., pixels)
p1 i1 , j1 , p2 i2 , j2 , and p3 i3 , j3 in succession, the defined as the fraction a+c
b+d , which always lies between
a c
metric usedto decide on the deviation of p2 from p1 p3 is b and d (Graham et al., 1994). Based on this unique
given by Δ p1 , p2 , p3 /d∞ (p1 , p3 ), where property, the sequence Fn can be computed from the
d∞ (p1 , p3 ) = sequence Fn−1 by inserting in Fn the mediant of any two
max |i1 −i3 |, |j1 −j3 | and Δ p1 , p2 , p3 denotes the area
of the triangle with vertices p1 , p2 , p3 . This is a commonly consecutive fractions of Fn−1 unless the denominator of
followed practice (Bhowmick and Bhattacharya, 2007; the mediant does not exceed n. To recursively generate Fn
Wall and Danielsson, 1984), which requires several from Fn−1 based on mediants, it is required to check all
pairs of adjacent fractions in Fn−1 . This calls for Θ((n −
multiplications for computing the value of Δ p1 , p2 , p3 .
Using an AFT provides a simpler and faster way to reach 1)2 ) steps, since Fn contains fn = π32 n2 + O(n log n) =
the solution by keeping away such multiplications, as Θ(n2 ) fractions (Hardy and Wright, 1968). Hence, the
shown in Section 4. time complexity to generate Fn becomes Θ(n3 ).
Our contributions are as follows:
2.1. Generation of Fn . The mediant-based technique
(i) We show how a Farey sequence can be appropriately for generation of Fn , as explained above, is not optimal in
augmented for effectively using it in different runtime. As mentioned by Pătraşcu and Pătraşcu (2004)
applications. Theoretical results related to this are or Routledge (2008), and also by Graham et al. (1994),
given in Sections 2.1 and 2.2. Further, as the existing (Problem 4-61), the fractions of Fn can be generated using
algorithms for constructing a Farey sequence cannot their adjacency relation in O(n2 ) iterations. We state this
readily be adapted for AFT generation, we propose here in Theorem 1, which is proved using the following
an optimal-time algorithm for this in Section 2.3. lemma.
On the Farey sequence and its augmentation for applications to image analysis 639
Numerator
Proof. Since gcd(a, b) = 1, there exist two integers u and 1 24 23 22 19 13 7 4 3 2
v such that bu−av = 1 (Hardy and Wright, 1968). So, if dc 0 25 25 25 25 – 1 1 1 1
lies next to ab in Fn , then c = u+ka and d = v +kb, since -1 26 27 28 31 37 43 46 47 48
bc − ad = 1 (Graham et al., 1994). Also, the difference -2 28 29 31 34 37 40 43 45 46
-3 30 31 33 35 37 39 41 43 44
between these two fractions is w = dc − ab = bc−ad
bd
1
= bd . -4 31 32 34 36 37 38 40 42 43
Clearly, w is minimum when d is maximum. As d ≤ n,
a > 0, b 0, a −b a > 0, b 0, a b
we get v + kb ≤ n, or, k = n−v b , which completes the
proof. 4
2 4
4
1 4
2 4
: : 40 : 42
1 4
−3 −1 −2 −1 0 1 1 3 1 4
Given any fraction ∈ Fn , the above lemma says
a :
−1 −4 19 18 16 14 13 12 10 8 7 1
: 4
b 3 3 3 3
−2 −1 1 2
about the existence of the next fraction in Fn , but it does 17 15 11 9
a 0, b < 0, a −b
3 3
a 0, b > 0, a b
20 6
not provide an efficient way of solving it. Consequently, −4 4
−1 −1
a 0, b > 0, −a b
26 47 −1 48
its corresponding simple fraction, i.e., a/g
b/g , where g =
−4 −1
−3
27
3
4
−1 −1
gcd(a, b).) : −2
−2 −4 28 −2 29 45 −2 46 2
: −2
4
−3 3
e kc − a
= , (2)
f kd − b a < 0, b 0, −a −b a < 0, b 0, −a b
n+b
where k = . Fig. 1. Geometric interpretation (bottom) of the complete AFT
d (1) (2) (3) (4)
of order 4 (top), i.e., F4 := F4 ∪ F4 ∪ F4 ∪ F4 ,
Proof. As ab , dc are two adjacent fractions in Fn , we get a
containing the ranks of all fractions in F 4 := { b : |a| ≤
cb − ad = 1, or, d(−a) − c(−b) = 1. Thus,by Lemma 1, 4 ∧ |b| ≤ 4}. Notice the counter-clockwise increase in
the fraction next to dc is kc−a
kd−b , where k = n+b
d , which ranks of the fractions in F4 .
is our assertion.
Using Theorem 1, the Farey sequence Fn can be 2.2. Augmentation of Fn . As the Farey sequence Fn
generated, starting with 01 , n1 , in Θ(n2 ) time. However, is restricted only to simple and proper fractions, it needs
to speed up the computation, we use the ranks of the an appropriate augmentation for geometric applications.
leading fractions in Fn to obtain those of the trailing The rationale is as follows. Let pq be a straight line
fractions. To this end, we define the lower half of Fn by segment with integer endpoints p = (xp , yp ) and q =
FnL = Fn ∩[0, 12 ), and the upper half by FnU = Fn ∩( 12 , 1], max{|x −x |,|y −y |}
(xq , yq ). Also, let gcd(|xqq−xpp|,|yqq−ypp|) ≤ n. Then
so that Fn = FnL ∪ { 12 } ∪ FnU . Now we have the following
the slope of pq equals some fraction in Fn if and only
theorem.
if 0 ≤ yq − yp ≤ xq − xp ≤ n. In other words,
Theorem 2. (Complementary rank) If x ∈ Fn, then Fn corresponds to just one half of the first quadrant, as
rn (1 − x) = fn + 1 − rn (x), where fn = rn 11 = shown in Fig. 1. Hence, to overcome this limitation, we
2rn 12 − 1. augment Fn to F n , so that it includes compound fractions
and proper/improper fractions with negative numerators
Proof. Observe that x ∈ FnL if and only if y := 1 − x ∈ or/and denominators. We call F n the augmented Farey
1Fn → Fn is a bijection. Hence,
FnU , which implies that L U
sequence.
2 − x = y − 2 , or rn 2 − rn (x) = rn (y) − rn 2 , or
1 1 1
ranks of the smallest element (i.e., 0/1 and its equivalent To tackle this situation, we find its closest fraction ab in
fractions) and the next element (i.e., 1/n) are generated Fn , and use the rank of ab in subsequent applications. We
by the first for loop. Then, all the fractions in [0, 12 ] are call this rank the closest rank corresponding to ab .
generated by the repeat-until loop. Fractions in [ 12 , 1] are To search for the closest fraction ab , we use a range
generated by the subsequent for loop; cells are highlighted (i.e., subsequence) of fractions, namely [ ab11 , ab22 ] ⊂ Fn ,
in dark gray in Fig. 2). The last for loop generates the to speed up the process. The following theorem provides
ranks of the fractions greater than 1. this range.
On the Farey sequence and its augmentation for applications to image analysis 641
33 31 29 27 25 23 21 19
1 8 6 4 3 7 5 4
1 9 7 5 4 10 8 7
0 1 1 1 1 3 3 3
1 9 7 5 4 10 8 7
1 3 5 7 9 11 13 15 1 17
2
2 4 6 8 10 12 14 16
1 1 1 2 2 1 2 4
10 8 6 9 7 3 5 9
9 7 5 7 5 2 3 5
10 8 6 9 7 3 5 9
32 30 28 26 24 22 20 18
Denominator
0 1 2 3 4 5 6 7 8 9 10
0 − 1 1 1 1 1 1 1 1 1 1
1 65 33 17 12 9 7 6 5 4 3 2
2 65 49 33 22 17 14 12 10 9 8 7
3 65 54 44 33 25 20 17 15 13 12 11
Numerator
4 65 57 49 41 33 27 22 19 17 16 14
5 65 59 52 46 39 33 28 24 21 18 17
6 65 60 54 49 44 38 33 29 25 22 20
7 65 61 56 51 47 42 37 33 30 26 23
8 65 62 57 53 49 45 41 36 33 31 27
9 65 63 58 54 50 48 44 40 35 33 32
10 65 64 59 55 52 49 46 43 39 34 33
(1)
Fig. 2. Generation of F10 using Algorithm 1.
an/b
Theorem 4. (Fraction search) For any proper fraction ab , (ii) We have a1
b1 ≤ n ≤ a2
b2 , which implies a1
b1 ≤ a
b ≤
a2
let b2 .
gcd(an/b, n) = g1 ,
gcd(an/b, n) = g2 , We use the result of Theorem 4 later in Algorithm 2.
an/b n In particular, we use the following corollary, which
a1 = , b1 = , follows from the two statements of Theorem 4 when taken
g1 g1
together.
an/b n
a2 = , b2 = , Corollary 1. We have that ab ∈ Fn if ab11 = ab22 . However,
g2 g2
if ab11 < ab22 , then ab may or may not belong to Fn .
such that Also,
gcd(a1 , b1 ) = gcd(a2 , b2 ) = 1. a1 a2 an/b an/b
< ⇔ < ,
b1 b2 n n
Then, for
and hence the following corollary:
a1 an/b a2 an/b
:= , := , Corollary 2. If ab11 < a2
b2 , then a1
b1 = m
n and a2
b2 = m+1
n ,
b1 n b2 n where m = an
b .
the following statements are true: If we search for the closest fraction of ab in [ ab11 , ab22 ]
a1 a2 using binary search, it may take O(log n) time in the worst
(i) ∈ Fn , ∈ Fn . case. The following theorem accounts for this.
b1 b2
a1 a a2 Theorem 5. (Search range) Fn contains at most O(n)
(ii) ≤ ≤ .
b1 b b2 fractions in the range [ ab11 , ab22 ], where a1 , b1 , a2 , b2 are de-
Proof. fined as in Theorem 4.
(i) As 0 ≤ a ≤ b ≤ n, we have 0 ≤ an/b ≤ n and Proof. From Corollary 2, ab11 = m a2
n and b2 =
m+1
n
0 ≤ an/b ≤ n. This gives ab11 ∈ Fn and ab22 ∈ Fn . a1 a2 c
when b1 < b2 . Let, for a given positive integer d, d be a
642 S. Pratihar and P. Bhowmick
0.0046
fraction of Fn , if possible, lying between (and excluding)
m m+1
fraction dierence
n and n . Observe that d < n. We now show that
0.0006
no other fraction of Fn with denominator d can exist in
[ ab11 , ab22 ].
-0.0034
922
927
932
937
942
947
with denominator d and a numerator less than c lies
outside [ ab11 , ab22 ]. Similarly, any fraction with denominator rank
fraction dierence
Thus, for every integer d in [1, n − 1], at most one -0.0053
fraction of Fn can be there in [ ab11 , ab22 ]. Therefore, there
can be at most n − 1 fractions of Fn lying in [ ab11 , ab22 ].
-0.0113
782
787
792
797
802
807
performance of binary search, we consider various cases
rank
for searching for some key fractions in Farey tables of
small orders. For this, we define the fraction difference (b)
as the difference of a fraction dc of Fn from the key 0.005
fraction dierence
2096
2101
2106
2111
2116
2121
2126
from the key fraction ab .
rank
These differences would be monotonically
decreasing with the fraction ranks increasing from (c)
r1 to r2 . Further, they are initially positive and gradually Fig. 3. Instances of various cases for searching for fractions in
become negative, with a zero-crossing somewhere in lower order Farey tables using Algorithm 2: searching
between. Clearly, the closest fraction of ab in Fn is for a/b = 78/145 in F75 , the closest fraction in F75 =
the fraction lying nearest to this zero-crossing, which 7/13, rank= 935 (a), searching for a/b = 375/448 in
needs O(log n) iterations for searching using binary F55 , the closest fraction in F55 = 36/43, rank= 787(b),
search. In fact, for instances when the zero-crossing lies searching for a/b = 1957/2788 in F99 , the closest frac-
near one of the boundaries (r1 or r2 ), the binary search tion in F99 = 40/57, rank= 2108 (c). The black solid
algorithm really takes O(log n) iterations (Fig. 3(b)). lines indicate zero-crossing.
Hence, the number of bit operations is given by
O(log n loglog 3 n) ≈ O(log2.585 n). In our algorithm F IND -C LOSEST-R ANK
(Algorithm 2), after each iteration, we get two closer
3.2. Algorithm using regula falsi. The nature of the ranks between which the zero-crossing lies. The iteration
variation in the fraction difference is grossly linear with continues depending on how close the ranks are, and
the fraction rank. This is evident from three different plots it stops when the two ranks have unit a difference.
given in Fig. 3. Thus, as a better alternative, we use an The initialized search range is [a1 /b1 , a2 /b2 ], where
iterative approach based on the regula falsi method to find rn (a1 /b1 ) = r1 and rn (a2 /b2 ) = r2 . The fractions
the zero-crossing. The rationale is that the regula falsi a1 /b1 and a2 /b2 are computed at the beginning of the
method provides the exact solution for a linear function algorithm (Algorithm 2). By Theorem 4, the query
in constant time. For a nonlinear function, it provides an fraction a/b belongs to the range given by [a1 /b1 , a2 /b2 ].
approximate solution that can be successively improved We have now the following three cases.
by iteration. Further, if a function is not exactly but
grossly linear, then it provides an approximate solution Case 1 (r1 = r2 ). The query fraction belongs to Fn ,
very fast, and the number of iterations depends on the and it is equal to a1 /b1 (Corollary 1). Therefore, the
grossness of linearity. algorithm returns the rank of a1 /b1 .
On the Farey sequence and its augmentation for applications to image analysis 643
#iteration
Output: Rank of closest fraction of a
b in Fn
6
g1 ← gcd(an/b, n), g2 ← gcd(an/b, n)
a1 ← an/b/g1 , b1 ← n/g1 4
a2 ← an/b/g2 , b2 ← n/g2
(1) (1) 2
r1 ← Fn [a1 ][b1 ], r2 ← Fn [a2 ][b2 ]
10
100
200
300
400
500
600
700
800
900
1000
if r1 = r2 then
(1) order (n)
return Fn [a1 ][b1 ] Corollary 1
if r2 − r1 = 1 then Fig. 4. Comparison between F IND -C LOSEST-R ANK and binary
if (a/b − a1 /b1 ) < (a2 /b2 − a/b) then search. The average number of iterations (rounded off to
(1)
return Fn [a1 ][b1 ] integers) is plotted vs. n.
else
(1)
return Fn [a2 ][b2 ] 3.3. Performance of F IND -C LOSEST-R ANK. For
a1 /b1
← a1 /b1 , a2 /b2
← a2 /b2 studying the performance of F IND -C LOSEST-R ANK and
repeat to compare it with binary search, a set of 100 randomly
if a/b < a2 /b2 then generated proper fractions in [0, 1] is taken, and the av-
a2 /b2 ← a2 /b2 erage number of iterations required for each of these two
(1)
else methods is considered for Fn , with its order n varying
a1 /b1 ← a2 /b2 from 10 to 1000. We generate the set of random fractions
such that no fraction belongs to Fn . The corresponding
y1 ← (a1 b − b1a)/(b1 b), y2 ← (a2 b − b2 a)/(b2 b)
average number of iterations required for these randomly
r ← (r1 y2 − r2 y1 )/(y2 − y1 ) zero-crossing (1)
a1 /b1 ← Fn [r] generated sets for different Fn is plotted against n in
if a1 /b1 < a/b then Fig. 4. From this plot, it can be noticed that F IND -
a2 /b2 ← Fn [r + 1] C LOSEST-R ANK runs significantly faster compared with
binary search. Even for n of order of 1000, F IND -
else
C LOSEST-R ANK reports the closest rank in just two or
a2 /b2 ← Fn [r − 1]
three iterations.
until
((a1 /b1 ≤ a/b ≤ a2 /b2 ) ∨ (a2 /b2 ≤ a/b ≤ a1 /b1 ))
if a/b − a1 /b1 < a2 /b2 − a/b then 3.4. Comparison with the existing technique.
return Fn [a1 ][b1 ]
(1) Recently, an algorithm has been proposed by Charrier and
Buzer (2009) for determining the fraction ab ∈ Fn so
else a
that it is closest to any given fraction b . The technique is
return Fn [a2 ][b2 ]
(1)
based on the principal convergent series of an even order
and that of an odd order, denoted respectively by SE and
SO , which are obtained by decomposition to a continued
Case 2 (r2 − r1 = 1). There are only two fractions in fraction. As a natural follow-up, we compare our F IND -
the range [a1 /b1 , a2 /b2 ]. Therefore, the fraction between C LOSEST-R ANK with this algorithm, and some examples
a1 /b1 and a2 /b2 lying closest to a/b is returned, using the on the comparison are given below.
if-else statements.
Example 1. To detect the closest fraction of ab = 145 78
action:
a1 a2 return return a2 a2
b1
← b2 b2
← b2
closest rank closest rank
SO = {1/1, 2/3, 3/5, 4/7, 5/9, 6/11, 7/13}. Polygonal approximation is an important task in the
paradigm of computer vision, since it can succinctly
The closest fraction of ab in SE is 36 represent the boundary of an object (Melkman and
67 with error 0.000618
and in SO is 137
with error 0.000531. Hence, the reported O’Rourke, 1988). The result can subsequently be
7
closest fraction is 13 . Our algorithm also reports the same used in various applications, such as image analysis
(see Fig. 3(a)). (Neumann and Teisseron, 2002), image registration
(Wang et al., 2013; Zhang et al., 2015), image matching
a 1957 (Mikolajczyk and Schmid, 2005; Wang et al., 2009;
Example 2. Let b = 2788 , n = 99. We have
Zhang and Koch, 2013), shape understanding (Attneave,
1957 1954; Kumar et al., 2002; O’Connell, 1997), image
= [0 1 2 2 1 4 2 6 4]. and video retrieval (Mokhtarian and Mohanna, 2002;
2788
Van and Le, 2016). There exists a multitude of
Thus, algorithms for polygonal (or poly-chain) approximation
of a digital curve; see, for example, the works of
SE = {0/1, 1/2, 2/3, 7/10, 40/57, 73/104, 544/775, Bhowmick and Bhattacharya (2007), Wall and Danielsson
(1984), Chung et al. (2008), Koutroumbas (2012), Liu
1015/1446, 1486/2117, 1957/2788}
et al. (2008), Nguyen and Debled-Rennesson (2011),
Parvez and Mahmoud (2010), Prasad et al. (2012),
and
Rosin (1997), Teh and Chin (1989) or Yin (2004) and
the references therein. These are based on various
SO = {1/1, 3/4, 5/7, 12/17, 19/27, 26/37, 33/47, techniques like normalization of area deviation (Wall
106/151, 179/255252/359, 325/463, and Danielsson, 1984), curvature estimation (Liu et al.,
398/567, 471/671}. 2008; Teh and Chin, 1989), dominant point detection
(Prasad et al., 2012), particle swarm optimization (Yin,
The closest fraction reported is 40 2004), approximate digital straightness (Bhowmick and
57 , which matches
our result (see Fig. 3(c)). Bhattacharya, 2007), etc. Working principles of these
algorithms and their comparative study are given in
The time complexity of the algorithm is mainly Table 2.
determined by the number of iterations in the re- We show here how the AFT can be used for
peat-until loop. Since the fraction difference has a polygonal approximation of a (thinned) digital curve, once
grossly linear relation with rank, this loop converges the approximately digital straight segments (ADSSs) are
very fast within a small number of iterations because extracted from the curve (Bhowmick and Bhattacharya,
of the interpolation by the regula falsi method. 2007). For this purpose, the endpoints of the ADSS
Hence, the time complexity becomes a constant are taken in order and stored in V . Each maximal
factor of the arithmetic operations used in different subsequence of vertices in V , which are “almost
steps of the algorithm. The major operations are collinear”, is replaced by a single edge. That is,
GCD computation and multiplication/division, whose if eh , eh+1 , . . . , eh+i is the maximal subsequence of
respective time complexities in terms of bit operations are straight edges that are almost collinear, i.e., they conform
O(log2 n) and O(loglog 3 n) (Cormen et al., 2000). As to some approximation criterion, then these (i + 1)
a result, the overall time complexity becomes O(log2 n), edges are combined to a single edge. The process is
which is asymptotically better than the algorithm based on repeated for all such maximal subsequences in succession
binary search. to obtain a reduced sequence of (almost) straight edges
On the Farey sequence and its augmentation for applications to image analysis 645
Algorithm 3. P OLY-A PPROX -FT. are used to compute their differential Farey indices.
(1) Algorithm 3 shows the steps of the proposed algorithm,
Input: V, m, Fn , τ P OLY-A PPROX -FT. It takes the set V that stores m
Output: V endpoints defining the sequence of edges. The parameter
V ←V τ denotes the approximation parameter, and it signifies
for h ← 1 to m − 2 do the amount of approximation. A high value of τ provides
i ← 1, iLoop ← T RUE a loose approximation, whereas a low value gives a tight
while ((i < m − h) ∧ (iLoop = T RUE )) do approximation,
j←1 Algorithm 3 has a for loop containing two nested
while j ≤ i do while loops. Each iteration of the for loop finds a maximal
dx1 ← V [h + i + 1] .x − V [h] .x subsequence of straight edges that are approximately
dy1 ← V [h + i + 1] .y − V [h] .y collinear (within a tolerance τ ). In particular, if
dx2 ← V [h + j] .x − V [h + i + 1] .x eh , eh+1 , . . . , eh+i is the maximal subsequence, then
dy2 ← V [h + j] .y − V [h + i + 1] .y the distance of each vertex vh+j , 1 ≤ j ≤ i, from the
l ← max(|dx2 |, |dy2 |) straight line segment vh vh+i+1 is at most τ . Note that the
(1)
r1 ← Fn [dy1 ][dx1 ] Eqns. 3, 4 vertex vh+j is shared by the edges eh+j−1 and eh+j . For
(1)
r2 ← Fn [dy2 ][dx2 ] Eqns. 3, 4 each straight line segment pq with endpoints p(xp , yp ) and
r ← δn (r1 , r2 ) y −y
q(xq , yq ), the rank of the fraction xqq −xpp is obtained from
a/b ← F n [r] √ Fn using single memory access.
if l · |a| > τ · a2 + b2 then Each iteration of the inner while loop computes the
i ← i − 1, iLoop ← FALSE deviation (Euclidean distance) of a vertex vh+j from the
break straight line segment vh vh+i+1 . This computation is done
j ←j+1
using the differential Farey index obtained from the ranks
if iLoop = T RUE then corresponding to the slopes of vh vh+i+1 and vh+i+1 vh+j ,
delete V [h + i]
as shown in Fig. 5. This differential Farey index is treated
i←i+1 as a rank and its corresponding fraction, shown as ab in
h←h+i Fig. 5, is obtained from Fn . Consequently, the deviation
return V of vh+j from vh+j vh+i+1 is estimated as
a · vh+j vh+i+1
V corresponding to the object boundary. The process is d2 (vh+j , vh vh+i+1 ) ≈ √ , (6)
a2 + b 2
repeated for all such maximal subsequences in succession
to obtain a reduced set of straight edges corresponding to where vh+j vh+i+1 denotes the length of vh+j vh+i+1 .
the object boundary. The inner while loop executes for j varying from 1
to i. If the deviation lies within the threshold τ for each
vertex vh+j , then eh , eh+1 , . . . , eh+i is considered to be
4.1. Approximation by the differential Farey index. approximately straight; to include the next edge in this
There are several approximation criteria available in the sequence for its maximality, this loop is again executed
literature (Bhowmick and Bhattacharya, 2007; Rosin and with the value of i incremented to i + 1 in the outer while
West, 1988; 1995; Wall and Danielsson, 1984). As we use loop. If the deviation exceeds the threshold τ for some
the AFT for polygonal approximation, we introduce here vertex vh+j in the inner while loop, then the previous
the notion of a differential Farey index. For ranks r1 and solution remains the maximal solution, and the algorithm
r2 of two fractions in Fn , it is defined as finds the next maximal subsequence with initialization
from the for loop.
Δr21 if Δr21 ≤ 4fn − 4, The inner while loop runs in a time linear in the
δn (r1 , r2 ) =
8fn − 8 − Δr21 otherwise. number of vertices from vh+1 to vh+1 . Hence the overall
(5) time complexity obtained by summing up this runtime
Here, Δr21 = |r2 − r1 |. For example, in Fig. 1, n = becomes quadratic in the total number of input vertices.
4, fn = 7, which gives 4fn − 4 = 24. So, for r1 = 1 and However, as mentioned earlier, the input sequence of
r2 = 25, we get δn (r1 , r2 ) = 24; as r2 increases to 26, vertices is obtained by extraction of the ADSS from a
we have |r2 −r1 | = 25, and so δn (r1 , r2 ) = 48−25 = 23. digital curve. If the digital curve consists of M pixels,
While checking the approximate collinearity of a then the input sequence of m vertices is obtained in
subsequence of straight edges coming from the set of O(M ) time (Bhowmick and Bhattacharya, 2007). If m
straight edges E, their slopes are considered ranks of is asymptotically smaller than M , then the overall time
the corresponding fractions stored in Fn . These ranks complexity becomes asymptotically less than O(M 2 ).
646 S. Pratihar and P. Bhowmick
Table 2. Comparative study on existing polygonal approximation algorithms (listed chronologically) and the proposed algorithm.
Principle Non- Complexity Error control Non-recursive
Euclidean
Normalization of area deviation (Wall and Danielsson, No Low Yes No
1984)
Curvature estimation and non-maxima suppres- No High No No
sion (Teh and Chin, 1989)
Dominant point detection using trigonometric opera- No High No No
tions (Ray and Ray, 1992)
Particle swarm optimization (Yin, 2004) No High Yes No
Genetic algorithm (Sarkar et al., 2004) No High No No
Digital straightness properties and area devia- Yes Low Yes Yes
tion (Bhowmick and Bhattacharya, 2007)
Curvature estimation based on local integral square er- No High Yes No
ror (Chung et al., 2008)
Chain code and removal of dominant points (Masood, No High Yes No
2008)
Statistical and geometric properties (Dinesh and Guru, No High Yes No
2009)
Curvature estimation and adaptive fitting (Parvez and No High Yes No
Mahmoud, 2010)
Maximal blurred segments (Nguyen and Debled- No Low Yes No
Rennesson, 2011)
Graph-theoretic tools and elementary geome- No Low Yes No
try (Koutroumbas, 2012)
Reverse engineering on Bresenham’s algorithm (Ray Yes Low No Yes
and Ray, 2013)
Proposed algorithm: digital straightness properties Yes Low Yes Yes
and differential Farey index
yh+j −yh+i+1
rank = rn xh+j −xh+i+1
Fig. 5. Estimation of the deviation of a point vh+j from vh vh+i+1 using the differential Farey index.
Fig. 6. Results using the proposed algorithm: input image (a), straight edges extracted (b), polygonal approximation: τ = 1 : m =
7 (c), polygonal approximation: τ = 2 : m = 6 (d).
On the Farey sequence and its augmentation for applications to image analysis 647
(a) (b)
(c) (d)
Fig. 8. Polygonal approximation for the image lab, using the proposed algorithm (after detection of thin edge). Input image: lab
(512 × 484 pixels) (a), edge map extracted using the approach of Pratihar and Bhowmick (2009), M = 9349 (b), τ = 1,
m = 1080 (c), τ = 4, m = 561 (d).
√
For example, if m = O( M ), then the overall time results shown in this paper are obtained for n = 200. The
complexity is linear in M . In general, the actual runtime significance of τ in our algorithm is evident from a set
would depend on the amount of variation in direction of experimental results shown in Fig. 6. The number of
while traversing the input curve. edge points, the number of vertices before approximation,
and the number of vertices in the polygon or polychain
after approximation are denoted here by M , m, and m ,
4.2. Results of polygonal approximation. We have respectively. For τ = 1, we get a tight approximation with
implemented our algorithm in C in Ubuntu 12.04, Linux m = 7, whereas for τ = 2, we have more relaxation with
Kernel 3.2.0-54-generic 64-bit, Intel
R
TM
Core i5-2310 m = 6. We notice the same pattern for other images ,
CPU 2.90 GHz. Unless mentioned otherwise, all the too, such as the ones shown in Figs. 7 and 8.
648 S. Pratihar and P. Bhowmick
τ =1
m = 34 m = 30 m = 24 m = 26 m = 30
τ =2
m = 17 m = 16 m = 16 m = 14 m = 19
τ =5
m = 9 m = 9 m = 9 m = 9 m = 11
τ =8
m = 8 m = 8 m = 8 m = 8 m = 8
CM (Teh and Chin, 1989) ACS (Yin, 2003) AD (Wall and Danielsson, 1984)PO (Hu and Yan, 1997)
m = 15 m = 12 m = 12 m = 16
Fig. 11. Results of polygonal approximation obtained by existing algorithms and the proposed algorithm for the image chromosome.
d∞ p, p = max{|x − x |, |y − y |}. (7) The value d∞ p, p depends on the provided value
650 S. Pratihar and P. Bhowmick
CM (Teh and Chin, 1989) ACS (Yin, 2003) AD (Wall and Danielsson, 1984) PO (Hu and Yan, 1997) DZ (Buzer, 2009) proposed
m = 20 m = 16 m = 19 m = 26 m = 14 m = 17
Fig. 12. Results of polygonal approximation obtained by existing algorithms and the proposed algorithm for the image semi-circle
( = 1 for DZ and τ = 1 for the proposed).
Table 4. Comparative results on the number of operations for the image soldier for the polygonization part. (mul., add., comp.,
mem., assn., inr./dcr., AND denote the respective numbers of multiplications, additions, comparisons, memory accesses,
assignments, increments/decrements, and logical AND operations.)
Number of operations
τ m m mul. add. comp. mem. assn. inr./dcr. AND
1 302 209 2296 2435 1767 0 2890 664 295
Bhattacharya (2007)
of τ , and the average deviation is measured as from these plots that the output produced by the proposed
algorithm complies with the one produced using the
δE algorithm given by Bhowmick and Bhattacharya (2007).
δ= , (9)
M Comparative results on the error frequency for these two
where M denotes the total number of pixels in E. Again, images are also shown in Figs. 14 and 15. It yields
for the measure of the error frequency, for a chosen τ , high frequency for low deviation, and low frequency for
we find the number of edge points in P deviated from the high deviation, which indicates its qualitative merit in the
edges of E by a distance δ(= 0, 1, 2, 3, . . .). Then the process of approximation. Table 4 shows a comparative
error frequency is defined as study on the number of operations required for polygonal
approximation of soldier image. It can be noticed
1 in this table that, in comparison with the algorithm
ν(τ, δ) = |{p ∈ e : d∞ p, p = δ}|, (10)
M of Bhowmick and Bhattacharya (2007), the number of
e∈E
multiplications is significantly lower in the proposed
where the variation of ν(τ, δ) versus τ and δ provides the algorithm. However, the proposed algorithm needs
error distribution for polygonal approximation. memory access for accessing ranks from the AFT. Also, it
Since the focus of our work is on the AFT and its needs some more comparisons and assignments compared
applicability to practical problems, we have compared our with that of Bhowmick and Bhattacharya (2007). As
algorithm with one of those proposed by Bhowmick and the number of multiplications is reduced significantly, we
Bhattacharya (2007), which has a fast execution owing notice a marginal improvement in CPU time (see Table 5).
to integer operations. Plots for average deviation caused Also, we have compared the performance of our
by approximation are shown in Fig. 13 for the images method with that of some of the existing methods. These
soldier and bird-children. It can be noticed are curvature maximization (CM (Teh and Chin, 1989)),
On the Farey sequence and its augmentation for applications to image analysis 651
Proposed method
1 Bhowmick and Bhattacharya
1
0.9
avg. deviation
0.8 1 0.8
0.7
0.8 0.6
0.5
(,)
0.4
0.6 0.6 0.3
0.2
0.1
0.4 0
0.4
0.2
0 0
0.2 2
1 4
2 6
3
4 8
0 5 10
1 2 3 4 5 6 7 8
approx parameter
(a)
Proposed method
1 Bhowmick and Bhattacharya
avg. deviation
0.8 1
0.9
1 0.8
0.7
0.8 0.6
0.6 0.5
(,)
0.4
0.6 0.3
0.2
0.4 0.4
0.1
0
0.2
0.2 0
0 2
1 4
2 6
0 3
8
1 2 3 4 5 6 7 8 4
5 10
approx parameter
Table 5. Effect of n on the result obtained by the algorithm for the image soldier. (CPU time is shown in seconds for the polygo-
nization part.)
τ =2 τ =5 τ =8
Method m m CPU time m CPU time m CPU time
Proposed
n = 10 302 160 0.000083 96 0.000058 78 0.000051
n = 20 302 152 0.000075 90 0.000046 75 0.000044
n = 30 302 145 0.000070 89 0.000046 73 0.000042
n = 50 302 146 0.000064 87 0.000044 72 0.000050
n = 100 302 144 0.000063 87 0.000047 72 0.000051
n = 150 302 145 0.000062 87 0.000046 72 0.000051
n = 200 302 143 0.000061 87 0.000045 72 0.000055
Existing algorithm (Bhowmick 302 156 0.000094 90 0.000071 73 0.000068
and Bhattacharya, 2007)
0.4
0.6 0.3
0.4
0.2
0.1
0
for i ← 0, 1, . . . , nB do
0.2 B[i].len ← 0, B[i].wrank ← 0
0 2
0 ai ← w · i, bi ← min{ai + w − 1, |F n |}
1 4
2
3 6 [ai , bi ] = range of ranks in B[i]
4 8
for each ej (v = (x, y), v = (x , y )) ∈ E do
5 10
dy = |y − y |, dx = |x − x |
(a) lj ← max{dy, dx}
(1)
ρj ← Fn [dy][dx]
i ← w1 ρj
1
0.9
B[i].len ← B[i].len + lj
1
0.8
0.8
0.7
0.6
B[i].wrank ← B[i].wrank + lj · ρj
0.5
select the principal bin B[s] having maximum len
(,)
0.4
0.6 0.3
0.2
0.4
0.1
0 ρw ← B[s].wrank/B[s].len Eqn. (11)
0.2 ps /qs ← F n [ρw ]
0 0
1 4
2 return ps /qs
2 6
3
4 8
5 10
τ = 2, n = 10 τ = 2, n = 30 τ = 2, n = 50 τ = 2, n = 100 τ = 5, n = 10 τ = 5, n = 30
τ = 5, n = 50 τ = 5, n = 100 τ = 8, n = 10 τ = 8, n = 30 τ = 8, n = 50 τ = 8, n = 100
Fig. 16. Results of polygonal approximation for different values of the Farey order, n.
Fig. 17. Skew correction—step-wise snapshots of our algorithm: input (a), detected straight edges defining boundaries (b), principal
and its orthogonal direction shown in gray and in black, respectively (c), deskewed image (d).
and Rosenfeld, 2004), and so it is given by e∞ = taken w = 100 and the order of F n as n = 200,
max{|x − x |, |y − y |}. If the length of a straight edge whence |F n | = 97856. Accordingly, two consecutive
exceeds a threshold value, lmin , then it is included in fractions will approximately correspond to a difference
E. The value of lmin is set as twice the average font of 360◦ /97856 = 0.003679◦, which is practically a very
height. The rank of each edge e is obtained from Fn by small value. This fact has been exploited in binning. Since
single memory access, which saves time to a significant there are w = 100 consecutive ranks in each B[i], they
amount in comparison with slope-angle computation by approximately span over 0.003679×100 = 0.368◦ , which
trigonometric procedures linked to the math library. bounds the maximum possible binning error.
Table 6. Distribution of straight edges found for the image of Fig. 18, in B[0], B[1], . . . , B[489], each bin being of width w = 100.
Bin B[485] corresponds to the principal direction and Bin B[240] to the orthogonal one (highlighted in gray).
rank bin #lines Σ len. rank bin #lines Σ len. rank bin #lines Σ len.
0– 99 1 22 .. ..
300– 399 2 66 . .
600– 699 1 37 23700–23799 16 724 48100–48199 10 400
2300–2399 1 53 23800–23899 12 833 48200–48299 18 782
2500–2599 1 57 23900–23999 38 2768 48300–48399 18 1098
2900–2999 1 45 24000–24099 50 5026 48400–48499 30 2457
3200–3299 1 41 24100–24199 40 3398 48500–48599 53 5239
.. 24200–24299 8 441 48600–48699 37 2827
. 24300–24399 1 75 48700–48799 1 57
..
.
Knuth, D. E. (1997). The Art of Computer Programming, Vol. 2, Parvez, M.T. and Mahmoud, S.A. (2010). Polygonal
3rd Edn., Addison-Wesley, Boston, MA. approximation of digital planar curves through
adaptive optimizations, Pattern Recognition Letters
Koutroumbas, K.D. (2012). Piecewise linear curve
31(13): 1997–2005.
approximation using graph theory and geometrical
concepts, IEEE Transactions on Image Processing Pătraşcu, C.E. and Pătraşcu, M. (2004). Computing order
21(9): 3877–3887. statistics in the Farey sequence, Symposium on Algorith-
mic Number Theory, Burlington, VT, USA, pp. 358–366.
Kumar, M.P., Goyal, S., Jawahar, C.V. and Narayanan, P.J.
(2002). Polygonal approximation of closed curves across Pavlidis, T. and Zhou, J. (1991). Page segmentation by white
multiple views, 3rd Indian Conference on Computer Vi- streams, International Conference on Document Analysis
sion, Graphics and Image Processing, Ahmadabad, India, and Recognition, Saint-Malo, France, pp. 945–953.
pp. 317–322. Pawlewicz, J. and Pătraşcu, M. (2009). Order statistics in the
Farey sequences in sublinear time and counting primitive
Le, D.S., Thoma, G.R. and Wechsler, H. (1994). Automatic page lattice points in polygons, Algorithmica 55(2): 271–282.
orientation and skew angle detection for binary document
images, Pattern Recognition 27(10): 1325–1344. Prasad, D.K., Leung, M.K.H., Quek, C. and Cho, S. (2012).
A novel framework for making dominant point detection
Li, S., Shen, Q. and Sun, J. (2007). Skew detection using methods non-parametric, Image and Vision Computing
wavelet decomposition and projection profile analysis, Pat- 30(11): 843–859.
tern Recognition Letters 28(5): 555–562.
Pratihar, S. and Bhowmick, P. (2009). A thinning-free algorithm
Liu, H., Latecki, L. and Liu, W. (2008). A unified curvature for straight edge detection in a gray-scale image, Inter-
definition for regular, polygonal, and digital planar curves, national Conference on Advances on Pattern Recognition,
International Journal of Computer Vision 80(1): 104–124. Kolkata, India, pp. 341–344.
Manjunath, V.N., Kumar, G.H. and Shivakumara, P. (2006). Pratihar, S. and Bhowmick, P. (2010). Vectorization of
Skew detection technique for binary document images thick digital lines using Farey sequence and geometric
based on Hough transform, International Journal of Infor- refinement, ICVGIP, Chennai, India, pp. 518–525.
mation and Communication Engineering 3(7): 493–499. Pratihar, S. and Bhowmick, P. (2011). Skew correction of
Masood, A. (2008). Dominant point detection by reverse engineering drawings by digital-geometric analysis of
polygonization of digital curves, Image and Vision Com- Farey ranks, International Conference on Image Informa-
puting 26(5): 702–715. tion Processing (ICIIP), Shimla, India, pp. 1–6.
Pratihar, S., Bhowmick, P., Sural, S. and Mukhopadhyay, J.
Melkman, A. and O’Rourke, J. (1988). On polygonal chain (2013). Skew correction of document images by rank
approximation, in G.T. Toussaint (Ed.), Computational analysis in Farey sequence, International Journal of Pat-
Morphology, North-Holland, Amsterdam, pp. 87–95. tern Recognition and Artificial Intelligence 27(7): Article
Mikolajczyk, K. and Schmid, C. (2005). A performance ID 1353004.
evaluation of local descriptors, IEEE Transac- Ray, B.K. and Ray, K.S. (1992). An algorithm for detection of
tions on Pattern Analysis and Machine Intelligence dominant points and polygonal approximation of digitized
27(10): 1615–1630. curves, Pattern Recognition Letters 13(12): 849–856.
Mokhtarian, F. and Mohanna, F. (2002). Content-based video Ray, K.S. and Ray, B.K. (2013). Polygonal approximation of
database retrieval through robust corner tracking, IEEE digital curve based on reverse engineering concept, Inter-
Workshop on Multimedia Signal Processing, St. Thomas, national Journal of Image and Graphics 13(4): Article ID
Virgin Islands, USA, pp. 224–228. 1350017.
Neumann, R. and Teisseron, G. (2002). Extraction of dominant Rosin, P.L. (1997). Techniques for assessing polygonal
points by estimation of the contour fluctuations, Pattern approximation of curves, IEEE Transactions on Pattern
Recognition 35(7): 1447–1462. Analysis and Machine Intelligence 19(6): 659–666.
Neville, E.H. (1950). The Farey Series of Order 1025, Rosin, P.L. and West, G.A.W. (1988). Detection of circular arcs
Cambridge University Press, Cambridge. in images, 4th Alvey Vision Conference, Manchester, UK,
pp. 259–263.
Nguyen, T.P. and Debled-Rennesson, I. (2011). A discrete Rosin, P.L. and West, G.A.W. (1995). Non-parametric
geometry approach for dominant point detection, Pattern segmentation of curves into various representations, IEEE
Recognition 44(1): 32–44. Transactions on Pattern Analysis and Machine Intelli-
Nikiel, S. (2007). A proposition of mobile fractal image gence 17(12): 1140–1153.
decompression, International Journal of Applied Math- Routledge, N. (2008). Computing Farey series, Mathematical
ematics and Computer Science 17(1): 129–136, DOI: Gazette 92(523): 55–62.
10.2478/v10006-007-0012-5.
Sarkar, B., Singh, L.K. and Sarkar, D. (2004). A genetic
O’Connell, K.J. (1997). Object-adaptive vertex based shape algorithm-based approach for detection of significant
coding method, IEEE Transactions on Circuits and Sys- vertices for polygonal approximation of digital curves, In-
tems for Video Technology 7(1): 251–255. ternational Journal of Image and Graphics 4(2): 223–239.
658 S. Pratihar and P. Bhowmick
Schroeder, M. (2006). Fractions: Continued, Egyptian and Sanjoy Pratihar received his BTech and ME de-
Farey, in M.R. Schroeder (Ed.), Number Theory in Sci- grees from North Eastern Hill University, Shil-
ence and Communication, Springer, Berlin/Heidelberg, pp. long, India, and from Bengal Engineering and
Science University, Shibpur (presently the Indian
55–86.
Institute of Engineering Science and Technol-
Singh, C., Bhatia, N. and Kaur, A. (2008). Hough transform ogy), India, respectively, both in computer sci-
based fast skew detection and accurate skew correction ence and engineering. He received his PhD from
methods, Pattern Recognition 41(12): 3528–3546. the Indian Institute of Technology, Kharagpur,
India. Currently he is serving as an assistant pro-
Srihari, S.N. and Govindraju, V. (1989). Analysis of textual fessor in the Department of Computer Science
images using the Hough transform, Machine Vision Appli- and Engineering, National Institute of Technology Meghalaya, Shillong.
cations 2(3): 141–153. His research interests include digital geometry, low-level image process-
ing, document image processing, shape analysis, and intelligent human-
Teh, C.H. and Chin, R.T. (1989). On the detection of dominant computer interaction. He has coauthored several research papers, pub-
points on digital curves, IEEE Transactions on Pattern lished in various international conference proceedings, edited volumes,
Analysis and Machine Intelligence 2(8): 859–872. and journals in these fields.