Skip to content

Commit

Permalink
Added pixExtractRectangularRegions()
Browse files Browse the repository at this point in the history
* also improved feedback when fopenReadStream() fails for local directory
  • Loading branch information
DanBloomberg committed Feb 29, 2024
1 parent 5e4f9a6 commit 87af830
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/allheaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,7 @@ LEPT_DLL extern NUMA * pixaFindWidthHeightProduct ( PIXA *pixa );
LEPT_DLL extern l_ok pixFindOverlapFraction ( PIX *pixs1, PIX *pixs2, l_int32 x2, l_int32 y2, l_int32 *tab, l_float32 *pratio, l_int32 *pnoverlap );
LEPT_DLL extern BOXA * pixFindRectangleComps ( PIX *pixs, l_int32 dist, l_int32 minw, l_int32 minh );
LEPT_DLL extern l_ok pixConformsToRectangle ( PIX *pixs, BOX *box, l_int32 dist, l_int32 *pconforms );
LEPT_DLL extern PIX * pixExtractRectangularRegions ( PIX *pixs, BOXA *boxa );
LEPT_DLL extern PIXA * pixClipRectangles ( PIX *pixs, BOXA *boxa );
LEPT_DLL extern PIX * pixClipRectangle ( PIX *pixs, BOX *box, BOX **pboxc );
LEPT_DLL extern PIX * pixClipRectangleWithBorder ( PIX *pixs, BOX *box, l_int32 maxbord, BOX **pboxn );
Expand Down
47 changes: 44 additions & 3 deletions src/pix5.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
* BOXA *pixFindRectangleComps()
* l_int32 pixConformsToRectangle()
*
* Extract rectangular region
* Extract rectangular regions
* PIX *pixExtractRectangularRegions()
* PIXA *pixClipRectangles()
* PIX *pixClipRectangle()
* PIX *pixClipRectangleWithBorder()
Expand Down Expand Up @@ -911,8 +912,48 @@ PIX *pix1, *pix2;


/*-----------------------------------------------------------------------*
* Extract rectangular region *
* Extract rectangular regions *
*-----------------------------------------------------------------------*/
/*!
* \brief pixExtractRectangularRegions()
*
* \param[in] pixs
* \param[in] boxa regions to extract
* \return pix with extracted regions, or NULL on error
*
* <pre>
* Notes:
* (1) The returned pix has the rectangular regions clipped from
* the input pixs.
* (2) We could equally well do this operation using a mask of 1's over
* the regions determined by the boxa:
* pix1 = pixCreateTemplate(pixs);
* pixMaskBoxa(pix1, pix1, boxa, L_SET_PIXELS);
* pixAnd(pix1, pix1, pixs);
* </pre>
*/
PIX *
pixExtractRectangularRegions(PIX *pixs,
BOXA *boxa)
{
l_int32 w, h;
PIX *pix1;
PIXA *pixa1;

if (!pixs)
return (PIX *)ERROR_PTR("pixs not defined", __func__, NULL);
if (!boxa)
return (PIX *)ERROR_PTR("boxa not defined", __func__, NULL);

if ((pixa1 = pixClipRectangles(pixs, boxa)) == NULL)
return (PIX *)ERROR_PTR("pixa1 not made", __func__, NULL);
pixGetDimensions(pixs, &w, &h, NULL);
pix1 = pixaDisplay(pixa1, w, h);
pixaDestroy(&pixa1);
return pix1;
}


/*!
* \brief pixClipRectangles()
*
Expand All @@ -922,7 +963,7 @@ PIX *pix1, *pix2;
*
* <pre>
* Notes:
* (1) The returned pixa includes the actual regions clipped out from
* (1) The boxa in the returned pixa has the regions clipped from
* the input pixs.
* </pre>
*/
Expand Down
8 changes: 6 additions & 2 deletions src/sel1.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ static const l_int32 InitialPtrArraySize = 50; /*!< n'importe quoi */
static const l_uint32 MaxKernelSize = 10000;

/* Bounds on pix template size */
static const l_uint32 MaxPixTemplateSize = 100;
static const l_uint32 MaxPixTemplateHits = 1000;
static const l_uint32 MaxPixTemplateSize = 300;
static const l_uint32 MaxPixTemplateHits = 3000;

/* Static functions */
static l_int32 selaExtendArray(SELA *sela);
Expand Down Expand Up @@ -1949,11 +1949,15 @@ l_uint32 val;
L_ERROR("pix template too large (w = %d, h = %d)\n", __func__, w, h);
return NULL;
}
if (w > MaxPixTemplateSize / 5 || h > MaxPixTemplateSize / 5)
L_WARNING("large pix template: w = %d, h = %d\n", __func__, w, h);
pixCountPixels(pix, &nhits, NULL);
if (nhits > MaxPixTemplateHits) {
L_ERROR("too many hits (%d) in pix template\n", __func__, nhits);
return NULL;
}
if (nhits > MaxPixTemplateHits / 5)
L_WARNING("many hits (%d) in pix template\n", __func__, nhits);

sel = selCreate(h, w, name);
selSetOrigin(sel, cy, cx);
Expand Down
3 changes: 2 additions & 1 deletion src/utils2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,8 @@ FILE *fp;
return (FILE*)ERROR_PTR_1("tail not found", filename, __func__, NULL);
fp = fopen(tail, "rb");
if (!fp)
fp = (FILE *)ERROR_PTR_1("file not found", tail, __func__, NULL);
L_ERROR("failed to open locally with tail %s for filename %s\n",
__func__, tail, filename);
LEPT_FREE(tail);
return fp;
}
Expand Down

0 comments on commit 87af830

Please sign in to comment.