src/boxfunc5.c
BOXA *
boxaWindowedMedian(BOXA    *boxas,
                   l_int32  halfwin,
                   l_int32  debug)
{
l_int32  n, i, left, top, right, bot;
BOX     *box;
BOXA    *boxaf, *boxad;
NUMA    *nal, *nat, *nar, *nab, *naml, *namt, *namr, *namb;
PIX     *pix1;

    PROCNAME("boxaWindowedMedian");

    if (!boxas)
        return (BOXA *)ERROR_PTR("boxas not defined", procName, NULL);
    if ((n = boxaGetCount(boxas)) < 3) {
        L_WARNING("less than 3 boxes; returning a copy\n", procName);
        return boxaCopy(boxas, L_COPY);
    }
    if (halfwin <= 0) {
        L_WARNING("halfwin must be > 0; returning copy\n", procName);
        return boxaCopy(boxas, L_COPY);
    }

        /* Fill invalid boxes in the input sequence */
    if ((boxaf = boxaFillSequence(boxas, L_USE_ALL_BOXES, debug)) == NULL)
        return (BOXA *)ERROR_PTR("filled boxa not made", procName, NULL);

        /* Get the windowed median output from each of the sides */
    boxaExtractAsNuma(boxaf, &nal, &nat, &nar, &nab, NULL, NULL, 0);
    naml = numaWindowedMedian(nal, halfwin);
    namt = numaWindowedMedian(nat, halfwin);
    namr = numaWindowedMedian(nar, halfwin);
    namb = numaWindowedMedian(nab, halfwin);

    n = boxaGetCount(boxaf);
    boxad = boxaCreate(n);
    for (i = 0; i < n; i++) {
        numaGetIValue(naml, i, &left);
        numaGetIValue(namt, i, &top);
        numaGetIValue(namr, i, &right);
        numaGetIValue(namb, i, &bot);
        box = boxCreate(left, top, right - left + 1, bot - top + 1);
        boxaAddBox(boxad, box, L_INSERT);
    }

    if (debug) {
        lept_mkdir("lept/windowed");
        boxaPlotSides(boxaf, NULL, NULL, NULL, NULL, NULL, &pix1);
        pixWrite("/tmp/lept/windowed/plotsides1.png", pix1, IFF_PNG);
        pixDestroy(&pix1);
        boxaPlotSides(boxad, NULL, NULL, NULL, NULL, NULL, &pix1);
        pixWrite("/tmp/lept/windowed/plotsides2.png", pix1, IFF_PNG);
        pixDestroy(&pix1);
        boxaPlotSizes(boxaf, NULL, NULL, NULL, &pix1);
        pixWrite("/tmp/lept/windowed/plotsizes1.png", pix1, IFF_PNG);
        pixDestroy(&pix1);
        boxaPlotSizes(boxad, NULL, NULL, NULL, &pix1);
        pixWrite("/tmp/lept/windowed/plotsizes2.png", pix1, IFF_PNG);
        pixDestroy(&pix1);
    }

    boxaDestroy(&boxaf);
    numaDestroy(&nal);
    numaDestroy(&nat);
    numaDestroy(&nar);
    numaDestroy(&nab);
    numaDestroy(&naml);
    numaDestroy(&namt);
    numaDestroy(&namr);
    numaDestroy(&namb);
    return boxad;
}
/*!
 * \brief   boxaWindowedMedian()
 *
 * \param[in]    boxas     source boxa
 * \param[in]    halfwin   half width of window over which the median is found
 * \param[in]    debug     1 for debug output
 * \return  boxad smoothed boxa, or NULL on error
 *
 * <pre>
 * Notes:
 *      (1) This finds a set of boxes (boxad) where each edge of each box is
 *          a windowed median smoothed value to the edges of the
 *          input set of boxes (boxas).
 *      (2) Invalid input boxes are filled from nearby ones.
 *      (3) The returned boxad can then be used in boxaModifyWithBoxa()
 *          to selectively change the boxes in the source boxa.
 * </pre>
 */



Notes:
(1) This finds a set of boxes (boxad) where each edge of each box is
a windowed median smoothed value to the edges of the
input set of boxes (boxas).
(2) Invalid input boxes are filled from nearby ones.
(3) The returned boxad can then be used in boxaModifyWithBoxa()
to selectively change the boxes in the source boxa.
Notes : ( 1 ) This finds a set of boxes ( boxad ) where each edge of each box is a windowed median smoothed value to the edges of the input set of boxes ( boxas ) . ( 2 ) Invalid input boxes are filled from nearby ones . ( 3 ) The returned boxad can then be used in boxaModifyWithBoxa () to selectively change the boxes in the source boxa .
BOXA *
boxaWindowedMedian(BOXA    *boxas,
                   l_int32  halfwin,
                   l_int32  debug)
{
l_int32  n, i, left, top, right, bot;
BOX     *box;
BOXA    *boxaf, *boxad;
NUMA    *nal, *nat, *nar, *nab, *naml, *namt, *namr, *namb;
PIX     *pix1;
    PROCNAME("boxaWindowedMedian");
    if (!boxas)
        return (BOXA *)ERROR_PTR("boxas not defined", procName, NULL);
    if ((n = boxaGetCount(boxas)) < 3) {
        L_WARNING("less than 3 boxes; returning a copy\n", procName);
        return boxaCopy(boxas, L_COPY);
    }
    if (halfwin <= 0) {
        L_WARNING("halfwin must be > 0; returning copy\n", procName);
        return boxaCopy(boxas, L_COPY);
    }
    if ((boxaf = boxaFillSequence(boxas, L_USE_ALL_BOXES, debug)) == NULL)
        return (BOXA *)ERROR_PTR("filled boxa not made", procName, NULL);
    boxaExtractAsNuma(boxaf, &nal, &nat, &nar, &nab, NULL, NULL, 0);
    naml = numaWindowedMedian(nal, halfwin);
    namt = numaWindowedMedian(nat, halfwin);
    namr = numaWindowedMedian(nar, halfwin);
    namb = numaWindowedMedian(nab, halfwin);
    n = boxaGetCount(boxaf);
    boxad = boxaCreate(n);
    for (i = 0; i < n; i++) {
        numaGetIValue(naml, i, &left);
        numaGetIValue(namt, i, &top);
        numaGetIValue(namr, i, &right);
        numaGetIValue(namb, i, &bot);
        box = boxCreate(left, top, right - left + 1, bot - top + 1);
        boxaAddBox(boxad, box, L_INSERT);
    }
    if (debug) {
        lept_mkdir("lept/windowed");
        boxaPlotSides(boxaf, NULL, NULL, NULL, NULL, NULL, &pix1);
        pixWrite("/tmp/lept/windowed/plotsides1.png", pix1, IFF_PNG);
        pixDestroy(&pix1);
        boxaPlotSides(boxad, NULL, NULL, NULL, NULL, NULL, &pix1);
        pixWrite("/tmp/lept/windowed/plotsides2.png", pix1, IFF_PNG);
        pixDestroy(&pix1);
        boxaPlotSizes(boxaf, NULL, NULL, NULL, &pix1);
        pixWrite("/tmp/lept/windowed/plotsizes1.png", pix1, IFF_PNG);
        pixDestroy(&pix1);
        boxaPlotSizes(boxad, NULL, NULL, NULL, &pix1);
        pixWrite("/tmp/lept/windowed/plotsizes2.png", pix1, IFF_PNG);
        pixDestroy(&pix1);
    }
    boxaDestroy(&boxaf);
    numaDestroy(&nal);
    numaDestroy(&nat);
    numaDestroy(&nar);
    numaDestroy(&nab);
    numaDestroy(&naml);
    numaDestroy(&namt);
    numaDestroy(&namr);
    numaDestroy(&namb);
    return boxad;
}
BOXA * boxaWindowedMedian ( BOXA * boxas , l_int32 halfwin , l_int32 debug ) { l_int32 n , i , left , top , right , bot ; BOX * box ; BOXA * boxaf , * boxad ; NUMA * nal , * nat , * nar , * nab , * naml , * namt , * namr , * namb ; PIX * pix1 ; PROCNAME ( " " ) ; if ( ! boxas ) return ( BOXA * ) ERROR_PTR ( " " , procName , NULL ) ; if ( ( n = boxaGetCount ( boxas ) ) < 3 ) { L_WARNING ( " \n " , procName ) ; return boxaCopy ( boxas , L_COPY ) ; } if ( halfwin <= 0 ) { L_WARNING ( " \n " , procName ) ; return boxaCopy ( boxas , L_COPY ) ; } if ( ( boxaf = boxaFillSequence ( boxas , L_USE_ALL_BOXES , debug ) ) == NULL ) return ( BOXA * ) ERROR_PTR ( " " , procName , NULL ) ; boxaExtractAsNuma ( boxaf , & nal , & nat , & nar , & nab , NULL , NULL , 0 ) ; naml = numaWindowedMedian ( nal , halfwin ) ; namt = numaWindowedMedian ( nat , halfwin ) ; namr = numaWindowedMedian ( nar , halfwin ) ; namb = numaWindowedMedian ( nab , halfwin ) ; n = boxaGetCount ( boxaf ) ; boxad = boxaCreate ( n ) ; for ( i = 0 ; i < n ; i ++ ) { numaGetIValue ( naml , i , & left ) ; numaGetIValue ( namt , i , & top ) ; numaGetIValue ( namr , i , & right ) ; numaGetIValue ( namb , i , & bot ) ; box = boxCreate ( left , top , right - left + 1 , bot - top + 1 ) ; boxaAddBox ( boxad , box , L_INSERT ) ; } if ( debug ) { lept_mkdir ( " " ) ; boxaPlotSides ( boxaf , NULL , NULL , NULL , NULL , NULL , & pix1 ) ; pixWrite ( " " , pix1 , IFF_PNG ) ; pixDestroy ( & pix1 ) ; boxaPlotSides ( boxad , NULL , NULL , NULL , NULL , NULL , & pix1 ) ; pixWrite ( " " , pix1 , IFF_PNG ) ; pixDestroy ( & pix1 ) ; boxaPlotSizes ( boxaf , NULL , NULL , NULL , & pix1 ) ; pixWrite ( " " , pix1 , IFF_PNG ) ; pixDestroy ( & pix1 ) ; boxaPlotSizes ( boxad , NULL , NULL , NULL , & pix1 ) ; pixWrite ( " " , pix1 , IFF_PNG ) ; pixDestroy ( & pix1 ) ; } boxaDestroy ( & boxaf ) ; numaDestroy ( & nal ) ; numaDestroy ( & nat ) ; numaDestroy ( & nar ) ; numaDestroy ( & nab ) ; numaDestroy ( & naml ) ; numaDestroy ( & namt ) ; numaDestroy ( & namr ) ; numaDestroy ( & namb ) ; return boxad ; }
\brief   boxaWindowedMedian()
 \param[in]    boxas     source boxa
 \param[in]    halfwin   half width of window over which the median is found
 \param[in]    debug     1 for debug output
 \return  boxad smoothed boxa, or NULL on error
\ brief boxaWindowedMedian () \ param [ in ] boxas source boxa \ param [ in ] halfwin half width of window over which the median is found \ param [ in ] debug 1 for debug output \ return boxad smoothed boxa or NULL on error
/* Fill invalid boxes in the input sequence */ /* Get the windowed median output from each of the sides */