/*! * boxOverlapFraction() * * Input: box1, box2 (two boxes) * &fract (<return> the fraction of box2 overlapped by box1) * Return: 0 if OK, 1 on error. * * Notes: * (1) The result depends on the order of the input boxes, * because the overlap is taken as a fraction of box2. */ l_int32 boxOverlapFraction(BOX *box1, BOX *box2, l_float32 *pfract) { l_int32 w2, h2, w, h; BOX *boxo; PROCNAME("boxOverlapFraction"); if (!pfract) return ERROR_INT("&fract not defined", procName, 1); *pfract = 0.0; if (!box1) return ERROR_INT("box1 not defined", procName, 1); if (!box2) return ERROR_INT("box2 not defined", procName, 1); if ((boxo = boxOverlapRegion(box1, box2)) == NULL) /* no overlap */ return 0; boxGetGeometry(box2, NULL, NULL, &w2, &h2); boxGetGeometry(boxo, NULL, NULL, &w, &h); *pfract = (l_float32)(w * h) / (l_float32)(w2 * h2); boxDestroy(&boxo); return 0; }
/*! * boxaClipToBox() * * Input: boxas * box (for clipping) * Return boxad (boxa with boxes in boxas clipped to box), * or null on error * * Notes: * (1) All boxes in boxa not intersecting with box are removed, and * the remaining boxes are clipped to box. */ BOXA * boxaClipToBox(BOXA *boxas, BOX *box) { l_int32 i, n; BOX *boxt, *boxo; BOXA *boxad; PROCNAME("boxaClipToBox"); if (!boxas) return (BOXA *)ERROR_PTR("boxas not defined", procName, NULL); if (!box) return (BOXA *)ERROR_PTR("box not defined", procName, NULL); if ((n = boxaGetCount(boxas)) == 0) return boxaCreate(1); /* empty */ boxad = boxaCreate(0); for (i = 0; i < n; i++) { boxt = boxaGetBox(boxas, i, L_CLONE); if ((boxo = boxOverlapRegion(box, boxt)) != NULL) boxaAddBox(boxad, boxo, L_INSERT); boxDestroy(&boxt); } return boxad; }
l_float32 ComputePairNormalizedOverlapArea(BOX *b1, BOX *b2) { BOX *overlap = boxOverlapRegion(b1, b2); if (!overlap || overlap->w == 0.0 || overlap->h == 0.0) return 0.0; l_float32 area0 = overlap->w * overlap->h; l_float32 area1 = b1->w * b1->h; l_float32 area2 = b2->w * b2->h; l_float32 oarea = 2.0 * area0 / (area1 + area2); return oarea; }