Leptonica 1.68
C Image Processing Library

conncomp.c File Reference

Connected component counting and extraction (Heckbert's seedfill algorithm) More...

#include <stdio.h>
#include <stdlib.h>
#include "allheaders.h"

Go to the source code of this file.

Data Structures

struct  FillSeg

Defines

#define DEBUG   0

Typedefs

typedef struct FillSeg FILLSEG

Functions

static void pushFillsegBB (L_STACK *lstack, l_int32 xleft, l_int32 xright, l_int32 y, l_int32 dy, l_int32 ymax, l_int32 *pminx, l_int32 *pmaxx, l_int32 *pminy, l_int32 *pmaxy)
static void pushFillseg (L_STACK *lstack, l_int32 xleft, l_int32 xright, l_int32 y, l_int32 dy, l_int32 ymax)
static void popFillseg (L_STACK *lstack, l_int32 *pxleft, l_int32 *pxright, l_int32 *py, l_int32 *pdy)
BOXApixConnComp (PIX *pixs, PIXA **ppixa, l_int32 connectivity)
BOXApixConnCompPixa (PIX *pixs, PIXA **ppixa, l_int32 connectivity)
BOXApixConnCompBB (PIX *pixs, l_int32 connectivity)
l_int32 pixCountConnComp (PIX *pixs, l_int32 connectivity, l_int32 *pcount)
l_int32 nextOnPixelInRaster (PIX *pixs, l_int32 xstart, l_int32 ystart, l_int32 *px, l_int32 *py)
l_int32 nextOnPixelInRasterLow (l_uint32 *data, l_int32 w, l_int32 h, l_int32 wpl, l_int32 xstart, l_int32 ystart, l_int32 *px, l_int32 *py)
BOXpixSeedfillBB (PIX *pixs, L_STACK *lstack, l_int32 x, l_int32 y, l_int32 connectivity)
BOXpixSeedfill4BB (PIX *pixs, L_STACK *lstack, l_int32 x, l_int32 y)
BOXpixSeedfill8BB (PIX *pixs, L_STACK *lstack, l_int32 x, l_int32 y)
l_int32 pixSeedfill (PIX *pixs, L_STACK *lstack, l_int32 x, l_int32 y, l_int32 connectivity)
l_int32 pixSeedfill4 (PIX *pixs, L_STACK *lstack, l_int32 x, l_int32 y)
l_int32 pixSeedfill8 (PIX *pixs, L_STACK *lstack, l_int32 x, l_int32 y)

Detailed Description

Connected component counting and extraction (Heckbert's seedfill algorithm)

  Connected component counting and extraction, using Heckbert's
  stack-based filling algorithm.

    4- and 8-connected components: counts, bounding boxes and images

    Top-level calls:
         BOXA     *pixConnComp()
         BOXA     *pixConnCompPixa()
         BOXA     *pixConnCompBB()
         l_int32   pixCountConnComp()

    Identify the next c.c. to be erased:
         l_int32   nextOnPixelInRaster()
         l_int32   nextOnPixelInRasterLow()

    Erase the c.c., saving the b.b.:
         BOX      *pixSeedfillBB()
         BOX      *pixSeedfill4BB()
         BOX      *pixSeedfill8BB()

    Just erase the c.c.:
         l_int32   pixSeedfill()
         l_int32   pixSeedfill4()
         l_int32   pixSeedfill8()

    Static stack helper functions for single raster line seedfill:
         static void    pushFillsegBB()
         static void    pushFillseg()
         static void    popFillseg()

The basic method in pixConnCompBB() is very simple.  We scan the
image in raster order, looking for the next ON pixel.  When it
is found, we erase it and every pixel of the 4- or 8-connected
component to which it belongs, using Heckbert's seedfill
algorithm.  As pixels are erased, we keep track of the
minimum rectangle that encloses all erased pixels; after
the connected component has been erased, we save its
bounding box in an array of boxes.  When all pixels in the
image have been erased, we have an array that describes every
4- or 8-connected component in terms of its bounding box.

pixConnCompPixa() is a slight variation on pixConnCompBB(),
where we additionally save an array of images (in a Pixa)
of each of the 4- or 8-connected components.  This is done trivially
by maintaining two temporary images.  We erase a component from one,
and use the bounding box to extract the pixels within the b.b.
from each of the two images.  An XOR between these subimages
gives the erased component.  Then we erase the component from the
second image using the XOR again, with the extracted component
placed on the second image at the location of the bounding box.
Rasterop does all the work.  At the end, we have an array
of the 4- or 8-connected components, as well as an array of the
bounding boxes that describe where they came from in the original image.

If you just want the number of connected components, pixCountConnComp()
is a bit faster than pixConnCompBB(), because it doesn't have to
keep track of the bounding rectangles for each c.c.

Definition in file conncomp.c.


Define Documentation

#define DEBUG   0

Definition at line 111 of file conncomp.c.


Typedef Documentation

typedef struct FillSeg FILLSEG

Definition at line 96 of file conncomp.c.


Function Documentation

static void pushFillsegBB ( L_STACK lstack,
l_int32  xleft,
l_int32  xright,
l_int32  y,
l_int32  dy,
l_int32  ymax,
l_int32 pminx,
l_int32 pmaxx,
l_int32 pminy,
l_int32 pmaxy 
) [static]

pushFillsegBB()

Input: lstack xleft, xright y dy ymax, &minx (<return>) &maxx (<return>) &miny (<return>) &maxy (<return>) Return: void

Notes: (1) This adds a line segment to the stack, and returns its size. (2) The auxiliary stack is used as a storage area to recycle fillsegs that are no longer in use. We only calloc new fillsegs if the auxiliary stack is empty.

Definition at line 1025 of file conncomp.c.

References L_Stack::auxstack, CALLOC, FillSeg::dy, L_ERROR, L_MAX, L_MIN, lstackAdd(), lstackGetCount(), lstackRemove(), NULL, PROCNAME, FillSeg::xleft, FillSeg::xright, and FillSeg::y.

Referenced by pixSeedfill4BB(), and pixSeedfill8BB().

static void pushFillseg ( L_STACK lstack,
l_int32  xleft,
l_int32  xright,
l_int32  y,
l_int32  dy,
l_int32  ymax 
) [static]

pushFillseg()

Input: lstack xleft, xright y dy ymax Return: void

Notes: (1) This adds a line segment to the stack. (2) The auxiliary stack is used as a storage area to recycle fillsegs that are no longer in use. We only calloc new fillsegs if the auxiliary stack is empty.

Definition at line 1094 of file conncomp.c.

References L_Stack::auxstack, CALLOC, FillSeg::dy, L_ERROR, lstackAdd(), lstackGetCount(), lstackRemove(), NULL, PROCNAME, FillSeg::xleft, FillSeg::xright, and FillSeg::y.

Referenced by pixSeedfill4(), and pixSeedfill8().

static void popFillseg ( L_STACK lstack,
l_int32 pxleft,
l_int32 pxright,
l_int32 py,
l_int32 pdy 
) [static]

popFillseg()

Input: lstack &xleft (<return>) &xright (<return>) &y (<return>) &dy (<return>) Return: void

Notes: (1) This removes a line segment from the stack, and returns its size. (2) The surplussed fillseg is placed on the auxiliary stack for future use.

Definition at line 1153 of file conncomp.c.

References L_Stack::auxstack, FillSeg::dy, L_ERROR, lstackAdd(), lstackRemove(), NULL, PROCNAME, FillSeg::xleft, FillSeg::xright, and FillSeg::y.

Referenced by pixSeedfill4(), pixSeedfill4BB(), pixSeedfill8(), and pixSeedfill8BB().

BOXA* pixConnCompPixa ( PIX pixs,
PIXA **  ppixa,
l_int32  connectivity 
)

pixConnCompPixa()

Input: pixs (1 bpp) &pixa (<return> pixa of each c.c.) connectivity (4 or 8) Return: boxa, or null on error

Notes: (1) This finds bounding boxes of 4- or 8-connected components in a binary image, and saves images of each c.c in a pixa array. (2) It sets up 2 temporary pix, and for each c.c. that is located in raster order, it erases the c.c. from one pix, then uses the b.b. to extract the c.c. from the two pix using an XOR, and finally erases the c.c. from the second pix. (3) A clone of the returned boxa (where all boxes in the array are clones) is inserted into the pixa. (4) If the input is valid, this always returns a boxa and a pixa. If pixs is empty, the boxa and pixa will be empty.

Definition at line 176 of file conncomp.c.

References L_Stack::auxstack, Pixa::boxa, boxaAddBox(), boxaCopy(), boxaCreate(), boxaDestroy(), ERROR_PTR, Box::h, IFF_PNG, L_CLONE, L_INSERT, lstackCreate(), lstackDestroy(), nextOnPixelInRaster(), NULL, PIX_DST, PIX_SRC, pixaAddPix(), pixaCreate(), pixClipRectangle(), pixCopy(), pixCountPixels(), pixDestroy(), pixGetDepth(), pixGetHeight(), pixRasterop(), pixSeedfillBB(), pixWrite(), pixXor(), pixZero(), PROCNAME, TRUE, Box::w, Box::x, Box::y, and FillSeg::y.

Referenced by pixConnComp().

BOXA* pixConnCompBB ( PIX pixs,
l_int32  connectivity 
)

pixConnCompBB()

Input: pixs (1 bpp) connectivity (4 or 8) Return: boxa, or null on error

Notes: (1) Finds bounding boxes of 4- or 8-connected components in a binary image. (2) This works on a copy of the input pix. The c.c. are located in raster order and erased one at a time. In the process, the b.b. is computed and saved.

Definition at line 276 of file conncomp.c.

References L_Stack::auxstack, boxaAddBox(), boxaCreate(), ERROR_PTR, IFF_PNG, L_INSERT, lstackCreate(), lstackDestroy(), nextOnPixelInRaster(), NULL, pixCopy(), pixCountPixels(), pixDestroy(), pixGetDepth(), pixGetHeight(), pixSeedfillBB(), pixWrite(), pixZero(), PROCNAME, TRUE, and FillSeg::y.

Referenced by main(), pixConnComp(), and pixWordMaskByDilation().

l_int32 pixCountConnComp ( PIX pixs,
l_int32  connectivity,
l_int32 pcount 
)

pixCountConnComp()

Input: pixs (1 bpp) connectivity (4 or 8) &count (<return> Return: 0 if OK, 1 on error

Notes: (1) This is the top-level call for getting the number of 4- or 8-connected components in a 1 bpp image. (2) It works on a copy of the input pix. The c.c. are located in raster order and erased one at a time.

Definition at line 353 of file conncomp.c.

References L_Stack::auxstack, ERROR_INT, lstackCreate(), lstackDestroy(), nextOnPixelInRaster(), NULL, pixCopy(), pixDestroy(), pixGetDepth(), pixSeedfill(), pixZero(), PROCNAME, TRUE, and FillSeg::y.

Referenced by main().

l_int32 nextOnPixelInRaster ( PIX pixs,
l_int32  xstart,
l_int32  ystart,
l_int32 px,
l_int32 py 
)

nextOnPixelInRaster()

Input: pixs (1 bpp) xstart, ystart (starting point for search) &x, &y (<return> coord value of next ON pixel) Return: 1 if a pixel is found; 0 otherwise or on error

Definition at line 416 of file conncomp.c.

References ERROR_INT, nextOnPixelInRasterLow(), pixGetData(), pixGetDimensions(), pixGetWpl(), and PROCNAME.

Referenced by pixConnCompBB(), pixConnCompPixa(), pixCountConnComp(), pixGetOuterBorder(), pixQualifyLocalMinima(), and pixSubsampleBoundaryPixels().

l_int32 nextOnPixelInRasterLow ( l_uint32 data,
l_int32  w,
l_int32  h,
l_int32  wpl,
l_int32  xstart,
l_int32  ystart,
l_int32 px,
l_int32 py 
)

Definition at line 440 of file conncomp.c.

References GET_DATA_BIT, and FillSeg::y.

Referenced by nextOnPixelInRaster().

BOX* pixSeedfillBB ( PIX pixs,
L_STACK lstack,
l_int32  x,
l_int32  y,
l_int32  connectivity 
)

pixSeedfillBB()

Input: pixs (1 bpp) lstack (for holding fillsegs) x,y (location of seed pixel) connectivity (4 or 8) Return: box or null on error

Notes: (1) This is the high-level interface to Paul Heckbert's stack-based seedfill algorithm.

Definition at line 515 of file conncomp.c.

References ERROR_PTR, NULL, pixGetDepth(), pixSeedfill4BB(), pixSeedfill8BB(), and PROCNAME.

Referenced by pixConnCompBB(), and pixConnCompPixa().

BOX* pixSeedfill4BB ( PIX pixs,
L_STACK lstack,
l_int32  x,
l_int32  y 
)

pixSeedfill4BB()

Input: pixs (1 bpp) lstack (for holding fillsegs) x,y (location of seed pixel) Return: box or null on error.

Notes: (1) This is Paul Heckbert's stack-based 4-cc seedfill algorithm. (2) This operates on the input 1 bpp pix to remove the fg seed pixel, at (x,y), and all pixels that are 4-connected to it. The seed pixel at (x,y) must initially be ON. (3) Returns the bounding box of the erased 4-cc component. (4) Reference: see Paul Heckbert's stack-based seed fill algorithm in "Graphic Gems", ed. Andrew Glassner, Academic Press, 1990. The algorithm description is given on pp. 275-277; working C code is on pp. 721-722.) The code here follows Heckbert's exactly, except we use function calls instead of macros for pushing data on and popping data off the stack. This makes sense to do because Heckbert's fixed-size stack with macros is dangerous: images exist that will overrun the stack and crash. The stack utility here grows dynamically as needed, and the fillseg structures that are not in use are stored in another stack for reuse. It should be noted that the overhead in the function calls (vs. macros) is negligible.

Definition at line 577 of file conncomp.c.

References boxCreate(), CLEAR_DATA_BIT, FillSeg::dy, ERROR_PTR, GET_DATA_BIT, lstackGetCount(), NULL, pixGetData(), pixGetDepth(), pixGetDimensions(), pixGetWpl(), popFillseg(), PROCNAME, pushFillsegBB(), x1, x2, and FillSeg::y.

Referenced by pixSeedfillBB().

BOX* pixSeedfill8BB ( PIX pixs,
L_STACK lstack,
l_int32  x,
l_int32  y 
)

pixSeedfill8BB()

Input: pixs (1 bpp) lstack (for holding fillsegs) x,y (location of seed pixel) Return: box or null on error.

Notes: (1) This is Paul Heckbert's stack-based 8-cc seedfill algorithm. (2) This operates on the input 1 bpp pix to remove the fg seed pixel, at (x,y), and all pixels that are 8-connected to it. The seed pixel at (x,y) must initially be ON. (3) Returns the bounding box of the erased 8-cc component. (4) Reference: see Paul Heckbert's stack-based seed fill algorithm in "Graphic Gems", ed. Andrew Glassner, Academic Press, 1990. The algorithm description is given on pp. 275-277; working C code is on pp. 721-722.) The code here follows Heckbert's closely, except the leak checks are changed for 8 connectivity. See comments on pixSeedfill4BB() for more details.

Definition at line 689 of file conncomp.c.

References boxCreate(), CLEAR_DATA_BIT, FillSeg::dy, ERROR_PTR, GET_DATA_BIT, lstackGetCount(), NULL, pixGetData(), pixGetDepth(), pixGetDimensions(), pixGetWpl(), popFillseg(), PROCNAME, pushFillsegBB(), x1, x2, and FillSeg::y.

Referenced by pixSeedfillBB().

l_int32 pixSeedfill ( PIX pixs,
L_STACK lstack,
l_int32  x,
l_int32  y,
l_int32  connectivity 
)

pixSeedfill()

Input: pixs (1 bpp) lstack (for holding fillsegs) x,y (location of seed pixel) connectivity (4 or 8) Return: 0 if OK, 1 on error

Notes: (1) This removes the component from pixs with a fg pixel at (x,y). (2) See pixSeedfill4() and pixSeedfill8() for details.

Definition at line 792 of file conncomp.c.

References ERROR_INT, pixGetDepth(), pixSeedfill4(), pixSeedfill8(), and PROCNAME.

Referenced by pixCountConnComp().

l_int32 pixSeedfill4 ( PIX pixs,
L_STACK lstack,
l_int32  x,
l_int32  y 
)

pixSeedfill4()

Input: pixs (1 bpp) lstack (for holding fillsegs) x,y (location of seed pixel) Return: 0 if OK, 1 on error

Notes: (1) This is Paul Heckbert's stack-based 4-cc seedfill algorithm. (2) This operates on the input 1 bpp pix to remove the fg seed pixel, at (x,y), and all pixels that are 4-connected to it. The seed pixel at (x,y) must initially be ON. (3) Reference: see pixSeedFill4BB()

Definition at line 834 of file conncomp.c.

References CLEAR_DATA_BIT, FillSeg::dy, ERROR_INT, GET_DATA_BIT, lstackGetCount(), NULL, pixGetData(), pixGetDepth(), pixGetDimensions(), pixGetWpl(), popFillseg(), PROCNAME, pushFillseg(), x1, and x2.

Referenced by pixSeedfill().

l_int32 pixSeedfill8 ( PIX pixs,
L_STACK lstack,
l_int32  x,
l_int32  y 
)

pixSeedfill8()

Input: pixs (1 bpp) lstack (for holding fillsegs) x,y (location of seed pixel) Return: 0 if OK, 1 on error

Notes: (1) This is Paul Heckbert's stack-based 8-cc seedfill algorithm. (2) This operates on the input 1 bpp pix to remove the fg seed pixel, at (x,y), and all pixels that are 8-connected to it. The seed pixel at (x,y) must initially be ON. (3) Reference: see pixSeedFill8BB()

Definition at line 925 of file conncomp.c.

References CLEAR_DATA_BIT, FillSeg::dy, ERROR_INT, GET_DATA_BIT, lstackGetCount(), NULL, pixGetData(), pixGetDepth(), pixGetDimensions(), pixGetWpl(), popFillseg(), PROCNAME, pushFillseg(), x1, and x2.

Referenced by pixSeedfill().

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines