/*! * wshedApply() * * Input: wshed (generated from wshedCreate()) * Return: 0 if OK, 1 on error * * Iportant note: * (1) This is buggy. It seems to locate watersheds that are * duplicates. The watershed extraction after complete fill * grabs some regions belonging to existing watersheds. * See prog/watershedtest.c for testing. */ l_int32 wshedApply(L_WSHED *wshed) { char two_new_watersheds[] = "Two new watersheds"; char seed_absorbed_into_seeded_basin[] = "Seed absorbed into seeded basin"; char one_new_watershed_label[] = "One new watershed (label)"; char one_new_watershed_index[] = "One new watershed (index)"; char minima_absorbed_into_seeded_basin[] = "Minima absorbed into seeded basin"; char minima_absorbed_by_filler_or_another[] = "Minima absorbed by filler or another"; l_int32 nseeds, nother, nboth, arraysize; l_int32 i, j, val, x, y, w, h, index, mindepth; l_int32 imin, imax, jmin, jmax, cindex, clabel, nindex; l_int32 hindex, hlabel, hmin, hmax, minhindex, maxhindex; l_int32 *lut; l_uint32 ulabel, uval; void **lines8, **linelab32; NUMA *nalut, *nalevels, *nash, *namh, *nasi; NUMA **links; L_HEAP *lh; PIX *pixmin, *pixsd; PIXA *pixad; L_STACK *rstack; PTA *ptas, *ptao; PROCNAME("wshedApply"); if (!wshed) return ERROR_INT("wshed not defined", procName, 1); /* ------------------------------------------------------------ * * Initialize priority queue and pixlab with seeds and minima * * ------------------------------------------------------------ */ lh = lheapCreate(0, L_SORT_INCREASING); /* remove lowest values first */ rstack = lstackCreate(0); /* for reusing the WSPixels */ pixGetDimensions(wshed->pixs, &w, &h, NULL); lines8 = wshed->lines8; /* wshed owns this */ linelab32 = wshed->linelab32; /* ditto */ /* Identify seed (marker) pixels, 1 for each c.c. in pixm */ pixSelectMinInConnComp(wshed->pixs, wshed->pixm, &ptas, &nash); pixsd = pixGenerateFromPta(ptas, w, h); nseeds = ptaGetCount(ptas); for (i = 0; i < nseeds; i++) { ptaGetIPt(ptas, i, &x, &y); uval = GET_DATA_BYTE(lines8[y], x); pushWSPixel(lh, rstack, (l_int32) uval, x, y, i); } wshed->ptas = ptas; nasi = numaMakeConstant(1, nseeds); /* indicator array */ wshed->nasi = nasi; wshed->nash = nash; wshed->nseeds = nseeds; /* Identify minima that are not seeds. Use these 4 steps: * (1) Get the local minima, which can have components * of arbitrary size. This will be a clipping mask. * (2) Get the image of the actual seeds (pixsd) * (3) Remove all elements of the clipping mask that have a seed. * (4) Shrink each of the remaining elements of the minima mask * to a single pixel. */ pixLocalExtrema(wshed->pixs, 200, 0, &pixmin, NULL); pixRemoveSeededComponents(pixmin, pixsd, pixmin, 8, 2); pixSelectMinInConnComp(wshed->pixs, pixmin, &ptao, &namh); nother = ptaGetCount(ptao); for (i = 0; i < nother; i++) { ptaGetIPt(ptao, i, &x, &y); uval = GET_DATA_BYTE(lines8[y], x); pushWSPixel(lh, rstack, (l_int32) uval, x, y, nseeds + i); } wshed->namh = namh; /* ------------------------------------------------------------ * * Initialize merging lookup tables * * ------------------------------------------------------------ */ /* nalut should always give the current after-merging index. * links are effectively backpointers: they are numas associated with * a dest index of all indices in nalut that point to that index. */ mindepth = wshed->mindepth; nboth = nseeds + nother; arraysize = 2 * nboth; wshed->arraysize = arraysize; nalut = numaMakeSequence(0, 1, arraysize); lut = numaGetIArray(nalut); wshed->lut = lut; /* wshed owns this */ links = (NUMA **) CALLOC(arraysize, sizeof(NUMA * )); wshed->links = links; /* wshed owns this */ nindex = nseeds + nother; /* the next unused index value */ /* ------------------------------------------------------------ * * Fill the basins, using the priority queue * * ------------------------------------------------------------ */ pixad = pixaCreate(nseeds); wshed->pixad = pixad; /* wshed owns this */ nalevels = numaCreate(nseeds); wshed->nalevels = nalevels; /* wshed owns this */ L_INFO("nseeds = %d, nother = %d\n", procName, nseeds, nother); while (lheapGetCount(lh) > 0) { popWSPixel(lh, rstack, &val, &x, &y, &index); /* fprintf(stderr, "x = %d, y = %d, index = %d\n", x, y, index); */ ulabel = GET_DATA_FOUR_BYTES(linelab32[y], x); if (ulabel == MAX_LABEL_VALUE) clabel = ulabel; else clabel = lut[ulabel]; cindex = lut[index]; if (clabel == cindex) continue; /* have already seen this one */ if (clabel == MAX_LABEL_VALUE) { /* new one; assign index and try to * propagate to all neighbors */ SET_DATA_FOUR_BYTES(linelab32[y], x, cindex); imin = L_MAX(0, y - 1); imax = L_MIN(h - 1, y + 1); jmin = L_MAX(0, x - 1); jmax = L_MIN(w - 1, x + 1); for (i = imin; i <= imax; i++) { for (j = jmin; j <= jmax; j++) { if (i == y && j == x) continue; uval = GET_DATA_BYTE(lines8[i], j); pushWSPixel(lh, rstack, (l_int32) uval, j, i, cindex); } } } else { /* pixel is already labeled (differently); must resolve */ /* If both indices are seeds, check if the min height is * greater than mindepth. If so, we have two new watersheds; * locate them and assign to both regions a new index * for further waterfill. If not, absorb the shallower * watershed into the deeper one and continue filling it. */ pixGetPixel(pixsd, x, y, &uval); if (clabel < nseeds && cindex < nseeds) { wshedGetHeight(wshed, val, clabel, &hlabel); wshedGetHeight(wshed, val, cindex, &hindex); hmin = L_MIN(hlabel, hindex); hmax = L_MAX(hlabel, hindex); if (hmin == hmax) { hmin = hlabel; hmax = hindex; } if (wshed->debug) { fprintf(stderr, "clabel,hlabel = %d,%d\n", clabel, hlabel); fprintf(stderr, "hmin = %d, hmax = %d\n", hmin, hmax); fprintf(stderr, "cindex,hindex = %d,%d\n", cindex, hindex); if (hmin < mindepth) fprintf(stderr, "Too shallow!\n"); } if (hmin >= mindepth) { debugWshedMerge(wshed, two_new_watersheds, x, y, clabel, cindex); wshedSaveBasin(wshed, cindex, val - 1); wshedSaveBasin(wshed, clabel, val - 1); numaSetValue(nasi, cindex, 0); numaSetValue(nasi, clabel, 0); if (wshed->debug) fprintf(stderr, "nindex = %d\n", nindex); debugPrintLUT(lut, nindex, wshed->debug); mergeLookup(wshed, clabel, nindex); debugPrintLUT(lut, nindex, wshed->debug); mergeLookup(wshed, cindex, nindex); debugPrintLUT(lut, nindex, wshed->debug); nindex++; } else /* extraneous seed within seeded basin; absorb */ { debugWshedMerge(wshed, seed_absorbed_into_seeded_basin, x, y, clabel, cindex); } maxhindex = clabel; /* TODO: is this part of above 'else'? */ minhindex = cindex; if (hindex > hlabel) { maxhindex = cindex; minhindex = clabel; } mergeLookup(wshed, minhindex, maxhindex); } else if (clabel < nseeds && cindex >= nboth) { /* If one index is a seed and the other is a merge of * 2 watersheds, generate a single watershed. */ debugWshedMerge(wshed, one_new_watershed_label, x, y, clabel, cindex); wshedSaveBasin(wshed, clabel, val - 1); numaSetValue(nasi, clabel, 0); mergeLookup(wshed, clabel, cindex); } else if (cindex < nseeds && clabel >= nboth) { debugWshedMerge(wshed, one_new_watershed_index, x, y, clabel, cindex); wshedSaveBasin(wshed, cindex, val - 1); numaSetValue(nasi, cindex, 0); mergeLookup(wshed, cindex, clabel); } else if (clabel < nseeds) { /* cindex from minima; absorb */ /* If one index is a seed and the other is from a minimum, * merge the minimum wshed into the seed wshed. */ debugWshedMerge(wshed, minima_absorbed_into_seeded_basin, x, y, clabel, cindex); mergeLookup(wshed, cindex, clabel); } else if (cindex < nseeds) { /* clabel from minima; absorb */ debugWshedMerge(wshed, minima_absorbed_into_seeded_basin, x, y, clabel, cindex); mergeLookup(wshed, clabel, cindex); } else { /* If neither index is a seed, just merge */ debugWshedMerge(wshed, minima_absorbed_by_filler_or_another, x, y, clabel, cindex); mergeLookup(wshed, clabel, cindex); } } } #if 0 /* Use the indicator array to save any watersheds that fill * to the maximum value. This seems to screw things up! */ for (i = 0; i < nseeds; i++) { numaGetIValue(nasi, i, &ival); if (ival == 1) { wshedSaveBasin(wshed, lut[i], val - 1); numaSetValue(nasi, i, 0); } } #endif numaDestroy(&nalut); pixDestroy(&pixmin); pixDestroy(&pixsd); ptaDestroy(&ptao); lheapDestroy(&lh, TRUE); lstackDestroy(&rstack, TRUE); return 0; }
int main(int argc, char **argv) { l_int32 i, j, w, h, empty; l_uint32 redval, greenval; l_float32 f; L_WSHED *wshed; PIX *pixs, *pixc, *pixd; PIX *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7, *pix8; PIXA *pixac; PTA *pta; static char mainName[] = "watershedtest"; if (argc != 1) return ERROR_INT(" Syntax: watershedtest", mainName, 1); pixac = pixaCreate(0); pixs = pixCreate(500, 500, 8); pixGetDimensions(pixs, &w, &h, NULL); for (i = 0; i < 500; i++) { for (j = 0; j < 500; j++) { #if 1 f = 128.0 + 26.3 * sin(0.0438 * (l_float32) i); f += 33.4 * cos(0.0712 * (l_float32) i); f += 18.6 * sin(0.0561 * (l_float32) j); f += 23.6 * cos(0.0327 * (l_float32) j); #else f = 128.0 + 26.3 * sin(0.0238 * (l_float32)i); f += 33.4 * cos(0.0312 * (l_float32)i); f += 18.6 * sin(0.0261 * (l_float32)j); f += 23.6 * cos(0.0207 * (l_float32)j); #endif pixSetPixel(pixs, j, i, (l_int32) f); } } pixSaveTiled(pixs, pixac, 1.0, 1, 10, 32); pixWrite("/tmp/pattern.png", pixs, IFF_PNG); startTimer(); pixLocalExtrema(pixs, 0, 0, &pix1, &pix2); fprintf(stderr, "Time for extrema: %7.3f\n", stopTimer()); pixSetOrClearBorder(pix1, 2, 2, 2, 2, PIX_CLR); composeRGBPixel(255, 0, 0, &redval); composeRGBPixel(0, 255, 0, &greenval); pixc = pixConvertTo32(pixs); pixPaintThroughMask(pixc, pix2, 0, 0, greenval); pixPaintThroughMask(pixc, pix1, 0, 0, redval); pixSaveTiled(pixc, pixac, 1.0, 0, 10, 32); pixWrite("/tmp/pixc.png", pixc, IFF_PNG); pixSaveTiled(pix1, pixac, 1.0, 0, 10, 32); pixSelectMinInConnComp(pixs, pix1, &pta, NULL); /* ptaWriteStream(stderr, pta, 1); */ pix3 = pixGenerateFromPta(pta, w, h); pixSaveTiled(pix3, pixac, 1.0, 1, 10, 32); pix4 = pixConvertTo32(pixs); pixPaintThroughMask(pix4, pix3, 0, 0, greenval); pixSaveTiled(pix4, pixac, 1.0, 0, 10, 32); pix5 = pixRemoveSeededComponents(NULL, pix3, pix1, 8, 2); pixSaveTiled(pix5, pixac, 1.0, 0, 10, 32); pixZero(pix5, &empty); fprintf(stderr, "Is empty? %d\n", empty); pixDestroy(&pix4); pixDestroy(&pix5); wshed = wshedCreate(pixs, pix3, 10, 0); startTimer(); wshedApply(wshed); fprintf(stderr, "Time for wshed: %7.3f\n", stopTimer()); pix6 = pixaDisplayRandomCmap(wshed->pixad, w, h); pixSaveTiled(pix6, pixac, 1.0, 1, 10, 32); numaWriteStream(stderr, wshed->nalevels); pix7 = wshedRenderFill(wshed); pixSaveTiled(pix7, pixac, 1.0, 0, 10, 32); pix8 = wshedRenderColors(wshed); pixSaveTiled(pix8, pixac, 1.0, 0, 10, 32); wshedDestroy(&wshed); pixd = pixaDisplay(pixac, 0, 0); pixDisplay(pixd, 100, 100); pixWrite("/tmp/wshed.png", pixd, IFF_PNG); pixDestroy(&pixd); pixaDestroy(&pixac); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix6); pixDestroy(&pix7); pixDestroy(&pix8); pixDestroy(&pixs); pixDestroy(&pixc); ptaDestroy(&pta); return 0; }
void DoWatershed(L_REGPARAMS *rp, PIX *pixs) { l_uint8 *data; size_t size; l_int32 w, h, empty; l_uint32 redval, greenval; L_WSHED *wshed; PIX *pixc, *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7, *pix8, *pix9; PIXA *pixa; PTA *pta; /* Find local extrema */ pixa = pixaCreate(0); pixGetDimensions(pixs, &w, &h, NULL); regTestWritePixAndCheck(rp, pixs, IFF_PNG); /* 0 */ pixSaveTiled(pixs, pixa, 1.0, 1, 10, 32); startTimer(); pixLocalExtrema(pixs, 0, 0, &pix1, &pix2); fprintf(stderr, "Time for extrema: %7.3f\n", stopTimer()); pixSetOrClearBorder(pix1, 2, 2, 2, 2, PIX_CLR); composeRGBPixel(255, 0, 0, &redval); composeRGBPixel(0, 255, 0, &greenval); pixc = pixConvertTo32(pixs); pixPaintThroughMask(pixc, pix2, 0, 0, greenval); pixPaintThroughMask(pixc, pix1, 0, 0, redval); regTestWritePixAndCheck(rp, pixc, IFF_PNG); /* 1 */ pixSaveTiled(pixc, pixa, 1.0, 0, 10, 32); regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 2 */ pixSaveTiled(pix1, pixa, 1.0, 0, 10, 32); /* Generate seeds for watershed */ pixSelectMinInConnComp(pixs, pix1, &pta, NULL); pix3 = pixGenerateFromPta(pta, w, h); regTestWritePixAndCheck(rp, pix3, IFF_PNG); /* 3 */ pixSaveTiled(pix3, pixa, 1.0, 1, 10, 32); pix4 = pixConvertTo32(pixs); pixPaintThroughMask(pix4, pix3, 0, 0, greenval); regTestWritePixAndCheck(rp, pix4, IFF_PNG); /* 4 */ pixSaveTiled(pix4, pixa, 1.0, 0, 10, 32); pix5 = pixRemoveSeededComponents(NULL, pix3, pix1, 8, 2); regTestWritePixAndCheck(rp, pix5, IFF_PNG); /* 5 */ pixSaveTiled(pix5, pixa, 1.0, 0, 10, 32); pixZero(pix5, &empty); regTestCompareValues(rp, 1, empty, 0.0); /* 6 */ /* Make and display watershed */ wshed = wshedCreate(pixs, pix3, 10, 0); startTimer(); wshedApply(wshed); fprintf(stderr, "Time for wshed: %7.3f\n", stopTimer()); pix6 = pixaDisplayRandomCmap(wshed->pixad, w, h); regTestWritePixAndCheck(rp, pix6, IFF_PNG); /* 7 */ pixSaveTiled(pix6, pixa, 1.0, 1, 10, 32); numaWriteMem(&data, &size, wshed->nalevels); regTestWriteDataAndCheck(rp, data, size, "na"); /* 8 */ pix7 = wshedRenderFill(wshed); regTestWritePixAndCheck(rp, pix7, IFF_PNG); /* 9 */ pixSaveTiled(pix7, pixa, 1.0, 0, 10, 32); pix8 = wshedRenderColors(wshed); regTestWritePixAndCheck(rp, pix8, IFF_PNG); /* 10 */ pixSaveTiled(pix8, pixa, 1.0, 0, 10, 32); wshedDestroy(&wshed); pix9 = pixaDisplay(pixa, 0, 0); regTestWritePixAndCheck(rp, pix9, IFF_PNG); /* 11 */ pixDisplayWithTitle(pix9, 100, 100, NULL, rp->display); lept_free(data); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); pixDestroy(&pix6); pixDestroy(&pix7); pixDestroy(&pix8); pixDestroy(&pix9); pixDestroy(&pixc); pixaDestroy(&pixa); ptaDestroy(&pta); }