tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
imagefind.h
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 nullptr, meaning no images found.
48  // If not nullptr, 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 nullptr, meaning no images found.
56  // If not nullptr, 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_t* line1, const uint8_t* line2,
88  const uint8_t* point);
89 
90  // Returns the leptonica combined code for the given RGB triplet.
91  static uint32_t ComposeRGB(uint32_t r, uint32_t g, uint32_t b);
92 
93  // Returns the input value clipped to a uint8_t.
94  static uint8_t 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_t* color1, uint8_t* 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 possible 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_
Definition: colpartitiongrid.h:33
static void ComputeRectangleColors(const TBOX &rect, Pix *pix, int factor, Pix *color_map1, Pix *color_map2, Pix *rms_map, uint8_t *color1, uint8_t *color2)
Definition: imagefind.cpp:415
Definition: rect.h:34
static void TransferImagePartsToImageMask(const FCOORD &rerotation, ColPartitionGrid *part_grid, Pix *image_mask)
Definition: imagefind.cpp:1246
Definition: baseapi.cpp:94
static bool BlankImageInBetween(const TBOX &box1, const TBOX &box2, const TBOX &im_box, const FCOORD &rotation, Pix *pix)
Definition: imagefind.cpp:577
Definition: imagefind.h:43
static Pix * FindImages(Pix *pix, DebugPixa *pixa_debug)
Definition: imagefind.cpp:63
static void ConnCompAndRectangularize(Pix *pix, DebugPixa *pixa_debug, Boxa **boxa, Pixa **pixa)
Definition: imagefind.cpp:155
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:267
static uint8_t ClipToByte(double pixel)
Definition: imagefind.cpp:397
static int CountPixelsInRotatedBox(TBOX box, const TBOX &im_box, const FCOORD &rotation, Pix *pix)
Definition: imagefind.cpp:598
static double ColorDistanceFromLine(const uint8_t *line1, const uint8_t *line2, const uint8_t *point)
Definition: imagefind.cpp:356
Definition: debugpixa.h:10
static bool BoundsWithinRect(Pix *pix, int *x_start, int *y_start, int *x_end, int *y_end)
Definition: imagefind.cpp:333
Definition: tabfind.h:53
Definition: blobbox.h:705
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:1299
static uint32_t ComposeRGB(uint32_t r, uint32_t g, uint32_t b)
Definition: imagefind.cpp:390
Definition: points.h:189