/*! * ptaRotate() * * Input: ptas (for initial points) * (xc, yc) (location of center of rotation) * angle (rotation in radians; clockwise is positive) * (&ptad) (<return> new locations) * Return: 0 if OK; 1 on error * * Notes; * (1) See createMatrix2dScale() for details of transform. */ PTA * ptaRotate(PTA *ptas, l_float32 xc, l_float32 yc, l_float32 angle) { l_int32 i, npts; l_float32 x, y, xp, yp, sina, cosa; PTA *ptad; PROCNAME("ptaRotate"); if (!ptas) return (PTA *)ERROR_PTR("ptas not defined", procName, NULL); npts = ptaGetCount(ptas); if ((ptad = ptaCreate(npts)) == NULL) return (PTA *)ERROR_PTR("ptad not made", procName, NULL); sina = sin(angle); cosa = cos(angle); for (i = 0; i < npts; i++) { ptaGetPt(ptas, i, &x, &y); xp = xc + (x - xc) * cosa - (y - yc) * sina; yp = yc + (x - xc) * sina + (y - yc) * cosa; ptaAddPt(ptad, xp, yp); } return ptad; }
/*! * ptaAffineTransform() * * Input: ptas (for initial points) * mat (3x3 transform matrix; canonical form) * Return: ptad (transformed points), or null on error */ PTA * ptaAffineTransform(PTA *ptas, l_float32 *mat) { l_int32 i, npts; l_float32 vecs[3], vecd[3]; PTA *ptad; PROCNAME("ptaAffineTransform"); if (!ptas) return (PTA *)ERROR_PTR("ptas not defined", procName, NULL); if (!mat) return (PTA *)ERROR_PTR("transform not defined", procName, NULL); vecs[2] = 1; npts = ptaGetCount(ptas); if ((ptad = ptaCreate(npts)) == NULL) return (PTA *)ERROR_PTR("ptad not made", procName, NULL); for (i = 0; i < npts; i++) { ptaGetPt(ptas, i, &vecs[0], &vecs[1]); l_productMatVec(mat, vecs, vecd, 3); ptaAddPt(ptad, vecd[0], vecd[1]); } return ptad; }
/*! * ptaSortByIndex() * * Input: ptas * naindex (na that maps from the new pta to the input pta) * Return: ptad (sorted), or null on error */ PTA * ptaSortByIndex(PTA *ptas, NUMA *naindex) { l_int32 i, index, n; l_float32 x, y; PTA *ptad; PROCNAME("ptaSortByIndex"); if (!ptas) return (PTA *)ERROR_PTR("ptas not defined", procName, NULL); if (!naindex) return (PTA *)ERROR_PTR("naindex not defined", procName, NULL); /* Build up sorted pta using sort index */ n = numaGetCount(naindex); if ((ptad = ptaCreate(n)) == NULL) return (PTA *)ERROR_PTR("ptad not made", procName, NULL); for (i = 0; i < n; i++) { numaGetIValue(naindex, i, &index); ptaGetPt(ptas, index, &x, &y); ptaAddPt(ptad, x, y); } return ptad; }
/*! * ptaGetSortIndex() * * Input: ptas * sorttype (L_SORT_BY_X, L_SORT_BY_Y) * sortorder (L_SORT_INCREASING, L_SORT_DECREASING) * &naindex (<return> index of sorted order into * original array) * Return: 0 if OK, 1 on error */ l_int32 ptaGetSortIndex(PTA *ptas, l_int32 sorttype, l_int32 sortorder, NUMA **pnaindex) { l_int32 i, n; l_float32 x, y; NUMA *na; PROCNAME("ptaGetSortIndex"); if (!pnaindex) return ERROR_INT("&naindex not defined", procName, 1); *pnaindex = NULL; if (!ptas) return ERROR_INT("ptas not defined", procName, 1); if (sorttype != L_SORT_BY_X && sorttype != L_SORT_BY_Y) return ERROR_INT("invalid sort type", procName, 1); if (sortorder != L_SORT_INCREASING && sortorder != L_SORT_DECREASING) return ERROR_INT("invalid sort order", procName, 1); /* Build up numa of specific data */ n = ptaGetCount(ptas); if ((na = numaCreate(n)) == NULL) return ERROR_INT("na not made", procName, 1); for (i = 0; i < n; i++) { ptaGetPt(ptas, i, &x, &y); if (sorttype == L_SORT_BY_X) numaAddNumber(na, x); else numaAddNumber(na, y); } /* Get the sort index for data array */ *pnaindex = numaGetSortIndex(na, sortorder); numaDestroy(&na); if (!*pnaindex) return ERROR_INT("naindex not made", procName, 1); return 0; }
/*! * ptaScale() * * Input: ptas (for initial points) * scalex (horizontal scale factor) * scaley (vertical scale factor) * Return: 0 if OK; 1 on error * * Notes; * (1) See createMatrix2dScale() for details of transform. */ PTA * ptaScale(PTA *ptas, l_float32 scalex, l_float32 scaley) { l_int32 i, npts; l_float32 x, y; PTA *ptad; PROCNAME("ptaScale"); if (!ptas) return (PTA *)ERROR_PTR("ptas not defined", procName, NULL); npts = ptaGetCount(ptas); if ((ptad = ptaCreate(npts)) == NULL) return (PTA *)ERROR_PTR("ptad not made", procName, NULL); for (i = 0; i < npts; i++) { ptaGetPt(ptas, i, &x, &y); ptaAddPt(ptad, scalex * x, scaley * y); } return ptad; }
/*! * dewarpBuildModel() * * Input: dew * debugflag (1 for debugging output) * Return: 0 if OK, 1 on error * * Notes: * (1) This is the basic function that builds the vertical * disparity array, which allows determination of the * src pixel in the input image corresponding to each * dest pixel in the dewarped image. * (2) The method is as follows: * * Estimate the centers of all the long textlines and * fit a LS quadratic to each one. This smooths the curves. * * Sample each curve at a regular interval, find the y-value * of the flat point on each curve, and subtract the sampled * curve value from this value. This is the vertical * disparity. * * Fit a LS quadratic to each set of vertically aligned * disparity samples. This smooths the disparity values * in the vertical direction. Then resample at the same * regular interval, We now have a regular grid of smoothed * vertical disparity valuels. * * Interpolate this grid to get a full resolution disparity * map. This can be applied directly to the src image * pixels to dewarp the image in the vertical direction, * making all textlines horizontal. */ l_int32 dewarpBuildModel(L_DEWARP *dew, l_int32 debugflag) { char *tempname; l_int32 i, j, nlines, nx, ny, sampling; l_float32 c0, c1, c2, x, y, flaty, val; l_float32 *faflats; NUMA *nax, *nafit, *nacurve, *nacurves, *naflat, *naflats, *naflatsi; PIX *pixs, *pixt1, *pixt2; PTA *pta, *ptad; PTAA *ptaa1, *ptaa2, *ptaa3, *ptaa4, *ptaa5, *ptaa6, *ptaa7; FPIX *fpix1, *fpix2, *fpix3; PROCNAME("dewarpBuildModel"); if (!dew) return ERROR_INT("dew not defined", procName, 1); pixs = dew->pixs; if (debugflag) { pixDisplayWithTitle(pixs, 0, 0, "pixs", 1); pixWriteTempfile("/tmp", "pixs.png", pixs, IFF_PNG, NULL); } /* Make initial estimate of centers of textlines */ ptaa1 = pixGetTextlineCenters(pixs, DEBUG_TEXTLINE_CENTERS); if (debugflag) { pixt1 = pixConvertTo32(pixs); pixt2 = pixDisplayPtaa(pixt1, ptaa1); pixWriteTempfile("/tmp", "lines1.png", pixt2, IFF_PNG, NULL); pixDestroy(&pixt1); pixDestroy(&pixt2); } /* Remove all lines that are not near the length * of the longest line. */ ptaa2 = ptaaRemoveShortLines(pixs, ptaa1, 0.8, DEBUG_SHORT_LINES); if (debugflag) { pixt1 = pixConvertTo32(pixs); pixt2 = pixDisplayPtaa(pixt1, ptaa2); pixWriteTempfile("/tmp", "lines2.png", pixt2, IFF_PNG, NULL); pixDestroy(&pixt1); pixDestroy(&pixt2); } nlines = ptaaGetCount(ptaa2); if (nlines < dew->minlines) return ERROR_INT("insufficient lines to build model", procName, 1); /* Do quadratic fit to smooth each line. A single quadratic * over the entire width of the line appears to be sufficient. * Quartics tend to overfit to noise. Each line is thus * represented by three coefficients: c2 * x^2 + c1 * x + c0. * Using the coefficients, sample each fitted curve uniformly * across the full width of the image. */ sampling = dew->sampling; nx = dew->nx; ny = dew->ny; ptaa3 = ptaaCreate(nlines); nacurve = numaCreate(nlines); /* stores curvature coeff c2 */ for (i = 0; i < nlines; i++) { /* for each line */ pta = ptaaGetPta(ptaa2, i, L_CLONE); ptaGetQuadraticLSF(pta, &c2, &c1, &c0, NULL); numaAddNumber(nacurve, c2); ptad = ptaCreate(nx); for (j = 0; j < nx; j++) { /* uniformly sampled in x */ x = j * sampling; applyQuadraticFit(c2, c1, c0, x, &y); ptaAddPt(ptad, x, y); } ptaaAddPta(ptaa3, ptad, L_INSERT); ptaDestroy(&pta); } if (debugflag) { ptaa4 = ptaaCreate(nlines); for (i = 0; i < nlines; i++) { pta = ptaaGetPta(ptaa2, i, L_CLONE); ptaGetArrays(pta, &nax, NULL); ptaGetQuadraticLSF(pta, NULL, NULL, NULL, &nafit); ptad = ptaCreateFromNuma(nax, nafit); ptaaAddPta(ptaa4, ptad, L_INSERT); ptaDestroy(&pta); numaDestroy(&nax); numaDestroy(&nafit); } pixt1 = pixConvertTo32(pixs); pixt2 = pixDisplayPtaa(pixt1, ptaa4); pixWriteTempfile("/tmp", "lines3.png", pixt2, IFF_PNG, NULL); pixDestroy(&pixt1); pixDestroy(&pixt2); ptaaDestroy(&ptaa4); } /* Find and save the flat points in each curve. */ naflat = numaCreate(nlines); for (i = 0; i < nlines; i++) { pta = ptaaGetPta(ptaa3, i, L_CLONE); numaGetFValue(nacurve, i, &c2); if (c2 <= 0) /* flat point at bottom; max value of y in curve */ ptaGetRange(pta, NULL, NULL, NULL, &flaty); else /* flat point at top; min value of y in curve */ ptaGetRange(pta, NULL, NULL, &flaty, NULL); numaAddNumber(naflat, flaty); ptaDestroy(&pta); } /* Sort the lines in ptaa3 by their position */ naflatsi = numaGetSortIndex(naflat, L_SORT_INCREASING); naflats = numaSortByIndex(naflat, naflatsi); nacurves = numaSortByIndex(nacurve, naflatsi); dew->naflats = naflats; dew->nacurves = nacurves; ptaa4 = ptaaSortByIndex(ptaa3, naflatsi); numaDestroy(&naflat); numaDestroy(&nacurve); numaDestroy(&naflatsi); if (debugflag) { tempname = genTempFilename("/tmp", "naflats.na", 0); numaWrite(tempname, naflats); FREE(tempname); } /* Convert the sampled points in ptaa3 to a sampled disparity with * with respect to the flat point in the curve. */ ptaa5 = ptaaCreate(nlines); for (i = 0; i < nlines; i++) { pta = ptaaGetPta(ptaa4, i, L_CLONE); numaGetFValue(naflats, i, &flaty); ptad = ptaCreate(nx); for (j = 0; j < nx; j++) { ptaGetPt(pta, j, &x, &y); ptaAddPt(ptad, x, flaty - y); } ptaaAddPta(ptaa5, ptad, L_INSERT); ptaDestroy(&pta); } if (debugflag) { tempname = genTempFilename("/tmp", "ptaa5.ptaa", 0); ptaaWrite(tempname, ptaa5, 0); FREE(tempname); } /* Generate a ptaa taking vertical 'columns' from ptaa5. * We want to fit the vertical disparity on the column to the * vertical position of the line, which we call 'y' here and * obtain from naflats. */ ptaa6 = ptaaCreate(nx); faflats = numaGetFArray(naflats, L_NOCOPY); for (j = 0; j < nx; j++) { pta = ptaCreate(nlines); for (i = 0; i < nlines; i++) { y = faflats[i]; ptaaGetPt(ptaa5, i, j, NULL, &val); /* disparity value */ ptaAddPt(pta, y, val); } ptaaAddPta(ptaa6, pta, L_INSERT); } if (debugflag) { tempname = genTempFilename("/tmp", "ptaa6.ptaa", 0); ptaaWrite(tempname, ptaa6, 0); FREE(tempname); } /* Do quadratic fit vertically on a subset of pixel columns * for the vertical displacement, which identifies the * src pixel(s) for each dest pixel. Sample the displacement * on a regular grid in the vertical direction. */ ptaa7 = ptaaCreate(nx); /* uniformly sampled across full height of image */ for (j = 0; j < nx; j++) { /* for each column */ pta = ptaaGetPta(ptaa6, j, L_CLONE); ptaGetQuadraticLSF(pta, &c2, &c1, &c0, NULL); ptad = ptaCreate(ny); for (i = 0; i < ny; i++) { /* uniformly sampled in y */ y = i * sampling; applyQuadraticFit(c2, c1, c0, y, &val); ptaAddPt(ptad, y, val); } ptaaAddPta(ptaa7, ptad, L_INSERT); ptaDestroy(&pta); } if (debugflag) { tempname = genTempFilename("/tmp", "ptaa7.ptaa", 0); ptaaWrite(tempname, ptaa7, 0); FREE(tempname); } /* Save the result in a fpix at the specified subsampling */ fpix1 = fpixCreate(nx, ny); for (i = 0; i < ny; i++) { for (j = 0; j < nx; j++) { ptaaGetPt(ptaa7, j, i, NULL, &val); fpixSetPixel(fpix1, j, i, val); } } dew->sampvdispar = fpix1; /* Generate a full res fpix for vertical dewarping. We require that * the size of this fpix is at least as big as the input image. */ fpix2 = fpixScaleByInteger(fpix1, sampling); dew->fullvdispar = fpix2; if (debugflag) { pixt1 = fpixRenderContours(fpix2, -2., 2.0, 0.2); pixWriteTempfile("/tmp", "vert-contours.png", pixt1, IFF_PNG, NULL); pixDisplay(pixt1, 1000, 0); pixDestroy(&pixt1); } /* Generate full res and sampled fpix for horizontal dewarping. This * works to the extent that the line curvature is due to bending * out of the plane normal to the camera, and not wide-angle * "fishbowl" distortion. Also generate the sampled horizontal * disparity array. */ if (dew->applyhoriz) { fpix3 = fpixBuildHorizontalDisparity(fpix2, 0, &dew->extraw); dew->fullhdispar = fpix3; dew->samphdispar = fpixSampledDisparity(fpix3, dew->sampling); if (debugflag) { pixt1 = fpixRenderContours(fpix3, -2., 2.0, 0.2); pixWriteTempfile("/tmp", "horiz-contours.png", pixt1, IFF_PNG, NULL); pixDisplay(pixt1, 1000, 0); pixDestroy(&pixt1); } } dew->success = 1; ptaaDestroy(&ptaa1); ptaaDestroy(&ptaa2); ptaaDestroy(&ptaa3); ptaaDestroy(&ptaa4); ptaaDestroy(&ptaa5); ptaaDestroy(&ptaa6); ptaaDestroy(&ptaa7); return 0; }
/*! * getProjectiveXformCoeffs() * * Input: ptas (source 4 points; unprimed) * ptad (transformed 4 points; primed) * &vc (<return> vector of coefficients of transform) * Return: 0 if OK; 1 on error * * We have a set of 8 equations, describing the projective * transformation that takes 4 points (ptas) into 4 other * points (ptad). These equations are: * * x1' = (c[0]*x1 + c[1]*y1 + c[2]) / (c[6]*x1 + c[7]*y1 + 1) * y1' = (c[3]*x1 + c[4]*y1 + c[5]) / (c[6]*x1 + c[7]*y1 + 1) * x2' = (c[0]*x2 + c[1]*y2 + c[2]) / (c[6]*x2 + c[7]*y2 + 1) * y2' = (c[3]*x2 + c[4]*y2 + c[5]) / (c[6]*x2 + c[7]*y2 + 1) * x3' = (c[0]*x3 + c[1]*y3 + c[2]) / (c[6]*x3 + c[7]*y3 + 1) * y3' = (c[3]*x3 + c[4]*y3 + c[5]) / (c[6]*x3 + c[7]*y3 + 1) * x4' = (c[0]*x4 + c[1]*y4 + c[2]) / (c[6]*x4 + c[7]*y4 + 1) * y4' = (c[3]*x4 + c[4]*y4 + c[5]) / (c[6]*x4 + c[7]*y4 + 1) * * Multiplying both sides of each eqn by the denominator, we get * * AC = B * * where B and C are column vectors * * B = [ x1' y1' x2' y2' x3' y3' x4' y4' ] * C = [ c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] ] * * and A is the 8x8 matrix * * x1 y1 1 0 0 0 -x1*x1' -y1*x1' * 0 0 0 x1 y1 1 -x1*y1' -y1*y1' * x2 y2 1 0 0 0 -x2*x2' -y2*x2' * 0 0 0 x2 y2 1 -x2*y2' -y2*y2' * x3 y3 1 0 0 0 -x3*x3' -y3*x3' * 0 0 0 x3 y3 1 -x3*y3' -y3*y3' * x4 y4 1 0 0 0 -x4*x4' -y4*x4' * 0 0 0 x4 y4 1 -x4*y4' -y4*y4' * * These eight equations are solved here for the coefficients C. * * These eight coefficients can then be used to find the mapping * (x,y) --> (x',y'): * * x' = (c[0]x + c[1]y + c[2]) / (c[6]x + c[7]y + 1) * y' = (c[3]x + c[4]y + c[5]) / (c[6]x + c[7]y + 1) * * that is implemented in projectiveXformSampled() and * projectiveXFormInterpolated(). */ l_int32 getProjectiveXformCoeffs(PTA *ptas, PTA *ptad, l_float32 **pvc) { l_int32 i; l_float32 x1, y1, x2, y2, x3, y3, x4, y4; l_float32 *b; /* rhs vector of primed coords X'; coeffs returned in *pvc */ l_float32 *a[8]; /* 8x8 matrix A */ PROCNAME("getProjectiveXformCoeffs"); if (!ptas) return ERROR_INT("ptas not defined", procName, 1); if (!ptad) return ERROR_INT("ptad not defined", procName, 1); if (!pvc) return ERROR_INT("&vc not defined", procName, 1); if ((b = (l_float32 *)CALLOC(8, sizeof(l_float32))) == NULL) return ERROR_INT("b not made", procName, 1); *pvc = b; ptaGetPt(ptas, 0, &x1, &y1); ptaGetPt(ptas, 1, &x2, &y2); ptaGetPt(ptas, 2, &x3, &y3); ptaGetPt(ptas, 3, &x4, &y4); ptaGetPt(ptad, 0, &b[0], &b[1]); ptaGetPt(ptad, 1, &b[2], &b[3]); ptaGetPt(ptad, 2, &b[4], &b[5]); ptaGetPt(ptad, 3, &b[6], &b[7]); for (i = 0; i < 8; i++) { if ((a[i] = (l_float32 *)CALLOC(8, sizeof(l_float32))) == NULL) return ERROR_INT("a[i] not made", procName, 1); } a[0][0] = x1; a[0][1] = y1; a[0][2] = 1.; a[0][6] = -x1 * b[0]; a[0][7] = -y1 * b[0]; a[1][3] = x1; a[1][4] = y1; a[1][5] = 1; a[1][6] = -x1 * b[1]; a[1][7] = -y1 * b[1]; a[2][0] = x2; a[2][1] = y2; a[2][2] = 1.; a[2][6] = -x2 * b[2]; a[2][7] = -y2 * b[2]; a[3][3] = x2; a[3][4] = y2; a[3][5] = 1; a[3][6] = -x2 * b[3]; a[3][7] = -y2 * b[3]; a[4][0] = x3; a[4][1] = y3; a[4][2] = 1.; a[4][6] = -x3 * b[4]; a[4][7] = -y3 * b[4]; a[5][3] = x3; a[5][4] = y3; a[5][5] = 1; a[5][6] = -x3 * b[5]; a[5][7] = -y3 * b[5]; a[6][0] = x4; a[6][1] = y4; a[6][2] = 1.; a[6][6] = -x4 * b[6]; a[6][7] = -y4 * b[6]; a[7][3] = x4; a[7][4] = y4; a[7][5] = 1; a[7][6] = -x4 * b[7]; a[7][7] = -y4 * b[7]; gaussjordan(a, b, 8); for (i = 0; i < 8; i++) FREE(a[i]); return 0; }
/*! * \brief pixGetSortedNeighborValues() * * \param[in] pixs 8, 16 or 32 bpp, with pixels labeled by c.c. * \param[in] x, y location of pixel * \param[in] conn 4 or 8 connected neighbors * \param[out] pneigh array of integers, to be filled with * the values of the neighbors, if any * \param[out] pnvals the number of unique neighbor values found * \return 0 if OK, 1 on error * * <pre> * Notes: * (1) The returned %neigh array is the unique set of neighboring * pixel values, of size nvals, sorted from smallest to largest. * The value 0, which represents background pixels that do * not belong to any set of connected components, is discarded. * (2) If there are no neighbors, this returns %neigh = NULL; otherwise, * the caller must free the array. * (3) For either 4 or 8 connectivity, the maximum number of unique * neighbor values is 4. * </pre> */ l_int32 pixGetSortedNeighborValues(PIX *pixs, l_int32 x, l_int32 y, l_int32 conn, l_int32 **pneigh, l_int32 *pnvals) { l_int32 i, npt, index; l_int32 neigh[4]; l_uint32 val; l_float32 fx, fy; L_ASET *aset; L_ASET_NODE *node; PTA *pta; RB_TYPE key; PROCNAME("pixGetSortedNeighborValues"); if (pneigh) *pneigh = NULL; if (pnvals) *pnvals = 0; if (!pneigh || !pnvals) return ERROR_INT("&neigh and &nvals not both defined", procName, 1); if (!pixs || pixGetDepth(pixs) < 8) return ERROR_INT("pixs not defined or depth < 8", procName, 1); /* Identify the locations of nearest neighbor pixels */ if ((pta = ptaGetNeighborPixLocs(pixs, x, y, conn)) == NULL) return ERROR_INT("pta of neighbors not made", procName, 1); /* Find the pixel values and insert into a set as keys */ aset = l_asetCreate(L_UINT_TYPE); npt = ptaGetCount(pta); for (i = 0; i < npt; i++) { ptaGetPt(pta, i, &fx, &fy); pixGetPixel(pixs, (l_int32)fx, (l_int32)fy, &val); key.utype = val; l_asetInsert(aset, key); } /* Extract the set keys and put them into the %neigh array. * Omit the value 0, which indicates the pixel doesn't * belong to one of the sets of connected components. */ node = l_asetGetFirst(aset); index = 0; while (node) { val = node->key.utype; if (val > 0) neigh[index++] = (l_int32)val; node = l_asetGetNext(node); } *pnvals = index; if (index > 0) { *pneigh = (l_int32 *)LEPT_CALLOC(index, sizeof(l_int32)); for (i = 0; i < index; i++) (*pneigh)[i] = neigh[i]; } ptaDestroy(&pta); l_asetDestroy(&aset); return 0; }
/*! * \brief pixConnCompIncrAdd() * * \param[in] pixs 32 bpp, with pixels labeled by c.c. * \param[in] ptaa with each pta of pixel locations indexed by c.c. * \param[out] pncc number of c.c * \param[in] x,y location of added pixel * \param[in] debug 0 for no output; otherwise output whenever * debug <= nvals, up to debug == 3 * \return -1 if nothing happens; 0 if a pixel is added; 1 on error * * <pre> * Notes: * (1) This adds a pixel and updates the labeled connected components. * Before calling this function, initialize the process using * pixConnCompIncrInit(). * (2) As a result of adding a pixel, one of the following can happen, * depending on the number of neighbors with non-zero value: * (a) nothing: the pixel is already a member of a c.c. * (b) no neighbors: a new component is added, increasing the * number of c.c. * (c) one neighbor: the pixel is added to an existing c.c. * (d) more than one neighbor: the added pixel causes joining of * two or more c.c., reducing the number of c.c. A maximum * of 4 c.c. can be joined. * (3) When two c.c. are joined, the pixels in the larger index are * relabeled to those of the smaller in pixs, and their locations * are transferred to the pta with the smaller index in the ptaa. * The pta corresponding to the larger index is then deleted. * (4) This is an efficient implementation of a "union-find" operation, * which supports the generation and merging of disjoint sets * of pixels. This function can be called about 1.3 million times * per second. * </pre> */ l_int32 pixConnCompIncrAdd(PIX *pixs, PTAA *ptaa, l_int32 *pncc, l_float32 x, l_float32 y, l_int32 debug) { l_int32 conn, i, j, w, h, count, nvals, ns, firstindex; l_uint32 val; l_int32 *neigh; PTA *ptas, *ptad; PROCNAME("pixConnCompIncrAdd"); if (!pixs || pixGetDepth(pixs) != 32) return ERROR_INT("pixs not defined or not 32 bpp", procName, 1); if (!ptaa) return ERROR_INT("ptaa not defined", procName, 1); if (!pncc) return ERROR_INT("&ncc not defined", procName, 1); conn = pixs->special; if (conn != 4 && conn != 8) return ERROR_INT("connectivity must be 4 or 8", procName, 1); pixGetDimensions(pixs, &w, &h, NULL); if (x < 0 || x >= w) return ERROR_INT("invalid x pixel location", procName, 1); if (y < 0 || y >= h) return ERROR_INT("invalid y pixel location", procName, 1); pixGetPixel(pixs, x, y, &val); if (val > 0) /* already belongs to a set */ return -1; /* Find unique neighbor pixel values in increasing order of value. * If %nvals > 0, these are returned in the %neigh array, which * is of size %nvals. Note that the pixel values in each * connected component are used as the index into the pta * array of the ptaa, giving the pixel locations. */ pixGetSortedNeighborValues(pixs, x, y, conn, &neigh, &nvals); /* If there are no neighbors, just add a new component */ if (nvals == 0) { count = ptaaGetCount(ptaa); pixSetPixel(pixs, x, y, count); ptas = ptaCreate(1); ptaAddPt(ptas, x, y); ptaaAddPta(ptaa, ptas, L_INSERT); *pncc += 1; LEPT_FREE(neigh); return 0; } /* Otherwise, there is at least one neighbor. Add the pixel * to the first neighbor c.c. */ firstindex = neigh[0]; pixSetPixel(pixs, x, y, firstindex); ptaaAddPt(ptaa, neigh[0], x, y); if (nvals == 1) { if (debug == 1) fprintf(stderr, "nvals = %d: neigh = (%d)\n", nvals, neigh[0]); LEPT_FREE(neigh); return 0; } /* If nvals > 1, there are at least 2 neighbors, so this pixel * joins at least one pair of existing c.c. Join each component * to the first component in the list, which is the one with * the smallest integer label. This is done in two steps: * (a) re-label the pixels in the component to the label of the * first component, and * (b) save the pixel locations in the pta for the first component. */ if (nvals == 2) { if (debug >= 1 && debug <= 2) { fprintf(stderr, "nvals = %d: neigh = (%d,%d)\n", nvals, neigh[0], neigh[1]); } } else if (nvals == 3) { if (debug >= 1 && debug <= 3) { fprintf(stderr, "nvals = %d: neigh = (%d,%d,%d)\n", nvals, neigh[0], neigh[1], neigh[2]); } } else { /* nvals == 4 */ if (debug >= 1 && debug <= 4) { fprintf(stderr, "nvals = %d: neigh = (%d,%d,%d,%d)\n", nvals, neigh[0], neigh[1], neigh[2], neigh[3]); } } ptad = ptaaGetPta(ptaa, firstindex, L_CLONE); for (i = 1; i < nvals; i++) { ptas = ptaaGetPta(ptaa, neigh[i], L_CLONE); ns = ptaGetCount(ptas); for (j = 0; j < ns; j++) { /* relabel pixels */ ptaGetPt(ptas, j, &x, &y); pixSetPixel(pixs, x, y, firstindex); } ptaJoin(ptad, ptas, 0, -1); /* add relabeled pixel locations */ *pncc -= 1; ptaDestroy(&ptaa->pta[neigh[i]]); ptaDestroy(&ptas); /* the clone */ } ptaDestroy(&ptad); /* the clone */ LEPT_FREE(neigh); return 0; }