tesseract  4.00.00dev
imagefind.h
Go to the documentation of this file.
1 // File: imagefind.h
3 // Description: Class to find image and drawing regions in an image
4 // and create a corresponding list of empty blobs.
5 // Author: Ray Smith
6 // Created: Fri Aug 01 10:50:01 PDT 2008
7 //
8 // (C) Copyright 2008, Google Inc.
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
20 
21 #ifndef TESSERACT_TEXTORD_IMAGEFIND_H_
22 #define TESSERACT_TEXTORD_IMAGEFIND_H_
23 
24 #include "debugpixa.h"
25 #include "host.h"
26 
27 struct Boxa;
28 struct Pix;
29 struct Pixa;
30 class TBOX;
31 class FCOORD;
32 class TO_BLOCK;
33 class BLOBNBOX_LIST;
34 
35 namespace tesseract {
36 
37 class ColPartitionGrid;
38 class ColPartition_LIST;
39 class TabFind;
40 
41 // The ImageFind class is a simple static function wrapper class that
42 // exposes the FindImages function and some useful helper functions.
43 class ImageFind {
44  public:
45  // Finds image regions within the BINARY source pix (page image) and returns
46  // the image regions as a mask image.
47  // The returned pix may be NULL, meaning no images found.
48  // If not NULL, it must be PixDestroyed by the caller.
49  // If textord_tabfind_show_images, debug images are appended to pixa_debug.
50  static Pix* FindImages(Pix* pix, DebugPixa* pixa_debug);
51 
52  // Generates a Boxa, Pixa pair from the input binary (image mask) pix,
53  // analgous to pixConnComp, except that connected components which are nearly
54  // rectangular are replaced with solid rectangles.
55  // The returned boxa, pixa may be NULL, meaning no images found.
56  // If not NULL, they must be destroyed by the caller.
57  // Resolution of pix should match the source image (Tesseract::pix_binary_)
58  // so the output coordinate systems match.
59  static void ConnCompAndRectangularize(Pix* pix, DebugPixa* pixa_debug,
60  Boxa** boxa, Pixa** pixa);
61 
62  // Returns true if there is a rectangle in the source pix, such that all
63  // pixel rows and column slices outside of it have less than
64  // min_fraction of the pixels black, and within max_skew_gradient fraction
65  // of the pixels on the inside, there are at least max_fraction of the
66  // pixels black. In other words, the inside of the rectangle looks roughly
67  // rectangular, and the outside of it looks like extra bits.
68  // On return, the rectangle is defined by x_start, y_start, x_end and y_end.
69  // Note: the algorithm is iterative, allowing it to slice off pixels from
70  // one edge, allowing it to then slice off more pixels from another edge.
71  static bool pixNearlyRectangular(Pix* pix,
72  double min_fraction, double max_fraction,
73  double max_skew_gradient,
74  int* x_start, int* y_start,
75  int* x_end, int* y_end);
76 
77  // Given an input pix, and a bounding rectangle, the sides of the rectangle
78  // are shrunk inwards until they bound any black pixels found within the
79  // original rectangle. Returns false if the rectangle contains no black
80  // pixels at all.
81  static bool BoundsWithinRect(Pix* pix, int* x_start, int* y_start,
82  int* x_end, int* y_end);
83 
84  // Given a point in 3-D (RGB) space, returns the squared Euclidean distance
85  // of the point from the given line, defined by a pair of points in the 3-D
86  // (RGB) space, line1 and line2.
87  static double ColorDistanceFromLine(const uinT8* line1, const uinT8* line2,
88  const uinT8* point);
89 
90  // Returns the leptonica combined code for the given RGB triplet.
91  static uinT32 ComposeRGB(uinT32 r, uinT32 g, uinT32 b);
92 
93  // Returns the input value clipped to a uinT8.
94  static uinT8 ClipToByte(double pixel);
95 
96  // Computes the light and dark extremes of color in the given rectangle of
97  // the given pix, which is factor smaller than the coordinate system in rect.
98  // The light and dark points are taken to be the upper and lower 8th-ile of
99  // the most deviant of R, G and B. The value of the other 2 channels are
100  // computed by linear fit against the most deviant.
101  // The colors of the two point are returned in color1 and color2, with the
102  // alpha channel set to a scaled mean rms of the fits.
103  // If color_map1 is not null then it and color_map2 get rect pasted in them
104  // with the two calculated colors, and rms map gets a pasted rect of the rms.
105  // color_map1, color_map2 and rms_map are assumed to be the same scale as pix.
106  static void ComputeRectangleColors(const TBOX& rect, Pix* pix, int factor,
107  Pix* color_map1, Pix* color_map2,
108  Pix* rms_map,
109  uinT8* color1, uinT8* color2);
110 
111  // Returns true if there are no black pixels in between the boxes.
112  // The im_box must represent the bounding box of the pix in tesseract
113  // coordinates, which may be negative, due to rotations to make the textlines
114  // horizontal. The boxes are rotated by rotation, which should undo such
115  // rotations, before mapping them onto the pix.
116  static bool BlankImageInBetween(const TBOX& box1, const TBOX& box2,
117  const TBOX& im_box, const FCOORD& rotation,
118  Pix* pix);
119 
120  // Returns the number of pixels in box in the pix.
121  // The im_box must represent the bounding box of the pix in tesseract
122  // coordinates, which may be negative, due to rotations to make the textlines
123  // horizontal. The boxes are rotated by rotation, which should undo such
124  // rotations, before mapping them onto the pix.
125  static int CountPixelsInRotatedBox(TBOX box, const TBOX& im_box,
126  const FCOORD& rotation, Pix* pix);
127 
128 
129  // Locates all the image partitions in the part_grid, that were found by a
130  // previous call to FindImagePartitions, marks them in the image_mask,
131  // removes them from the grid, and deletes them. This makes it possble to
132  // call FindImagePartitions again to produce less broken-up and less
133  // overlapping image partitions.
134  // rerotation specifies how to rotate the partition coords to match
135  // the image_mask, since this function is used after orientation correction.
136  static void TransferImagePartsToImageMask(const FCOORD& rerotation,
137  ColPartitionGrid* part_grid,
138  Pix* image_mask);
139 
140  // Runs a CC analysis on the image_pix mask image, and creates
141  // image partitions from them, cutting out strong text, and merging with
142  // nearby image regions such that they don't interfere with text.
143  // Rotation and rerotation specify how to rotate image coords to match
144  // the blob and partition coords and back again.
145  // The input/output part_grid owns all the created partitions, and
146  // the partitions own all the fake blobs that belong in the partitions.
147  // Since the other blobs in the other partitions will be owned by the block,
148  // ColPartitionGrid::ReTypeBlobs must be called afterwards to fix this
149  // situation and collect the image blobs.
150  static void FindImagePartitions(Pix* image_pix, const FCOORD& rotation,
151  const FCOORD& rerotation, TO_BLOCK* block,
152  TabFind* tab_grid, DebugPixa* pixa_debug,
153  ColPartitionGrid* part_grid,
154  ColPartition_LIST* big_parts);
155 };
156 
157 } // namespace tesseract.
158 
159 #endif // TESSERACT_TEXTORD_LINEFIND_H_
static void ComputeRectangleColors(const TBOX &rect, Pix *pix, int factor, Pix *color_map1, Pix *color_map2, Pix *rms_map, uinT8 *color1, uinT8 *color2)
Definition: imagefind.cpp:408
Definition: points.h:189
static int CountPixelsInRotatedBox(TBOX box, const TBOX &im_box, const FCOORD &rotation, Pix *pix)
Definition: imagefind.cpp:591
static double ColorDistanceFromLine(const uinT8 *line1, const uinT8 *line2, const uinT8 *point)
Definition: imagefind.cpp:349
static Pix * FindImages(Pix *pix, DebugPixa *pixa_debug)
Definition: imagefind.cpp:66
static void FindImagePartitions(Pix *image_pix, const FCOORD &rotation, const FCOORD &rerotation, TO_BLOCK *block, TabFind *tab_grid, DebugPixa *pixa_debug, ColPartitionGrid *part_grid, ColPartition_LIST *big_parts)
Definition: imagefind.cpp:1292
uint32_t uinT32
Definition: host.h:39
static uinT8 ClipToByte(double pixel)
Definition: imagefind.cpp:390
static void ConnCompAndRectangularize(Pix *pix, DebugPixa *pixa_debug, Boxa **boxa, Pixa **pixa)
Definition: imagefind.cpp:148
static bool BoundsWithinRect(Pix *pix, int *x_start, int *y_start, int *x_end, int *y_end)
Definition: imagefind.cpp:326
Definition: rect.h:30
static bool pixNearlyRectangular(Pix *pix, double min_fraction, double max_fraction, double max_skew_gradient, int *x_start, int *y_start, int *x_end, int *y_end)
Definition: imagefind.cpp:260
static void TransferImagePartsToImageMask(const FCOORD &rerotation, ColPartitionGrid *part_grid, Pix *image_mask)
Definition: imagefind.cpp:1239
uint8_t uinT8
Definition: host.h:35
static uinT32 ComposeRGB(uinT32 r, uinT32 g, uinT32 b)
Definition: imagefind.cpp:383
static bool BlankImageInBetween(const TBOX &box1, const TBOX &box2, const TBOX &im_box, const FCOORD &rotation, Pix *pix)
Definition: imagefind.cpp:570