Abstract:
A method and apparatus for determining a predominant angle of orientation of an image with respect to a reference angle. A file of picture elements is generated which depicts the image with respect to the reference angle. The picture elements are projected onto a plurality of contiguous segments of imaginary lines at selected angles across the file. Each imaginary line is perpendicular to its associated direction of projection. The number of picture elements that fall into the segments for each projection are counted. An enhancement function is applied to the segment counts of each projection. The projection that generates the largest value of the enhancement function defines the angle of orientation of the image. The position of a document scanner or the document itself may be rotated to compensate for the detected skew.

Description:
TECHNICAL FIELD 
     The invention relates to the field of document reproduction, optical character readers, optical scanners, document readers and the like. In particular, the invention relates to the detection of and correction for rotational error (skew) between the dominant orientation of lines of printed objects, such as characters, of a document and a reference line observed by a reader, or the like, as being zero rotational error. 
     BACKGROUND OF THE INVENTION 
     A fundamental and pervasive problem arises in the art of automatic document analysis, character/object recognition and related fields. The problem is the recognition and correction for skew in printed documents during the automated process. By skew, it is meant the angle between the dominant orientation of lines of characters or other textual objects of a document and a reference line observed by a reader, or the like, as representing zero angular error. Exemplary of the actions that are performed during document analysis is the segmentation of images of printed documents into blocks and lines of objects, such as characters. Known segmentation methods include those called top-down methods and bottom-up methods. Top-down methods characteristically operate by estimating some global properties of an image and by using the properties to guide segmentation into local regions whose local properties are estimated in turn. Bottom-up methods of segmentation characteristically operate by first clustering characters into lines, then lines into paragraphs, and so on. Unfortunately, the top-down methods tend to be excessively sensitive to non-zero skew. 
     A representative bottom-up method is described by Nagy, G. et. al. in an article entitled &#34;Document Analysis with an Expert System,&#34; Proceedings, Pattern Recognition in Practice, Amsterdam, 1985. This bottom-up method relies on good skew alignment, with skew angle restricted to no more than a few degrees. However, while bottom-up methods are less sensitive to skew than top-down methods, the bottom-up methods are generally slower and suffer from other problems unrelated to skew sensitivity as well. 
     Hashizume, Yeh, &amp; Rosenfeld, in an article entitled &#34;A Method of Detecting the Orientation of Aligned Components&#34;, Pattern Recognition Letters, 1986, pp. 125-132, describe a skew determining method based on the premise that objects, e.g. characters, are often closer to one another along a dominant line orientation than in other directions. This technique computes the nearest neighbor of each object and connects each neighboring pair with a straight line segment. A PG,3 histogram of the orientations of these line segments is computed. The histogram may have a strongly-marked peak at the dominant skew angle. The skew angle is computed as an average of values near the peak. Among the known examples reported using this technique, the average error was 1.5 degrees and the worst 4.1 degrees. 
     W. Postl describe experiments with two methods of skew determining in a paper &#34;Detection of Linear Oblique Structures and Skew Scan in Digitized Documents&#34;, Proceedings, Eighth International Conference on Pattern Recognition, Paris, October 1986, pp. 687-689. The first method applies the discrete two-dimensional Fourier transform to an image plane and examines a half plane of the power spectrum coefficients. The technique assumes an orientation angle and measures the energy in spatial frequencies at that orientation angle. The accuracy obtained with this method is not known. The second method similarly hunts for the maximum of a measure over a range of angles. The integral density of points is computed along assumed scan angles. For each pair of neighboring scan lines, the difference of their densities is computed. Finally, the sum of squares of these differences is computed. 
     Rastogi &amp; Srihari describe a method using a Hough transform in an article, &#34;Recognizing Textual Blocks Using the Hough Transform&#34;, Department Computer Science, University of Buffalo (SUNY), 1986. For each angle in a discrete representation of Hough space, the number of large &#34;low-high-low transitions&#34; is counted, and the maximum count is interpreted as identifying the dominant skew. In the five examples shown in the paper, skew angle was coarsely quantized in increments of 15 degrees. 
     While the above methods operate satisfactorily in some contexts, they are both slow and complex, and give coarse estimates of skew. 
     SUMMARY OF THE INVENTION 
     The invention is a method and apparatus for determining a predominant angle of orientation of an image with respect to a reference angle. A file of picture elements is generated which depicts the image with respect to the reference angle. The picture elements are projected onto a plurality of contiguous segments of imaginary lines at selected angles across the file. Each imaginary line is perpendicular to its associated direction of projection. The number of picture elements that fall into the segments for each projection are counted. An enhancement function is applied to the segment counts of each projection. The projection that generates the largest value of the enhancement function defines the angle of orientation of the image. 
     In a preferred embodiment, projections are first taken at a plurality of relatively coarse angles on both sides of the reference angle. This results in a first coarse estimate of the correct orientation. Then, projections are taken at more refined angles on both sides of the coarse first estimate. Also, the image file is compressed before further processing is performed, although this is not essential. The compressing involves performing a connected components analysis on the raw image file to locate individual image objects and by representing each object with one or more (preferably one) picture element. Processing operations are then performed on the compressed file. 
     The method works well for many page layout styles, including multiple columns, sparse tables, variable line spacings, mixed fonts, and a wide range of font styles and text sizes. Runtime of the method by a computer is proportional to the function n log (l/r), where n is the number of characters in the document and r is the angular resolution desired. A resolution of two minutes of arc is routinely achievable. 
    
    
     BRIEF DESCRIPTION OF THE DRAWING 
     FIG. 1 shows a simplified block diagram of a document scanner, including a scanning platform, a rotatable scanning camera and controller for compensating for document skew; 
     FIG. 2 shows a slightly different embodiment of the system of FIG. 1, in which the platform is rotatable; 
     FIG. 3 conceptually illustrates the technique of the invention as aid to understanding; and 
     FIGS. 4 through 8 are flowcharts disclosing the principal steps of the invention. 
    
    
     DETAILED DESCRIPTION 
     FIG. 1 shows an illustrative document reader used to practice the invention. A document is placed automatically or manually on a transparent reading platform 10 where it is examined by a scanning device 12 in response to a READY signal arriving at a controller 14. The output of scanning device 12 is a stream of on/off pixels which reflect the document image on the platform 10 to the desired degree of resolution. Controller 14 processes the output from the scanning device in accordance with the algorithm described herein to determine the skew angle of the document. A signal is then applied to a motor 16 to rotate the scanning device 12 by an amount equal to the skew angle, thereby correcting for the document skew. Alternatively, motor 16 can be arranged to rotate the reading platform 10 rather than the scanning device 12 if desired, as shown in FIG. 2. 
     FIG. 3 pictorially illustrates the technique used to find the skew of a document. On the left side of FIG. 3 is shown a document skewed by some angle A with respect to an angle of reference observed by the scanner 12. The initial image file transmitted from the scanner 12 to the controller 14 reflects this skew. After correction for skew, the document has a skew angle substantially equal to zero within any desired tolerance down to about 2 minutes of arc. 
     The skew finding technique consists of first performing a connected components analysis on the image file to locate separate printed objects, such as characters, of the document. FIG. 3 shows two such objects &#34;B&#34; and &#34;C&#34; as part of the image file. Connected components are called &#34;blobs&#34; in various comments of the source code, which is discussed below and disclosed in Appendices 1 and 2. Each connected component is replaced with a single reference point in the file. This is illustrated in FIG. 3 by a dot at the lower rightmost point of the connected components B and C. The lower right corner of a connected component is preferably chosen for three reasons. First, in English text, character baselines are usually more uniform than top-lines due to the relatively low frequency of descenders. Second, if skew angle is not far from a zero reference angle, then any bottom point is a good approximation to the true base height of the character. Finally, the lower right corner is easy to compute. However, other reference points, such as the lower midpoint, may also be selected. 
     The purpose of a connected components analysis is to determine those groups of black pixels of the image file that touch one another directly or indirectly, such as an isolated character. The source code for performing a connected components analysis is not disclosed in the source code. This is because the code disclosed is tailored for a mainframe environment in which the connected components analysis is performed separately. Once the connected components analysis has been performed, the reference points replace the associated connected components in the image file for further processing. The connected components analysis is a conventional technique and is not described further herein. The reader is referred to &#34;Connected Components in Binary Images: the Detection Problem&#34;, Ronse, C. &amp; Devijver, P., Research Studies Press (Letchworth, Hertfordshire, England, 1984), for details of one technique of performing such an analysis. 
     The reference points are projected at a number of angles into a plurality of contiguous segments, which we call &#34;bins&#34;, which partition an imaginary accumulating line. For each projection, the number of reference points that fall into each bin are counted. An example projection at 30 of FIG. 3 is at zero degrees with respect to the skewed document. The bin counts are shown to the right of 30. A second example projection is shown at 32. This projection is taken at the skew angle A. The associated bin counts are shown to the right of projection 32. As illustrated by FIG. 3, at angles close to the correct skew angle, reference points that are in alignment accumulate in a small number of bins, so that the distribution of counts concentrates more into extremely large and small values. Methods for computing and exploiting a numerical measure of this behavior of the distribution of counts are important features of this invention. 
     If the size of the bins (their length of their segments along the imaginary accumulating line) is too small, then, even at correct alignment, noise can scatter the projected points so that only a few of the bin counts are larger than one. If the bins are too large, then small angular changes have little effect. The document or the capabilities of the particular scanning system should be taken into account. Preferably, the bin size is chosen to be a fraction of the smallest point size expected to occur. For a system that handles text down to about 6 points, the bin size is preferably chosen to be 1/3 of the 6-point text (5 pixels at a digitizing resolution of 300 pixels/inch). 
     A superlinear enhancement function, described below, is applied to each set of bin counts. As illustrated in FIG. 3, the enhanced distribution forms a dominant peak which is at its largest value at the correct skew angle. It is this peak that is used to determine the correct skew. 
     The controller 14 may be implemented in any number of ways. For example, it might be a circuit composed of discrete components, a custom integrated circuit chip, a microprocessor chip driven by firmware or software or a more conventional type of computer. For this discussion, it is assumed that controller 14 is a microprocessor driven by firmware contained in a ROM. The program contained within the firmware is disclosed below with respect to the flowcharts in FIGS. 4 through 8 and source code listings in Appendixes 1 and 2. The source code is written in the C programming language. Appendix 1 contains the code for the header information required by the C language and the code for the main flow of the program. Appendix 2 contains the remaining code. This language is described in many text books, including The C Programming Language, Kernighan and Ritchie, Prentice-Hall, Inc. (1978). Only the major aspects of the source code will be explained in detail, as any programmer skilled in the C language is able to discern the remaining details from the code. The source code as disclosed in the Appendixes is arranged for execution on a mainframe computer under the control of an operator at a terminal. It will be discussed in this context for consistency. Modifications to the source code to adapt it to the environment of FIGS. 1 and 2 will be obvious to a skilled programmer. 
     Every C program is composed of a main function MAIN() and usually a number of other subfunctions or subroutines. A flowchart of MAIN() is shown in FIG. 4. The corresponding source code of MAIN() is disclosed at lines 262 through 290 of Appendix 1. At step 40 of FIG. 4, MAIN() first calls a subroutine PARSE --  ARGS(). PARSE --  ARGS() examines an input command describing a file of reference points to process for skew. The reference points may be the black picture elements of the entire, or may have been selected by a connected components analysis. The command may also contain various optional arguments to select parameters such as skew tolerance limits. PARSE --  ARGS() is disclosed at lines 143 through 260 of Appendix 1. Once the file of reference points to be processed is identified, MAIN() executes a subroutine READ --  POINTS() to input that file at step 42 of FIG. 4 into main memory for processing. READ --  POINTS() is disclosed at lines 292 through 326 of Appendix 1. 
     Step 44 calls a subroutine FIND --  SKEW(), which determines the skew of the set of reference points, as discussed below. FIND --  SKEW() returns a value indicating the skew angle to MAIN() in variable &#34;skew&#34; at line 286 of Appendix 1. Step 48 then compensates for the skew. This compensation takes the form of a mere printout at line 287 of MAIN(). In the system of FIGS. 1 and 2, the printout command &#34;fprintf&#34; at line 287 would be replaced with an output command to rotate the document platform 10 or the scanner 12. 
     The algorithm can be summarized as follows. It first probes at angles of coarse resolution until it detects the characteristic shape of the principal peak, and then refines the location of the peak&#39;s maximum by probes at finer resolution. Each probe computes the skew at a given angle, maintains a history of the probes, sorted by angle, and remembers the angle in variable SK.BEST.T whose skew value is largest. In the source code shown in Appendix 2, the user can suggest a preferred starting angle (variable SKEWO) (default is 0 degrees), an initial coarse resolution (variable RESO) (default is 0.5 degrees), and the finest resolution desired (variable RESF) (default is 0.0167 degrees). Probes are made at an initial angle SKEWO and at increments away from the initial angle at RESO, until a pattern emerges in which the present best angle SK.BEST.T is bracketed on both sides by at least three probes whose enhancement function values rise monotonically towards it. This completes the coarse location of the peak. 
     The location of the peak is then refined by an iterative procedure, each step of which is as follows. Below the present angle SK.BEST.T, a monotonically increasing subsequence of probes is selected whose enhancement function values rise monotonically. From this set of probes, no fewer than 3 and no more than 5 are selected to represent the &#34;left slope&#34; of the peak. Similarly, above the angle SK.BEST.T, a monotonically increasing subsequence of probes is selected whose enhancement function values fall monotonically. From this set of probes, no fewer than 3 and no more than 5 are selected to represent the &#34;right slope&#34; of the peak. Initially, those nearest to the present angle SK.BEST.T are chosen, but for later iterations (k), as probes cluster closely about the angle SK.BEST.T, the nearest k probes on each side of SK.BEST.T are ignored and farther ones chosen. These better describe the overall shape of the slopes near the peak. Smooth approximating functions are fitted to the left and the right slopes. The intersection of these approximating functions is computed and becomes the center of a new set of probes at a new, finer resolution C --  RESOL=C --  RESOL/2. The above described step is iterated until C --  RESOL falls below RESF. When this occurs, SK.BEST.T, the angle of the best probe so far, is returned as the PEAK angle. 
     FIND --  SKEW() is flowcharted in FIG. 5. The source code is shown at lines 528 through 679 of SKEWLIB.C in Appendix 2. Step 50 of FIG. 5 establishes a memory array into which angular projections across the document file can be made. This occurs beginning at line 559 of Appendix 2. Initially, a coarse resolution probing of the document file at five initial probing angles is performed. This is indicated as step 52 in FIG. 5 and begins at line 600 of Appendix 2. The actual probing is performed by a subroutine PROBE(), which is illustrated in FIG. 6. The source code for PROBE() is shown in Appendix 2 at lines 495 through 526. PROBE() is called five times for the initial angles at lines 606 through 610 of Appendix 2. The initial angles include one at SKEWO degrees with respect to the document file and four other at coarse angles spaced at RESO degree increments on either side of SKEWO. Step 60 of PROBE() executes a subroutine PROJECT(). PROJECT() is shown at lines 235 through 459 of Appendix 2. PROJECT() performs a projection at an angle contained in a variable &#34;SKEW&#34;. The projection occurs at lines 235 through 432 of Appendix 2. The bin counts are accumulated in SK.PROJ at lines 332 through 361. 
     An enhancement function is applied to the bin counts at lines 416 through 418. Given a set of bin counts c(i), where i is an index ranging from one to the number of bins, the enhancement function computes the sum of E(c(i)) over all i, where the function E(c) is a &#34;superlinear&#34; function of c. That is, the value of E(c) grows asymptotically faster than that of any linear function of c. Many such functions exist which appear to work satisfactorily in practice, for example, E(c)=c log c, E(c)=c*c, and E(c)=c*c*c. Of these, E(c)=c*c is preferred, however, because it is well-motivated theoretically and it is relatively easy to compute. 
     After the initial five projections are obtained, a subroutine INFER --  PEAK() is iteratively called at line 614 of Appendix 2. This is shown at step 54 of FIG. 5. The iterations are symbolized by the flow line 56. INFER --  PEAK() is repetitively called, each time with a new incremental angle RESO degrees removed from the last call, until enough probes have been obtained to define two slopes of the peak. 
     After the coarse location of the peak is found, this location is refined by a series of iterations of probes taken at finer resolution. These iterations occur at lines 621 through 668 of Appendix 2. As part of these iterations, PROBE() and INFER --  PEAK() are iteratively executed with smaller incremental angles C --  RESOL to further refine the search around the skew angle SK.BEST.T. This is shown as step 58 and flow line 60 in FIG. 5. Eventually, FIND --  SKEW() returns with the best found skew angle in the variable SK.BEST.T. This occurs at line 678 of Appendix 2. 
     The flow chart of INFER --  PEAK() is shown in FIG. 7. The code is shown at lines 90 through 233 of Appendix 2. At steps 70 and 72 of FIG. 7, the representative probes for the left and right slopes are selected (Appendix 2, lines 132-147 and 148-169, respectively). Using this information, a least-squares fitting procedure is used at step 74 (Appendix 2, lines 170-217) to approximate the left and right slopes. The intersection point of the left and right slope approximating functions is calculated at step 76 (Appendix 2, lines 218-231). The angle SK.BEST.T is returned to the calling routine in variable PEAK. 
     The flowchart of PROBE() is shown in FIG. 6. The source code is shown in lines 495-526 of Appendix 2. PROBE() calls a function PROJECT() at step 60. The flowchart of PROJECT() is shown in FIG. 8. The source code is shown in lines 235-432 of Appendix 2. PROJECT() initializes a projection array at step 80. At step 82, the reference points of the image file are projected at an angle contained in variable SKEW. An enhancement function is applied to the bin counts resulting from the projection at step 84 and the value of the enhancement function is returned to PROBE(). 
     The skew-finding algorithm has been applied to many documents representative of a wide variety of typographical and layout styles, having been selected from books, journals, theses, and typewritten pages. The selected documents include multiple columns, mixed fonts at various sizes, headers, trailers, and footnotes. They all possessed a dominant skew angle. The algorithm detected skew as small as 1/5 of the height of the text characters. The algorithm is able to compute skew to an accuracy as great as 2 minutes. 
     Most documents require about 40 probes for convergence to two minutes of arc. Runtime for documents with over 1000 characters is dominated by probing, which in turn is dominated by projecting. The iterative probing technique is thus about a factor of 100 times faster than an exhaustive search over all angles at the desired resolution. 
     It is to be understood that the above described arrangements are merely illustrative of the application of principles of the invention and that other arrangements may be devised by workers skilled in the art without departing from the spirit and scope of the invention. ##SPC1##