A programme to determine the exact interior of any connected digital picture
aa r X i v : . [ c s . C G ] J a n A programme to determine the exact interior of any connected digital picture
A. E. Fabris a, , V. Ramos Batista b a USP - IME, r. do Mat˜ao 1010, 05508-090 S˜ao Paulo-SP, Brazil b UFABC - CMCC, r. Sta. Ad´elia 166, Bl.B, 09210-170 St. Andr´e-SP, Brazil
Abstract
Region filling is one of the most important and fundamental operations in computer graphics and image processing. Many fillingalgorithms and their implementations are based on the Euclidean geometry, which are then translated into computational modelsmoving carelessly from the continuous to the finite discrete space of the computer. The consequences of this approach is thatmost implementations fail when tested for challenging degenerate and nearly degenerate regions. We present a correct integer-onlyprocedure that works for all connected digital pictures. It finds all possible interior points, which are then displayed and stored in alocating matrix. Namely, we present a filling and locating procedure that can be used in computer graphics and image processingapplications.
Keywords:
Raster Graphics, Region Filling, Point-based Modelling, Discrete Jordan Curve Theorem
1. Introduction
Region filling is a crucial operation in computer graphicsand image processing. There are two basic approaches to re-gion filling on raster systems. One of them is to start froma given interior position called seed , and paint outward fromthis point until we meet the specified boundary conditions. Theother is to determine the overlap intervals of scan lines thatcross the region. The seed-fill methods are mostly applied ininteractive painting systems. Alternatively, the scan-conversionapproach is typically used in hardware implementations and ingeneral graphic packages to fill polygons, circumferences, el-lipses and spline curves. These two approaches will be resumedin Section 2, but now we focus on their limitations: the non-robustness of scan conversion, and the high computational costof seed fill.As explained in Section 2, neither seed fill nor scan con-version can really provide perfect filling at the lowest compu-tational cost. This is yet desirable in many circumstances. Welist three of them.The first is represented in Figure 1. It schematises the mouldof an integrated circuit. In general, thousands of models aremanufactured from the mould. A single flaw in it will pro-duce useless lots, and therefore a great waste of material andtime. Until nowadays, this kind of flaw detection still dependson handwork.In fact, Figure 1 right is an output of our algorithms. Theyaim at filling regions perfectly and quickly. As an application,this helps detect imperceptible flaws. For the time being, how-ever, they can detect only external flaws, but are adaptable forthe whole job.Our second example is of theoretical interest. Of course,there is already a valuable and extensive literature about regionfilling and point location. In fact, both problems are intimately
Figure 1: Detecting an imperceptible flaw in a mould of integrated circuit. related. Now, according to [1], none of them is free of mi-nor failures, and the best ones are computationally expensive.Therefore, if there are algorithms that prove to be quick andfaultless, they are really worth studying.The third and most striking example is the technology of3D-printing. This paper deals with a two-dimensional approach.But our algorithms were elaborated with ideas that are extend-able to higher dimensions. In fact, we are already researchingthis problem for future works. The basic idea is to slice a 3-dimensional object into several 2-dimensional ones, which arethen treated individually. In this case, least computational timebecomes highly important. But also perfect filling and pointlocation. The problem of printing plastic cups, for instance, isequivalent to what Figure 1 illustrates.Of course, when performing region filling or point locationwe can content ourselves with flaws as we see in Figure 2 drawnin Xfig and saved as a PostScript file. If not, seed fill reducesthem to a minimum but usually with a high computational cost.Our research was motivated by the fact that scan-conversionalgorithms are not always reliable. In the case of point loca-tion algorithms, they fail even for non-challenging regions, asdemonstrated in [1, 2]. Moreover, in [1] Schirra presents a com-plete comparison among all classical point-location algorithms.In this paper, the di ffi culties in discretisation are acknowl- Preprint submitted to Axiv January 16, 2014 igure 2: (a) A panda’s drawing; (b) detail of area around one eye showingindividual pixels. edged and tackled by casting the problem to be solved as a dis-crete integer problem from the very outset. We believe thatregion filling and point location algorithms cannot be alwaysreliable, unless these problems are set up and solved in a dis-crete space. Hence, we never have to accommodate any in-convenience of numerical accuracy that follows from floatingpoint discretisation, geometric approximation, or less rigorousapproach to final discretisation in the rendering process. Wegive formal definitions of the involved discrete topology, to-gether with a mathematical proof of robustness.However, we do not start from mathematical models. Ourinput is solely a matrix with entries 0 and 1, which we inter-pret as a picture of black pixels surrounded by white pixels. Noequations are given beforehand. Then we construct a mathe-matical model that retrieves our input, performs the region fill-ing and generates a filling matrix. This matrix locates any pointas belonging to the interior, exterior or to the picture itself.Our work is organised as follows. Section 2 gives somebackground on the two classical region filling algorithms. Sec-tion 3 explains some basic terms and definitions. They are usedin Section 4, where we discuss our main programme and its al-gorithm. The main programme is endowed with an optional im-provement, which is explained in Section 5. This improvementwill make the main programme correct, and Section 6 presentsa theoretical proof of this fact. Details about downloading andrunning our programme are explained in Section 7, whereasSection 8 is devoted to our conclusions. Finally, Appendix Adescribes some theoretical results that can be achieved throughthe source codes.
2. Seed fill and scan conversion
Seed-fill algorithms can be subdivided into four steps [3]: a start procedure to choose an interior starting point, namely theseed; a propagation method to determine the next consideredpoint; an inside test to check whether a point is in the regionand if it should be marked; a set procedure to change the colourof a given picture element or pixel . The start procedure can bedone automatically or user-guided. For complex objects, multi-ple seeds may be required and their automatic detection can bevery di ffi cult. This is one of the main reasons why this familyof algorithms is more suitable for interactive region filling [4].For the propagation method , the na¨ıve implementation of thealgorithm searches for connected pixels recursively, and can be based either on pixel- or line-adjacency. In the former, the fill-ing procedure is to move from a current pixel to all immediateneighbours in a certain order. In the latter, the interior of a re-gion is regarded as a set of adjacent horizontal line segments.Usually, seed-fill algorithms need working memory in additionto the frame bu ff er, and are slower than scan-conversion algo-rithms. The amount of working memory cannot be constant, forit increases with both size and complexity of the region. Con-sequently, it cannot be stored in advance. Compared to seed-fillalgorithms, scan-conversion algorithms are generally faster andrequire less or no additional working memory. In order to over-come the di ffi culties of seed-fill algorithms, di ff erent strategieslike [5, 6] try to re-model the region and so get a fast imagemanipulation, processing and displaying.Regarding scan conversion, well-known related to algorithmsrequire computationally expensive presorting and marking phasesbefore they compute the actual intervals of points contained inthe region. This holds even for simple concave polygons. Forcurved boundaries, the marking phase requires careful attentionto both geometric and numerical details to provide robust algo-rithms. Conventional filling of curved regions is based on algo-rithms for scan conversion of polygons, in which the end pointsof spans are incrementally updated, and then the intermediatepixels are filled. At some stage the intersection formula, de-rived in the continuous plane and usually computed using realarithmetic, has to be mapped to the discrete plane. This map-ping needs an implicit or explicit epsilon-test that may causeincorrect results. Many scan-conversion algorithms fail to han-dle complex objects correctly, while in principle seed filling ismore robust [1, 4].Levoy and Whitted suggest points rather than triangle meshesas display primitives [7, 8, 9]. Similarly, we propose pointsas low-level modelling primitives, and so get rid of the well-known discretisation problems of the polygon and of all gen-eral curved boundaries too. Points are building bricks of geom-etry discretisation, and they can also provide a common formatat the lowest level for both constructed and scanned geometricdata. Namely, for both computer graphics and image process-ing.
3. The mathematical structure
A dominant reason for which raster graphics algorithms failin robustness is the absence of formal specifications. Rastersystems are discrete devices, which generate image by display-ing the intensity value of each pixel in a finite two-dimensionalmatrix of pixels. But discrete definitions are implicitly assumedto be like their respective counterparts of the continuous Eu-clidean space. Commonly, a formal definition of interior / exteriorof digital objects is omitted. This can cause di ffi culties in dif-fering between the rules of odd-parity and nonzero windingnumber, and mainly in comparing di ff erent algorithms. Fig-ure 3 shows a typical ambiguity that arises when such terms aretreated only implicitly.Now we define some basic terms, and the first is our spaceof points:2 igure 3: Ambiguity regarding interior and exterior of a digital picture (exam-ple with a 12 ×
12 matrix of pixels).
Definition 3.1.
Given any two positive natural numbers N and M , a canvas in Z is the set { ( p y , p x ) : 1 ≤ y ≤ N , ≤ x ≤ M } ,where N × M indicates its dimension .In a computing context, a digital image is a two-dimensionalarray of values reflecting the brightness and colours of the pix-els. Definition 3.2.
Let S = S N × M be a canvas. For any f : S →{ , } , we say that the set f − (1) is a digital picture in the canvas.In Definition 3.2, our convention is to think of 0 as whiteand 1 as black. Therefore, the digital picture is a set of blackdots over a white background. As remarked in the introduction,our input is a digital picture f − (1), or just a matrix with en-tries 0 and 1. Our filling algorithm does not require any othermathematical expression for the function f . Definition 3.3. A discrete curve in a canvas S is an 8-connectedlist L : [0 . . . n ] → S . We say that L is monotone and closed when L ( i ) = L ( j ) ⇐⇒ i = j mod n . Definition 3.4.
Consider a digital picture P with its correspond-ing f : S → { , } . If any two points p , q ∈ f − (1) always admita discrete curve L : [0 . . . n ] → S with L (0) = p , L ( n ) = q and f ( L ( i )) = ∀ i , then we say that P is connected .These definitions are comparable to those given in works byRosenfeld and Kong [10, 11]. Notice that the same digital pic-ture can correspond to distinct discrete curves. By looking atFigure 3, the black pixels on either case form exactly the same digital picture P . However, we can track these pixels in dif-ferent ways. Each image comes from a di ff erent discrete curvethat represents P .Figure 4 helps understand the following definition: Figure 4: A digital picture with thickness varying from 1 to 3.
Definition 3.5.
Given p ∈ L ([0 . . . n ]), consider the biggest square Q such that p ∈ Q ⊂ L ([0 . . . n ]). Its dimension is k × k for acertain k ≥
1. Then k is the thickness of the discrete curve L atp . Definition 3.6.
We say that a discrete curve L is thin when ithas thickness 1 everywhere.In practice, Definition 3.6 can be replaced by a weaker con-dition: Definition 3.7.
An injective discrete curve L : [0 . . . n ] → S is locally thin when the following holds. There are intervals I ∪ . . . ∪ I k = [0 . . . n ] such that | I j | ≥ L | I j is thin, ∀ j ∈ { , . . . , k } .For instance, the black pixels in Figure 4 cannot representthe image of an L that is locally thin. We shall give more detailsin Section 5.Our purpose is to consider any digital picture P , solely re-stricted to Definition 3.4. For instance, take a more generalexample like in Figure 4. Now the question is: How to describeour input, a digital picture P as in Figure 4, in terms of a func-tion L in accordance with Definition 3.3?This question will be answered in Section 5. In fact, thatsection introduces what we call the Lego curve . Its set of pixelswill not always coincide with P . However, starting from theLego curve it is then possible to obtain a function L : [0 . . . n ] → S as in Definition 3.3, such that L ([0 . . . n ]) ≡ P .Notice that Definition 3.3 does not require a discrete curveto be simple . Namely, it can have self-intersections defined asfollows: Definition 3.8.
Based on Definition 3.3, we say that L has • a self-crossing when there are two disjoint intervals I , J ⊂ [0 , n ], | I | = | J | =
2, such that L ( I ) , L ( J ) and all pixels L ( I ∪ J ) are 8-connected. • an overlapping when we have I , J ⊂ [0 , n ], min {| I | , | J |} ≥
2, such that I ∩ J is unitary, and either L ( I ) ⊂ L ( J ) or L ( J ) ⊂ L ( I ).If any of these two cases occur, then L has a self-intersection .Otherwise, it is a simple curve.Examples in R can clarify Definition 3.8. For instance, theso-called Bernoulli’s Lemniscate is a closed curve, of which3he parametric equations are x ( t ) = t / (1 + sin t ), y ( t ) = sin 2 t / (1 + sin t ), with t ∈ [ − π, π ]. Its trace is ∞ (the symbol ofinfinity). In this case we just have a self-crossing at the origin.For the curve (0 , sin t ), t ∈ [0 , π ], overlapping occurs at t = t = π/
2. Notice that t could vary in a bigger domain, butwe always take the smallest for which the curve is closed.The reader must have noticed that we treat some technicalterms as synonyms: point and pixel, discrete and digital, etc.In fact, this started to happen right at Definition 3.1, becauseour constructions are devoted to both theoretical and practicalcontexts. The reader is free to choose any of these contexts, andthen think in accordance with the corresponding terms.
4. The main programme
In this section we discuss the algorithm of our main pro-gramme, named loci.m after a mnemonic to “location of curveinterior”. In Section 7 the reader will find details about down-loading and running it. The programme loci.m is an imple-mentation of the “Filling Up Algorithm” (FUA). Before dis-cussing FUA, it is important to explain some hypotheses underwhich we claim that loci.m will always give a correct answer.We recall the di ff erence between two similar terms: con-nectedness and connectivity . Connectedness of a digital picture P is a global property implied by Definition 3.4. Connectivityis a local property that applies to each pixel with respect to theones that surround it in a digital picture P .Namely, given any p ∈ P , if either p ± (1 ,
0) or p ± (0 , P , then it has connectivity 4 with p . If any of p + ( ± , ±
1) is in P , then it has connectivity D with p . Fi-nally, when both 4- and D-cases apply to p , we say in short thatsurrounding pixels have connectivity 8 with p .Now we summarise the three conditions on L that will makeFUA correct. The third condition will be explained in the se-quel: C1 . L is locally thin; C2 . L can only be tracked horizontally and vertically. Inother words, L : [0 . . . n ] → S is such that L ( i ) and L ( i +
1) are4-connected ∀ i < n ; C3 . L is “spike-free”.Before discussing C3 , we need to introduce a concept called i − o pixels (see Definition 5.1) which is analogous regardingscan-conversion algorithms to the usual marking phase requiredto compute the intervals of points contained in the region.From upside down, the scanning of a digital picture givesan N × M matrix of black and white pixels. Typical rows arerepresented in Figure 5. We track each row from the left to theright. In this process, i -pixels indicate the entry to and o -pixelsthe exit from P . When a pixel is simultaneously i and o , wemark it with an x .In Section 5 we are going to introduce CoTRA, Connectiv-ity and Thickness Reduction Algorithm. CoTRA produces our“Lego curve” L .Figure 6 gives an example of the input (top) and the output(bottom) by CoTRA applied to a digital picture P . In fact, P i i i ii i ii i i xo o o ooo o o oo Figure 5: Some typical rows of a digital picture and their entry-exit pixels.
Figure 6: Input (top) and output (bottom) of the Connectivity and ThicknessReduction Algorithm (CoTRA). is a detail of Figure 9 obtained from the drawing of a non-self-intersecting polygon in Xfig. The spurious self-intersection atthe left middle occurs due to discretization errors when movingfrom the ideal mathematical model to the digital picture.On top, P is given as a set of black pixels in the white can-vas. At the bottom, we mark blue pixels that are originallyfrom P , and i − o pixels in magenta. The blue and magentapixels form the sought after Lego curve L . However, noticethat not all pixels were marked by CoTRA. For example, closeto ( y , x ) = (130 ,
70) it found a stretch interpreted as a self-intersection of P . Moreover, close to (120 ,
90) and (150 , (see Section 6 for details).In Section 5, we formalise “interior” of P by means of Def-inition 5.3.Now the reader may be curious about the turning points ofFigure 6. Of course, the one close to (120 ,
90) could also havethickness 1 and look like a thinner “spike”. In technical terms,we have
Definition 4.1.
Let L : [0 . . . n ] → S be a monotone discretecurve with L ( i ) = ( L y ( i ) , L x ( i )) ∀ i . For all − < k <
3, if thereis i such that | L x ( i + k ) − L x ( i ) | < L y ( i ) ≥ L y ( i + k )or L y ( i ) ≤ L y ( i + k ), then L has a vertical spike at i . If there is i such that | L y ( i + k ) − L y ( i ) | < L x ( i ) ≥ L x ( i + k ) or L x ( i ) ≤ L x ( i + k ), then L has a horizontal spike at i .Handling a picture with spikes proved to needlessly increasethe complexity of FUA. Indeed, we are going to obtain the inte-rior of P as the interior of L minus the set P itself. CoTRA wasprogrammed in order to eliminate spikes and then correct FUA.As in any improvement, CoTRA adds a computational cost.But the users can choose: if the first output is not satisfactory,they call CoTRA for a second answer.Now we present the basic algorithm to fill up P by markingits entry-pixels (see Algorithm 1). Algorithm 1
The “Filling Up Algorithm” (FUA)
Input:
Digital Picture P = f − (1) ⊂ img (: , : , Output:
Interior of P for i = img (: , , − do bps ← Find ioPixels for k = bps ) − do if img ( i , bps ( k ) : bps ( k + ,
1) is an extreme then shorten bps for k = bps ) − do img ( i , bps ( k ) : bps ( k + , = i − o pixels of a local maximum (orminimum) stretch of the picture. This is because the statusof being inside / outside the picture does not change at passingthrough such stretches.For the rest, lines 6-7 simply mark the actual interior pointsof the i th row in yellow (R = = =
5. Definition of the Lego curve
At any row of pixels, the digital picture has white and blackones. Fix any row and consider the pixels in it. Up to scal-ing, each of then has an x -coordinate that ranges from a naturalnumber to its successor. Definition 5.1.
Given a row R of black and white pixels, wesay that p ∈ R is an i-pixel when p is white and p + p ∈ R is an o-pixel when p is white and p − p is simultaneously i and o , we call it an x-pixel .The letters i and o stand for the process of getting inside / outside the black horizontal segments from Figure 4, as de-picted in Figure 5. Definition 5.1 implies that the i - o sequenceis always alternated, if not empty. By adding a frame of whitepixels around the picture, we guarantee the sequence to beginwith i and end with o . We have added this frame in Figure 7.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 2401 02 03 04 05 06 07 08 090102030405060708091011
Figure 7: A digital picture and its corresponding track of the Lego curve.
We recall Definition 3.7. In Figure 7, the black pixels forma picture P . Notice that P cannot be the image of a discretecurve that is locally thin. This is only possible if we remove atleast 9 pixels from P , namely (7 ,
7) and (8 , − Definition 5.2.
Consider a pixel canvas S with dimension N × M , and also a digital picture P contained therein. Suppose P is surrounded by the white pixel frame F : = { , N } × [1 , M ] ∪ [1 , N ] × { , M } . In this case, the exterior of P is the set E , F ⊂ E ⊂ ( S \ P ), such that for any two points p , q ∈ E thereexists a discrete curve L : [0 . . . n ] → ( S \ P ) with L ( i ) and L ( i +
1) 4-connected ∀ i < n . Definition 5.3.
Under the hypotheses of Definition 5.2, if E isthe exterior of P , then its interior is S \ ( P ∪ E ). Definition 5.4.
Let P be a digital picture and p a correspond-ing i or o -pixel, such that p belongs to the exterior of P . If P has two pixels that are D-connected to each other, and also 4-connected to p , then we say that p is an L-pixel of P .In Definition 5.4, if we write the pixel coordinates as (line,column) in Figure 7, then P has exactly the following L-pixels:(2 , , , , , ,
20) (6 , , , , , ,
9) and (2 , Definition 5.5.
Given a digital picture P with exterior E , let L be the set of L-pixels of P that are 4-connected with E . The Lego curve of P is a digital curve of minimal length that satis-fies C2 , and has an exterior E ′ ⊂ E such that E \ E ′ ≡ L .We conclude this section by remarking that Definition 5.5 does not require the Lego curve to be locally thin, not evenfree of self-intersections. However, they can only occur in thespecial form of overlapping , not self-crossing . The FUA doeswork in the former case, but can fail in the latter.5 . Existence and uniqueness of the Lego curve In this section we present our main theorem, which trans-lates to the open source code lego.m . We have written ourproof like a pseudocode . Theorem 6.1.
Any digital picture P admits a unique corre-sponding Lego curve L. Proof: We are going to construct a list L : [0 . . . n ] → Z . Take P as in Definition 5.2. Its pixels will be called black . However,we shall say that a pixel is white exactly when it belongs to theexterior of P . In what follows, both the words increase / decrease mean “by one”.Consider the i - o pixels of P with least ordinate y . Amongthem, name p the one with the least abscissa x . It has coordi-nates ( p y , p x ), 1 < p y < N and 1 < p x < M . We start with n = L (0) = p or L (0) = p + (0 , p is an L-pixel or not, respectively. At any step, theconstruction interrupts when L ( n ) coincides with L (0).Step I. Increase n and define L ( n ) as follows:(a) if at the southwest of L ( n −
1) we have a black or anL-pixel, then it will be L ( n + L ( n ) will be thesouthern pixel;(b) otherwise, if the south is black or an L-pixel then it willbe L ( n );(c) if neither (a) nor (b) then take L ( n ) = L ( n − + (0 , L ( n ) has just been defined.If L ( n +
1) is already defined, interchange the roles x ↔ y by rotating the canvas counterclockwise. Increase n , redefine p as L ( n ) and repeat Steps I-II.If L ( n +
1) is not defined yet, then we look at L ( n ) y . In thecase it is L ( n − y +
1, redefine p as L ( n ) and repeat Steps I-II.In the case it is L ( n − y , we can have(a) L ( n ) and L ( n ) + (1 ,
0) both white; this situation is predictedin Step III.(b) L ( n ) + (1 ,
0) is black or an L-pixel; take L ( n + = L ( n ) + (1 , n , redefine p = L ( n ) and repeatSteps I-II.(c) Otherwise redefine p = L ( n ) and repeat Steps I-II.Step III. In the present orientation you cannot increase L ( n ) y any more. Then interchange the roles x ↔ y by rotating thecanvas clockwise. Clear L ( n ), decrease n and repeat Steps I-II. Note: if this step happens three times consecutively, then P consists of a single pixel. This case can be excluded before-hand.Remark: If P does not consist of a single row of black pix-els, then there will be a least value n ≤ n such that L ( n ) has agreater ordinate than L (0) with respect to the initial orientation.The above construction is in fact what we called CoTRA.But the reader will notice a gap in it, as illustrated in Figure 8. It is easily identified by “trapped” 4-connected L-pixels. Com-pare it with Figure 6. But we easily go round this gap by identi-fying non-marked L-pixels and locally repeating the above con-struction. Figure 8: False self-intersections interpreted by CoTRA.
However, our list L may still have spikes. In this case, werecall Section 4 and see that C2 trivially holds for L . Regarding C1 , it easily follows from C2 and C3 . In order to get C3 , wehave to adapt the above construction as follows: if the roles x ↔ y are interchanged consecutively twice, then lop o ff somenewly defined L ( n ) by checking if either x - or y -coordinatesremained constant.Of course, there remain two important questions that werenot answered yet. The first is: why does the construction everfinish? In other words, why does L ( n ) coincide with L (0) for acertain positive n ?Notice that any two 8-connected pixels of P , that are bothconnected to E , can either project onto Ox or Oy injectively. Westart with Oy and keep on using it until injectivity fails. Thenwe change to Ox and do the same to this new axis. This isexactly when the roles x ↔ y are interchanged in the aboveconstruction. It begins with a local graph x = x ( y ) so that pixelsright under the graph belong to the exterior E of P . As a matterof fact, this reasoning holds for the whole construction. Thusno pixel of E , that is right under a local graph, is left out. Hence L ( n ) will eventually coincide with L (0) for a certain n .By the way, the same reasoning explains why L containsthe set L from Definition 5.5. This partially answers the secondquestion: why L fits Definition 5.5? It remains to check thatit has minimal length among any other curve L ′ with exterior E ′′ ⊂ E and E \ E ′′ = L .We know E = E ′ ˙ ∪L = E ′′ ˙ ∪L , thus E ′ = E ′′ . Suppose thereexists p ∈ L \ L ′ . Consequently, p ∈ P and is 4-connected with E . Indeed, it cannot be only D-connected because then an L-pixel would have replaced it in the above construction. Either p is 4-connected with E ′ = E ′′ or p is simultaneously 4-connectedwith two di ff erent pixels q , r ∈ L . This latter is the only possi-bility for p < L ′ , since p < L and both L , L ′ have to satisfy C2 .But then q , r ∈ L ′ have to be connected by a longer path thanthrough p . Hence L ′ cannot have minimal length. q.e.d.We finish this section by stating and proving a corollary,which is a weaker version of the discrete Jordan curve theorem .This is because we use an additional hypothesis H , describedbelow. In Appendix A we discuss some other theoretical re-sults that can be derived from ours.6 orollary 6.1. Let C : [0 . . . n ] → S be a simple closed curveand denote its image by P . Suppose S and P are such that Def-inition 5.2 applies. Additionally, the following holds:H: Any p ∈ P is connected to exactly two other elements of P .Hence S \ P = E ˙ ∪J , where E is the exterior of both P and C ,and J the interior of both P and C . Moreover, E is connected,and the same holds for J . Proof: Since any p ∈ P is connected to exactly two other ele-ments of P , then C is thin and spike-free. The same will happento L , constructed from P by means of Theorem 6.1. Moreover, L can only be tracked horizontally and vertically. Hence, itis not di ffi cult to conclude that its image P ′ has the followingproperty: S \ P ′ = E ′ ˙ ∪J ′ , where E ′ is connected, and the sameholds for J ′ . But E = E ′ ∪ L , where L is given by Defini-tion 5.5. Moreover, E is connected. Now, J = S \ ( P ∪ E ) = S \ ( P ′ ∪ E ′ ) = J ′ . Therefore, J is also connected. q.e.d.Notice that neither lists of Figure 3 fulfil the hypothesis ofCorollary 6.1.
7. Results
This present section is like a manual to the programme.Download legoloci.zip from https://sites.google.com/site/aefabris/codes and extract it in a folder. You will get loci.p , lego.p andsome test-files. In order to draw our figures, we have chosenXfig for Linux Ubuntu 12.04. They all have extension FIG,and the reader can either change them or even create new tests.However, our programmes do not read FIG-files. Wheneveryou create a figure, please export it in a format like TIF or JPG.We shall make the programmes loci.m and lego.m avail-able in future. For the time being, users can run the p-codes inMatlab. To test the robustness of our algorithms, one can eitheredit the presented test files or even create new ones.The source codes run both in Matlab and Octave. Table1 shows the average performance of these programmes in eachsoftware, providing you use 4GB of RAM, microprocessor IntelCore i5 3.2GHz, and operational system Linux Ubuntu 12.04.Table 1 refers to Matlab 7.8.0 (R2009a) and Octave 3.2.4 (R2009).All test files have extension JPG, M stands for Matlab and O forOctave.Type loci at the Matlab prompt. You will be asked to givea filename, for instance test3.tif . Please write the full name with extension. After pressing the Enter-key, loci will displaythe figure and also its inside according to FUA. The elapsedcomputational time is printed in the Matlab terminal window.It counts all commands that come right after having stored theimage in the variable img (see Algorithm 1), and right after having displayed its interior on the screen.Now the user gets the following message: Try CoTRA? Yes = 1; No = any key
Table 1: Comparison to Computational Times in Seconds loci-M lego-M loci-O lego-O test1 0.30 0.80 0.34 2.18test2 0.36 1.17 0.51 4.03test3 0.29 0.63 0.34 2.91test4 0.29 0.76 0.39 3.46test5 0.56 2.65 1.25 8.40test6 0.30 0.89 0.41 2.32test7 0.36 0.59 0.43 7.51test8 0.30 0.93 0.39 3.86By choosing 1, CoTRA is called to create the Lego curve asin the proof of Theorem 6.1. A new figure will be displayed, butnow its inside matches Definition 5.3. Once again, the elapsedcomputational time is printed in the Matlab terminal window.It counts all commands the same way as done in the previouscase.Now we illustrate some outputs.
Figure 9: Filling up a challenging picture with CoTRA.
At the Introduction, we mentioned that the marking phaseof scan conversion requires careful attention to both geomet-ric and numerical details. Figure 9 left was obtained by draw-ing a polygonal test4.fig in Xfig. Like many other drawingsoftwares in computer graphics, the continuous mathematicalstructure is then discretized as a digital picture.A detail of Figure 9 is zoomed in Figure 6, which shows aself-intersection that occurs when we move from the ideal sim-ple (i.e. non-self-intersecting) continuous polygon to the digitalpicture : the digital picture test4.jpg is obtained by export-ing test4.fig to the JPG-format. As commented in Figure6, CoTRA finds a self-intersection at ( y , x ) = (130 , test4.jpg is filled in accordance with Definition 5.3.Figure 10 shows that parts with empty or non-connectedinteriors are correctly detected.
8. Conclusions
In this paper, the di ffi culties in discretisation are acknowl-edged and tackled by casting the problem to be solved as adiscrete integer problem from the very outset. Integer spaces7 able 2: Comparison to Seed-fill, Scan-conversion and Our approach Seed-fill Scan Conversion Our approach
Input data scanned / math defined math defined scanned / math definedImplementation na¨ıve recursive: simple complex simpleModelling space screen space object space screen spaceBoundary modelling points / polygons polygons pointsAlgorithm integer / floating point floating point integerApplications areas image processing computer graphics computer graphics andCG painting systems image processingAdditional memory very large stack small / large stack no / small stackHard / software design unsuitable hardware suitable hard / software suitable hard / softwareDevice dependency requires GetPixelValue device independent device independentTheoretical background simple simple relatively complexRobustness to be more robust most algorithms fail robustE ffi ciency relatively slow most are fast relatively fast Figure 10: Examples of degenerate regions: empty and non-connected interiors. are more amenable to analysis and proof. There the commonlyemployed geometric operations are translated into discrete ver-sions, which allow a better control of robustness.This approach enables the development of an integer-onlyalgorithm, of which the main characteristics are: simple struc-ture, no special cases to forecast, no extra memory allocation,and proved robustness. Namely, it can handle arbitrarily com-plex inputs. The source codes loci.m and lego.m were im-plemented without either recursive calls or repeat / while-loops.Only for-loops were allowed in the programmes. These charac-teristics can be forwarded to hardware implementation.In fact, the main purpose of our work was to make faultlessand fast algorithms that could also be implemented in hardware.Regarding the e ffi ciency of our present implementations,FUA alone has linear complexity with respect to N · M . Ourobservations regarding CoTRA on numerous test scenes sug-gest quasi-linearity, but before any formalisation we need tocheck for improvements. Indeed, although CoTRA is relativelyfast, we could improve its speed by exploring coherence testsin several ways.Moreover, FUA and CoTRA require input data that can ei-ther be scanned or constructed by a mathematical model. There-fore, these algorithms can be interactively used for applicationsin both Image Processing and Computer Graphics.See Table 2 for a summary of seed-fill, scan-conversion andour approach. Appendix A. Some Theory After This Work
Any seed-fill algorithm easily finds the exterior E of a pic-ture P ⊂ S directly from Definition 5.2. Hence we get the inte-rior as S \ ( P ∪ E ). But the computational cost is huge becauseseed-fill approaches are recursive.Theorem 6.1 constructs the Lego curve, which after somepruning is then applied to FUA (see Section 6).However, the Lego curve is much more than just a way toget correct digital answers. It can help prove theorems in the continuous
Euclidean plane. For instance, consider this strongversion of Jordan’s theorem, called
Sch¨oenflies Theorem : Theorem Appendix A.1.
Let S be the unitary circumferencein R centred at the origin. Let f : S → C ⊂ R be a homeo-morphism between S and C . Hence, there exists a homeomor-phism F : R → R such that F | S = f . An immediate consequence of Theorem Appendix A.1 isthat the interior of C , namely the bounded component of R \ C ,is simply connected. It is a powerful result that, however, doesnot seem to admit a proof accessible to graduate students. Buthere we outline some ideas of which details could be carriedout in future works.As depicted in Figure 7, we could get a continuous Legocurve out of any grid in which the original C is covered byblack pixels. Ideally, resolution becomes infinite when the pixeldimension (1 / n ) × (1 / n ) goes to zero, where n is a positive in-teger. For each n , we get the continuous Lego curve L n thatcorresponds to C . It is not di ffi cult to describe f n : S → L n such that f n → f , because L n is tracked only horizontally andvertically. This same reason allows us to construct a homeo-morphism F n : R → R , with F n | S = f n . Finally, a limitprocess will then result in the general proof. References [1] Schirra S. How reliable are practical point-in-polygon strategies? In:Algorithms-ESA 2008. Springer; 2008, p. 744–55.