On the Farey Sequence and Its Augmentation for Applications to Image Analysis
Total Page:16
File Type:pdf, Size:1020Kb
Int. J. Appl. Math. Comput. Sci., 2017, Vol. 27, No. 3, 637–658 DOI: 10.1515/amcs-2017-0045 ON THE FAREY SEQUENCE AND ITS AUGMENTATION FOR APPLICATIONS TO IMAGE ANALYSIS ∗ SANJOY PRATIHAR a, ,PARTHA BHOWMICK b aDepartment of Computer Science and Engineering National Institute of Technology Meghalaya, Bijni Complex, Shillong, India e-mail: [email protected] bDepartment 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. 1. Introduction If we disregard the ordering of Fn for sake of notational simplicity, then Given a positive integer n,theFarey sequence Fn of or- der n is the (ordered) sequence of all simple fractions a Fn = :(0≤ a ≤ b ≤ n) ∧ (b>0) starting from 0, ending at 1, and having denominators not b exceeding n (Hardy and Wright, 1968). For example, the ∧ (gcd(a, b)=1) . (1) first five sequences are The Farey sequences are named after John Farey, 0 1 F = , , who first conjectured in 1816 that Fn can be obtained 1 1 1 from Fn−1; a brief history following that can be found 0 1 1 in the work of Hardy and Wright (1968). There are F = , , , 2 1 2 1 several works related with the Farey sequence, which 0 1 1 2 1 mostly concern the theory of fractions (Hardy and Wright, F3 = , , , , , 1968; Graham et al., 1994; Neville, 1950; P˘atras¸cu and 1 3 2 3 1 P˘atras¸cu, 2004; Pawlewicz and P˘atras¸cu, 2009; Schroeder, 0 1 1 1 2 3 1 F = , , , , , , , 2006). However, from the viewpoint of algorithms or 4 1 4 3 2 3 4 1 computation, limited work has been done so far. A 0 1 1 1 2 1 3 2 3 4 1 computationally interesting problem addressed in recent F = , , , , , , , , , , . 5 1 5 4 3 5 2 5 3 4 5 1 time is the rank problem and its associated order statis- tic problem (Pawlewicz and P˘atras¸cu, 2009). The rank ∗ Corresponding author rn (x) of a fraction x in a Farey sequence Fn is the 638 S. Pratihar and P. Bhowmick 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˘atras¸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 a ∈ c ∈ simple example. Given three integer points (i.e., pixels) Given two fractions b Fn and d Fn,theirmediant is a+c p1 i1,j1 , p2 i2,j2 ,andp3 i3,j3 in succession, the defined as the fraction b+d , which always lies between a c et al metric used to decide on the deviation of p2 from p1p3 is b and d (Graham ., 1994). Based on this unique property, the sequence F can be computed from the given by Δ p1,p2,p3/d∞(p1,p3),where d∞(p1,p3)= n sequence F − by inserting in F the mediant of any two max |i1−i3|, |j1−j3| and Δ p1,p2,p3 denotes the area n 1 n consecutive fractions of F − unless the denominator of of the triangle with vertices p1,p2,p3. This is a commonly n 1 followed practice (Bhowmick and Bhattacharya, 2007; the mediant does not exceed n. To recursively generate Fn F − Wall and Danielsson, 1984), which requires several from n 1 based on mediants, it is required to check all pairs of adjacent fractions in F − . This calls for Θ((n − multiplications for computing the value of Δ p1,p2,p3 . n 1 2 3 2 Using an AFT provides a simpler and faster way to reach 1) ) steps, since Fn contains fn = π2 n + O(n log n)= 2 the solution by keeping away such multiplications, as Θ(n ) fractions (Hardy and Wright, 1968). Hence, the 3 shown in Section 4. time complexity to generate Fn becomes Θ(n ). 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˘atras¸cu and P˘atras¸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 a ∈ Denominator Lemma 1. For any fraction b Fn, the next fraction in u+ka n−v -4 -3 -2 -1 0 1 2 3 4 Fn is v+kb ,wherek = b and (u, v) is any solution − 4 19 18 16 14 13 12 10 8 7 to bx ay =1. 3 20 19 17 15 13 11 9 7 6 2 22 21 19 16 13 10 7 5 4 Proof. Since gcd(a, b)=1, there exist two integers u and 1 24 23 22 19 13 7 4 3 2 − c v such that bu av =1(Hardy and Wright, 1968).