src/selgen.c
PTA *
pixSubsampleBoundaryPixels(PIX     *pixs,
                           l_int32  skip)
{
l_int32  x, y, xn, yn, xs, ys, xa, ya, count;
PIX     *pixt;
PTA     *pta;

    PROCNAME("pixSubsampleBoundaryPixels");

    if (!pixs)
        return (PTA *)ERROR_PTR("pixs not defined", procName, NULL);
    if (pixGetDepth(pixs) != 1)
        return (PTA *)ERROR_PTR("pixs not 1 bpp", procName, NULL);
    if (skip < 0)
        return (PTA *)ERROR_PTR("skip < 0", procName, NULL);
        
    if (skip == 0)
        return ptaGetPixelsFromPix(pixs, NULL);

    pta = ptaCreate(0);    
    pixt = pixCopy(NULL, pixs);
    xs = ys = 0;
    while (nextOnPixelInRaster(pixt, xs, ys, &xn, &yn)) {  /* new series */
        xs = xn;
        ys = yn;

            /* Add first point in this series */
        ptaAddPt(pta, xs, ys);

            /* Trace out boundary, erasing all and saving every (skip + 1)th */
        x = xs;
        y = ys;
        pixSetPixel(pixt, x, y, 0);
        count = 0;
        while (adjacentOnPixelInRaster(pixt, x, y, &xa, &ya)) {
            x = xa;
            y = ya;
            pixSetPixel(pixt, x, y, 0);
            if (count == skip) {
                ptaAddPt(pta, x, y);
                count = 0;
            }
            else {
                count++;
            }
        }
    }

    pixDestroy(&pixt);
    return pta;
}
/*!
 *  pixSubsampleBoundaryPixels()
 *
 *      Input:  pixs (1 bpp, with only boundary pixels in fg)
 *              skip (number to skip between samples as you traverse boundary)
 *      Return: pta, or null on error
 *
 *  Notes:
 *      (1) If skip = 0, we take all the fg pixels.
 *      (2) We try to traverse the boundaries in a regular way. 
 *          Some pixels may be missed, and these are then subsampled
 *          randomly with a fraction determined by 'skip'.
 *      (3) The most natural approach is to use a depth first (stack-based)
 *          method to find the fg pixels.  However, the pixel runs are
 *          4-connected and there are relatively few branches.  So 
 *          instead of doing a proper depth-first search, we get nearly
 *          the same result using two nested while loops: the outer
 *          one continues a raster-based search for the next fg pixel,
 *          and the inner one does a reasonable job running along
 *          each 4-connected coutour.
 */
pixSubsampleBoundaryPixels()
Input:  pixs (1 bpp, with only boundary pixels in fg)
skip (number to skip between samples as you traverse boundary)
Return: pta, or null on error

(1) If skip = 0, we take all the fg pixels.
(2) We try to traverse the boundaries in a regular way.
Some pixels may be missed, and these are then subsampled
randomly with a fraction determined by 'skip'.
(3) The most natural approach is to use a depth first (stack-based)
method to find the fg pixels.  However, the pixel runs are
4-connected and there are relatively few branches.  So
instead of doing a proper depth-first search, we get nearly
the same result using two nested while loops: the outer
one continues a raster-based search for the next fg pixel,
and the inner one does a reasonable job running along
each 4-connected coutour.
pixSubsampleBoundaryPixels () Input : pixs ( 1 bpp with only boundary pixels in fg ) skip ( number to skip between samples as you traverse boundary ) Return : pta or null on error ( 1 ) If skip = 0 we take all the fg pixels . ( 2 ) We try to traverse the boundaries in a regular way . Some pixels may be missed and these are then subsampled randomly with a fraction determined by ' skip ' . ( 3 ) The most natural approach is to use a depth first ( stack - based ) method to find the fg pixels . However the pixel runs are 4 - connected and there are relatively few branches . So instead of doing a proper depth - first search we get nearly the same result using two nested while loops : the outer one continues a raster - based search for the next fg pixel and the inner one does a reasonable job running along each 4 - connected coutour .
PTA *
pixSubsampleBoundaryPixels(PIX     *pixs,
                           l_int32  skip)
{
l_int32  x, y, xn, yn, xs, ys, xa, ya, count;
PIX     *pixt;
PTA     *pta;
    PROCNAME("pixSubsampleBoundaryPixels");
    if (!pixs)
        return (PTA *)ERROR_PTR("pixs not defined", procName, NULL);
    if (pixGetDepth(pixs) != 1)
        return (PTA *)ERROR_PTR("pixs not 1 bpp", procName, NULL);
    if (skip < 0)
        return (PTA *)ERROR_PTR("skip < 0", procName, NULL);
    if (skip == 0)
        return ptaGetPixelsFromPix(pixs, NULL);
    pta = ptaCreate(0);    
    pixt = pixCopy(NULL, pixs);
    xs = ys = 0;
    while (nextOnPixelInRaster(pixt, xs, ys, &xn, &yn)) {  
        xs = xn;
        ys = yn;
        ptaAddPt(pta, xs, ys);
        x = xs;
        y = ys;
        pixSetPixel(pixt, x, y, 0);
        count = 0;
        while (adjacentOnPixelInRaster(pixt, x, y, &xa, &ya)) {
            x = xa;
            y = ya;
            pixSetPixel(pixt, x, y, 0);
            if (count == skip) {
                ptaAddPt(pta, x, y);
                count = 0;
            }
            else {
                count++;
            }
        }
    }
    pixDestroy(&pixt);
    return pta;
}
PTA * pixSubsampleBoundaryPixels ( PIX * pixs , l_int32 skip ) { l_int32 x , y , xn , yn , xs , ys , xa , ya , count ; PIX * pixt ; PTA * pta ; PROCNAME ( " " ) ; if ( ! pixs ) return ( PTA * ) ERROR_PTR ( " " , procName , NULL ) ; if ( pixGetDepth ( pixs ) != 1 ) return ( PTA * ) ERROR_PTR ( " " , procName , NULL ) ; if ( skip < 0 ) return ( PTA * ) ERROR_PTR ( " " , procName , NULL ) ; if ( skip == 0 ) return ptaGetPixelsFromPix ( pixs , NULL ) ; pta = ptaCreate ( 0 ) ; pixt = pixCopy ( NULL , pixs ) ; xs = ys = 0 ; while ( nextOnPixelInRaster ( pixt , xs , ys , & xn , & yn ) ) { xs = xn ; ys = yn ; ptaAddPt ( pta , xs , ys ) ; x = xs ; y = ys ; pixSetPixel ( pixt , x , y , 0 ) ; count = 0 ; while ( adjacentOnPixelInRaster ( pixt , x , y , & xa , & ya ) ) { x = xa ; y = ya ; pixSetPixel ( pixt , x , y , 0 ) ; if ( count == skip ) { ptaAddPt ( pta , x , y ) ; count = 0 ; } else { count ++ ; } } } pixDestroy ( & pixt ) ; return pta ; }
pixSubsampleBoundaryPixels()
      Input:  pixs (1 bpp, with only boundary pixels in fg)
              skip (number to skip between samples as you traverse boundary)
      Return: pta, or null on error
pixSubsampleBoundaryPixels () Input : pixs ( 1 bpp with only boundary pixels in fg ) skip ( number to skip between samples as you traverse boundary ) Return : pta or null on error
/* new series */ /* Add first point in this series */ /* Trace out boundary, erasing all and saving every (skip + 1)th */