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