Leptonica 1.68
C Image Processing Library

boxfunc3.c File Reference

Mask <-> Boxa; painting into and drawing Boxa. More...

#include "allheaders.h"

Go to the source code of this file.

Defines

#define DEBUG_SPLIT   0

Functions

static l_int32 pixSearchForRectangle (PIX *pixs, BOX *boxs, l_int32 minsum, l_int32 skipdist, l_int32 delta, l_int32 maxbg, l_int32 sideflag, BOXA *boxat, NUMA *nascore)
PIXpixMaskConnComp (PIX *pixs, l_int32 connectivity, BOXA **pboxa)
PIXpixMaskBoxa (PIX *pixd, PIX *pixs, BOXA *boxa, l_int32 op)
PIXpixPaintBoxa (PIX *pixs, BOXA *boxa, l_uint32 val)
PIXpixSetBlackOrWhiteBoxa (PIX *pixs, BOXA *boxa, l_int32 op)
PIXpixPaintBoxaRandom (PIX *pixs, BOXA *boxa)
PIXpixBlendBoxaRandom (PIX *pixs, BOXA *boxa, l_float32 fract)
PIXpixDrawBoxa (PIX *pixs, BOXA *boxa, l_int32 width, l_uint32 val)
PIXpixDrawBoxaRandom (PIX *pixs, BOXA *boxa, l_int32 width)
PIXboxaaDisplay (BOXAA *boxaa, l_int32 linewba, l_int32 linewb, l_uint32 colorba, l_uint32 colorb, l_int32 w, l_int32 h)
BOXApixSplitIntoBoxa (PIX *pixs, l_int32 minsum, l_int32 skipdist, l_int32 delta, l_int32 maxbg, l_int32 maxcomps, l_int32 remainder)
BOXApixSplitComponentIntoBoxa (PIX *pix, BOX *box, l_int32 minsum, l_int32 skipdist, l_int32 delta, l_int32 maxbg, l_int32 maxcomps, l_int32 remainder)

Detailed Description

Mask <-> Boxa; painting into and drawing Boxa.

    Boxa/Boxaa painting into pix
         PIX             *pixMaskConnComp()
         PIX             *pixMaskBoxa()
         PIX             *pixPaintBoxa()
         PIX             *pixSetWhiteOrBlackBoxa()
         PIX             *pixPaintBoxaRandom()
         PIX             *pixBlendBoxaRandom()
         PIX             *pixDrawBoxa()
         PIX             *pixDrawBoxaRandom()
         PIX             *boxaaDisplay() 

    Split mask components into Boxa
         BOXA            *pixSplitIntoBoxa()
         BOXA            *pixSplitComponentIntoBoxa()
         static l_int32   pixSearchForRectangle()

See summary in pixPaintBoxa() of various ways to paint and draw
boxes on images.

Definition in file boxfunc3.c.


Define Documentation

#define DEBUG_SPLIT   0

Definition at line 47 of file boxfunc3.c.


Function Documentation

static l_int32 pixSearchForRectangle ( PIX pixs,
BOX boxs,
l_int32  minsum,
l_int32  skipdist,
l_int32  delta,
l_int32  maxbg,
l_int32  sideflag,
BOXA boxat,
NUMA nascore 
) [static]

pixSearchForRectangle()

Input: pixs (1 bpp) boxs (current region to investigate) minsum (minimum pixels to trigger propagation) skipdist (distance before computing sum for propagation) delta (difference required to stop propagation) maxbg (maximum number of allowed bg pixels in ref scan) sideflag (side to search from) boxat (add result of rectangular region found here) nascore (add score for this rectangle here) Return: 0 if OK, 1 on error

Notes: (1) See pixSplitByRectangles() for an explanation of the algorithm. This does the sweep from a single side. For each iteration in pixSplitByRectangles(), this will be called 4 times, for = {0, 1, 2, 3}. (2) If a valid rectangle is not found, add a score of 0 and input a minimum box.

Definition at line 886 of file boxfunc3.c.

References boxaAddBox(), boxCreate(), boxGetGeometry(), ERROR_INT, FREE, GET_DATA_BIT, L_ABS, L_FROM_BOTTOM, L_FROM_LEFT, L_FROM_RIGHT, L_FROM_TOP, L_INSERT, L_MAX, L_MIN, NULL, numaAddNumber(), pixGetDepth(), pixGetLinePtrs(), PROCNAME, x0, and y0.

Referenced by pixSplitComponentIntoBoxa().

PIX* pixMaskConnComp ( PIX pixs,
l_int32  connectivity,
BOXA **  pboxa 
)

pixMaskConnComp()

Input: pixs (1 bpp) connectivity (4 or 8) &boxa (<optional return>=""> bounding boxes of c.c.) Return: pixd (1 bpp mask over the c.c.), or null on error

Notes: (1) This generates a mask image with ON pixels over the b.b. of the c.c. in pixs. If there are no ON pixels in pixs, pixd will also have no ON pixels.

Definition at line 68 of file boxfunc3.c.

References boxaDestroy(), boxaGetCount(), ERROR_PTR, L_SET_PIXELS, NULL, pixConnComp(), pixCreateTemplate(), pixGetDepth(), pixMaskBoxa(), and PROCNAME.

Referenced by main().

PIX* pixMaskBoxa ( PIX pixd,
PIX pixs,
BOXA boxa,
l_int32  op 
)

pixMaskBoxa()

Input: pixd (<optional> may be null) pixs (any depth; not cmapped) boxa (of boxes, to paint) op (L_SET_PIXELS, L_CLEAR_PIXELS, L_FLIP_PIXELS) Return: pixd (with masking op over the boxes), or null on error

Notes: (1) This can be used with: pixd = NULL (makes a new pixd) pixd = pixs (in-place) (2) If pixd == NULL, this first makes a copy of pixs, and then bit-twiddles over the boxes. Otherwise, it operates directly on pixs. (3) This simple function is typically used with 1 bpp images. It uses the 1-image rasterop function, rasteropUniLow(), to set, clear or flip the pixels in pixd. (4) If you want to generate a 1 bpp mask of ON pixels from the boxes in a Boxa, in a pix of size (w,h): pix = pixCreate(w, h, 1); pixMaskBoxa(pix, pix, boxa, L_SET_PIXELS);

Definition at line 119 of file boxfunc3.c.

References boxaGetBox(), boxaGetCount(), boxDestroy(), boxGetGeometry(), ERROR_PTR, L_CLEAR_PIXELS, L_CLONE, L_FLIP_PIXELS, L_SET_PIXELS, L_WARNING, NULL, PIX_CLR, PIX_DST, PIX_NOT, PIX_SET, pixCopy(), pixGetColormap(), pixRasterop(), and PROCNAME.

Referenced by pixItalicWords(), and pixMaskConnComp().

PIX* pixPaintBoxa ( PIX pixs,
BOXA boxa,
l_uint32  val 
)

pixPaintBoxa()

Input: pixs (any depth, can be cmapped) boxa (of boxes, to paint) val (rgba color to paint) Return: pixd (with painted boxes), or null on error

Notes: (1) If pixs is 1 bpp or is colormapped, it is converted to 8 bpp and the boxa is painted using a colormap; otherwise, it is converted to 32 bpp rgb. (2) There are several ways to display a box on an image: * Paint it as a solid color * Draw the outline * Blend the outline or region with the existing image We provide painting and drawing here; blending is in blend.c. When painting or drawing, the result can be either a cmapped image or an rgb image. The dest will be cmapped if the src is either 1 bpp or has a cmap that is not full. To force RGB output, use pixConvertTo8(pixs, FALSE) before calling any of these paint and draw functions.

Definition at line 186 of file boxfunc3.c.

References boxaGetBox(), boxaGetCount(), boxDestroy(), ERROR_PTR, extractRGBValues(), FALSE, L_CLONE, L_WARNING, NULL, pixcmapAddNewColor(), pixcmapGetCount(), pixConvertTo32(), pixConvertTo8(), pixCopy(), pixGetColormap(), pixGetDepth(), pixSetInRectArbitrary(), PROCNAME, and TRUE.

Referenced by main().

PIX* pixSetBlackOrWhiteBoxa ( PIX pixs,
BOXA boxa,
l_int32  op 
)

pixSetBlackOrWhiteBoxa()

Input: pixs (any depth, can be cmapped) boxa (<optional> of boxes, to clear or set) op (L_SET_BLACK, L_SET_WHITE) Return: pixd (with boxes filled with white or black), or null on error

Definition at line 250 of file boxfunc3.c.

References boxaGetBox(), boxaGetCount(), boxDestroy(), ERROR_PTR, L_CLONE, L_SET_WHITE, NULL, pixClearInRect(), pixcmapAddBlackOrWhite(), pixCopy(), pixDestroy(), pixGetColormap(), pixGetDepth(), pixSetInRect(), pixSetInRectArbitrary(), PROCNAME, x0, and x3.

Referenced by pixConvertToPdfDataSegmented().

PIX* pixPaintBoxaRandom ( PIX pixs,
BOXA boxa 
)

pixPaintBoxaRandom()

Input: pixs (any depth, can be cmapped) boxa (of boxes, to paint) Return: pixd (with painted boxes), or null on error

Notes: (1) If pixs is 1 bpp, we paint the boxa using a colormap; otherwise, we convert to 32 bpp. (2) We use up to 254 different colors for painting the regions. (3) If boxes overlap, the later ones paint over earlier ones.

Definition at line 330 of file boxfunc3.c.

References boxaGetBox(), boxaGetCount(), boxDestroy(), composeRGBPixel(), ERROR_PTR, L_CLONE, L_WARNING, NULL, pixcmapCreateRandom(), pixcmapDestroy(), pixcmapGetColor(), pixConvert1To8(), pixConvertTo32(), pixCopy(), pixGetDepth(), pixSetColormap(), pixSetInRectArbitrary(), and PROCNAME.

Referenced by main().

PIX* pixBlendBoxaRandom ( PIX pixs,
BOXA boxa,
l_float32  fract 
)

pixBlendBoxaRandom()

Input: pixs (any depth; can be cmapped) boxa (of boxes, to blend/paint) fract (of box color to use) Return: pixd (32 bpp, with blend/painted boxes), or null on error

Notes: (1) pixs is converted to 32 bpp. (2) This differs from pixPaintBoxaRandom(), in that the colors here are blended with the color of pixs. (3) We use up to 254 different colors for painting the regions. (4) If boxes overlap, the final color depends only on the last rect that is used.

Definition at line 399 of file boxfunc3.c.

References boxaGetBox(), boxaGetCount(), boxDestroy(), composeRGBPixel(), ERROR_PTR, L_CLONE, L_WARNING, NULL, pixBlendInRect(), pixcmapCreateRandom(), pixcmapDestroy(), pixcmapGetColor(), pixConvertTo32(), pixCopy(), and PROCNAME.

Referenced by main().

PIX* pixDrawBoxa ( PIX pixs,
BOXA boxa,
l_int32  width,
l_uint32  val 
)

pixDrawBoxa()

Input: pixs (any depth; can be cmapped) boxa (of boxes, to draw) width (of lines) val (rgba color to draw) Return: pixd (with outlines of boxes added), or null on error

Notes: (1) If pixs is 1 bpp or is colormapped, it is converted to 8 bpp and the boxa is drawn using a colormap; otherwise, it is converted to 32 bpp rgb.

Definition at line 458 of file boxfunc3.c.

References boxaGetCount(), ERROR_PTR, extractRGBValues(), FALSE, L_WARNING, NULL, pixcmapAddNewColor(), pixcmapGetCount(), pixConvertTo32(), pixConvertTo8(), pixCopy(), pixGetColormap(), pixGetDepth(), pixRenderBoxaArb(), PROCNAME, and TRUE.

Referenced by main().

PIX* pixDrawBoxaRandom ( PIX pixs,
BOXA boxa,
l_int32  width 
)

pixDrawBoxaRandom()

Input: pixs (any depth, can be cmapped) boxa (of boxes, to draw) width (thickness of line) Return: pixd (with box outlines drawn), or null on error

Notes: (1) If pixs is 1 bpp, we draw the boxa using a colormap; otherwise, we convert to 32 bpp. (2) We use up to 254 different colors for drawing the boxes. (3) If boxes overlap, the later ones draw over earlier ones.

Definition at line 520 of file boxfunc3.c.

References boxaGetBox(), boxaGetCount(), boxDestroy(), ERROR_PTR, generatePtaaBoxa(), L_CLONE, L_WARNING, NULL, pixcmapCreateRandom(), pixcmapDestroy(), pixcmapGetColor(), pixConvertTo32(), pixCopy(), pixGetDepth(), pixRenderBoxArb(), pixRenderRandomCmapPtaa(), PROCNAME, and ptaaDestroy().

Referenced by main().

PIX* boxaaDisplay ( BOXAA boxaa,
l_int32  linewba,
l_int32  linewb,
l_uint32  colorba,
l_uint32  colorb,
l_int32  w,
l_int32  h 
)

boxaaDisplay()

Input: boxaa linewba (line width to display boxa) linewb (line width to display box) colorba (color to display boxa) colorb (color to display box) w (of pix; use 0 if determined by boxaa) h (of pix; use 0 if determined by boxaa) Return: 0 if OK, 1 on error

Definition at line 580 of file boxfunc3.c.

References boxaaGetBoxa(), boxaaGetCount(), boxaaGetExtent(), boxaDestroy(), boxaGetBox(), boxaGetCount(), boxaGetExtent(), boxDestroy(), ERROR_PTR, extractRGBValues(), L_CLONE, NULL, pixcmapAddColor(), pixcmapCreate(), pixCreate(), pixRenderBoxArb(), pixSetColormap(), and PROCNAME.

BOXA* pixSplitIntoBoxa ( PIX pixs,
l_int32  minsum,
l_int32  skipdist,
l_int32  delta,
l_int32  maxbg,
l_int32  maxcomps,
l_int32  remainder 
)

pixSplitIntoBoxa()

Input: pixs (1 bpp) minsum (minimum pixels to trigger propagation) skipdist (distance before computing sum for propagation) delta (difference required to stop propagation) maxbg (maximum number of allowed bg pixels in ref scan) maxcomps (use 0 for unlimited number of subdivided components) remainder (set to 1 to get b.b. of remaining stuff) Return: boxa (of rectangles covering the fg of pixs), or null on error

Notes: (1) This generates a boxa of rectangles that covers the fg of a mask. For each 8-connected component in pixs, it does a greedy partitioning, choosing the largest rectangle found from each of the four directions at each iter. See pixSplitComponentsIntoBoxa() for details. (2) The input parameters give some flexibility for boundary noise. The resulting set of rectangles may cover some bg pixels. (3) This should be used when there are a small number of mask components, each of which has sides that are close to horizontal and vertical. The input parameters and determine whether or not holes in the mask are covered. (4) The parameter gives the maximum number of allowed rectangles extracted from any single connected component. Use 0 if no limit is to be applied. (5) The flag specifies whether we take a final bounding box for anything left after the maximum number of allowed rectangle is extracted.

Definition at line 665 of file boxfunc3.c.

References boxaCreate(), boxaDestroy(), boxaGetBox(), boxaGetCount(), boxaJoin(), boxDestroy(), ERROR_PTR, L_CLONE, NULL, pixaDestroy(), pixaGetPix(), pixConnComp(), pixDestroy(), pixGetDepth(), pixSplitComponentIntoBoxa(), and PROCNAME.

Referenced by main().

BOXA* pixSplitComponentIntoBoxa ( PIX pix,
BOX box,
l_int32  minsum,
l_int32  skipdist,
l_int32  delta,
l_int32  maxbg,
l_int32  maxcomps,
l_int32  remainder 
)

pixSplitComponentIntoBoxa()

Input: pixs (1 bpp) box (<optional> location of pixs w/rt an origin) minsum (minimum pixels to trigger propagation) skipdist (distance before computing sum for propagation) delta (difference required to stop propagation) maxbg (maximum number of allowed bg pixels in ref scan) maxcomps (use 0 for unlimited number of subdivided components) remainder (set to 1 to get b.b. of remaining stuff) Return: boxa (of rectangles covering the fg of pixs), or null on error

Notes: (1) This generates a boxa of rectangles that covers the fg of a mask. It does so by a greedy partitioning of the mask, choosing the largest rectangle found from each of the four directions at each step. (2) The input parameters give some flexibility for boundary noise. The resulting set of rectangles must cover all the fg pixels and, in addition, may cover some bg pixels. Using small input parameters on a noiseless mask (i.e., one that has only large vertical and horizontal edges) will result in a proper covering of only the fg pixels of the mask. (3) The input is assumed to be a single connected component, that may have holes. From each side, sweep inward, counting the pixels. If the count becomes greater than , and we have moved forward a further amount , record that count ('countref'), but don't accept if the scan contains more than bg pixels. Continue the scan until we reach a count that differs from countref by at least , at which point the propagation stops. The box swept out gets a score, which is the sum of fg pixels minus a penalty. The penalty is the number of bg pixels in the box. This is done from all four sides, and the side with the largest score is saved as a rectangle. The process repeats until there is either no rectangle left, or there is one that can't be captured from any direction. For the latter case, we simply accept the last rectangle. (4) The input box is only used to specify the location of the UL corner of pixs, with respect to an origin that typically represents the UL corner of an underlying image, of which pixs is one component. If is null, the UL corner is taken to be (0, 0). (5) The parameter gives the maximum number of allowed rectangles extracted from any single connected component. Use 0 if no limit is to be applied. (6) The flag specifies whether we take a final bounding box for anything left after the maximum number of allowed rectangle is extracted. (7) So if > 0, it specifies that we want no more than the first rectangles that satisfy the input criteria. After this, we can get a final rectangle that bounds everything left over by setting == 1. If == 0, we only get rectangles that satisfy the input criteria. (8) It should be noted that the removal of rectangles can break the original c.c. into several c.c. (9) Summing up: * If == 0, the splitting proceeds as far as possible. * If > 0, the splitting stops when are found, or earlier if no more components can be selected. * If == 1 and components remain that cannot be selected, they are returned as a single final rectangle; otherwise, they are ignored.

Definition at line 772 of file boxfunc3.c.

References boxaAddBox(), boxaCreate(), boxaDestroy(), boxaGetBox(), boxaWriteStream(), boxCreate(), boxDestroy(), boxGetGeometry(), boxPrintStreamInfo(), boxTransform(), ERROR_PTR, L_CLONE, L_INSERT, L_SORT_DECREASING, NULL, numaCreate(), numaDestroy(), numaGetIValue(), numaGetSortIndex(), pixClearInRect(), pixClipBoxToForeground(), pixCopy(), pixDestroy(), pixGetDepth(), pixGetDimensions(), pixSearchForRectangle(), and PROCNAME.

Referenced by main(), and pixSplitIntoBoxa().

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines