144 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
145 if (pixGetDepth(pixs) != 32)
146 return (
PIX *)ERROR_PTR(
"must be rgb color", __func__, NULL);
151 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
153 lept_mkdir(
"lept/segment");
154 pixWriteDebug(
"/tmp/lept/segment/colorseg1.png", pixd, IFF_PNG);
158 countarray = (l_int32 *)LEPT_CALLOC(256,
sizeof(l_int32));
161 pixWriteDebug(
"/tmp/lept/segment/colorseg2.png", pixd, IFF_PNG);
165 LEPT_FREE(countarray);
167 pixWriteDebug(
"/tmp/lept/segment/colorseg3.png", pixd, IFF_PNG);
204l_int32 w, h, newmaxdist, ret, niters, ncolors, success;
209 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
210 if (pixGetDepth(pixs) != 32)
211 return (
PIX *)ERROR_PTR(
"must be rgb color", __func__, NULL);
213 pixGetDimensions(pixs, &w, &h, NULL);
214 if ((pixd = pixCreate(w, h, 8)) == NULL)
215 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
216 cmap = pixcmapCreate(8);
217 pixSetColormap(pixd, cmap);
218 pixCopyResolution(pixd, pixs);
220 newmaxdist = maxdist;
225 maxcolors, debugflag);
228 ncolors = pixcmapGetCount(cmap);
230 L_INFO(
"Success with %d colors after %d iters\n", __func__,
234 if (niters == MAX_ALLOWED_ITERATIONS) {
235 L_WARNING(
"too many iters; newmaxdist = %d\n",
236 __func__, newmaxdist);
240 newmaxdist = (l_int32)(DIST_EXPAND_FACT * (l_float32)newmaxdist);
245 return (
PIX *)ERROR_PTR(
"failure in phase 1", __func__, NULL);
274l_int32 rmap[256], gmap[256], bmap[256];
275l_int32 w, h, wpls, wpld, i, j, k, found, ret, index, ncolors;
276l_int32 rval, gval, bval, dist2, maxdist2;
277l_int32 countarray[256];
278l_int32 rsum[256], gsum[256], bsum[256];
280l_uint32 *datas, *datad, *lines, *lined;
284 return ERROR_INT(
"pixs not defined", __func__, 1);
286 return ERROR_INT(
"pixd not defined", __func__, 1);
288 w = pixGetWidth(pixs);
289 h = pixGetHeight(pixs);
290 maxdist2 = maxdist * maxdist;
291 cmap = pixGetColormap(pixd);
293 for (k = 0; k < 256; k++) {
294 rsum[k] = gsum[k] = bsum[k] = 0;
295 rmap[k] = gmap[k] = bmap[k] = 0;
298 datas = pixGetData(pixs);
299 datad = pixGetData(pixd);
300 wpls = pixGetWpl(pixs);
301 wpld = pixGetWpl(pixd);
303 for (i = 0; i < h; i++) {
304 lines = datas + i * wpls;
305 lined = datad + i * wpld;
306 for (j = 0; j < w; j++) {
311 ncolors = pixcmapGetCount(cmap);
313 for (k = 0; k < ncolors; k++) {
314 dist2 = (rval - rmap[k]) * (rval - rmap[k]) +
315 (gval - gmap[k]) * (gval - gmap[k]) +
316 (bval - bmap[k]) * (bval - bmap[k]);
317 if (dist2 <= maxdist2) {
328 ret = pixcmapAddNewColor(cmap, rval, gval, bval, &index);
332 if (ret == 0 && index < maxcolors) {
333 countarray[index] = 1;
343 L_INFO(
"maxcolors exceeded for maxdist = %d\n",
353 for (k = 0; k < ncolors; k++) {
354 rval = rsum[k] / countarray[k];
355 gval = gsum[k] / countarray[k];
356 bval = bsum[k] / countarray[k];
357 pixcmapResetColor(cmap, k, rval, gval, bval);
413l_int32 w, h, wpls, wpld, wplm, i, j, success;
414l_int32 rval, gval, bval, index;
417l_uint32 *rtab, *gtab, *btab;
419l_uint32 *datas, *datad, *datam = NULL, *lines, *lined, *linem = NULL;
423 return ERROR_INT(
"pixd not defined", __func__, 1);
424 if ((cmap = pixGetColormap(pixd)) == NULL)
425 return ERROR_INT(
"cmap not found", __func__, 1);
427 return ERROR_INT(
"pixs not defined", __func__, 1);
428 if (pixGetDepth(pixs) != 32)
429 return ERROR_INT(
"pixs not 32 bpp", __func__, 1);
430 if (level < 1 || level > 6)
431 return ERROR_INT(
"level not in [1 ... 6]", __func__, 1);
435 makeRGBToIndexTables(level, &rtab, >ab, &btab);
437 if (!rtab || !gtab || !btab || !cmaptab) {
438 L_ERROR(
"failure to make a table\n", __func__);
443 pixGetDimensions(pixs, &w, &h, NULL);
444 datas = pixGetData(pixs);
445 datad = pixGetData(pixd);
446 wpls = pixGetWpl(pixs);
447 wpld = pixGetWpl(pixd);
449 datam = pixGetData(pixm);
450 wplm = pixGetWpl(pixm);
452 for (i = 0; i < h; i++) {
453 lines = datas + i * wpls;
454 lined = datad + i * wpld;
456 linem = datam + i * wplm;
457 for (j = 0; j < w; j++) {
467 getOctcubeIndexFromRGB(rval, gval, bval, rtab, gtab, btab,
470 index = cmaptab[octindex];
482 return (success) ? 0 : 1;
509l_int32 i, ncolors, val;
516 return ERROR_INT(
"pixs not defined", __func__, 1);
517 if (pixGetDepth(pixs) != 8)
518 return ERROR_INT(
"pixs not 8 bpp", __func__, 1);
519 if ((cmap = pixGetColormap(pixs)) == NULL)
520 return ERROR_INT(
"cmap not found", __func__, 1);
522 return ERROR_INT(
"countarray not defined", __func__, 1);
527 ncolors = pixcmapGetCount(cmap);
528 na = numaCreate(ncolors);
529 for (i = 0; i < ncolors; i++)
530 numaAddNumber(na, countarray[i]);
534 return ERROR_INT(
"nasi not made", __func__, 1);
540 for (i = 0; i < ncolors; i++) {
541 numaGetIValue(nasi, i, &val);
542 pixt1 = pixGenerateMaskByValue(pixs, val, 1);
543 pixt2 = pixCloseSafeCompBrick(NULL, pixt1, selsize, selsize);
544 pixXor(pixt2, pixt2, pixt1);
545 pixcmapGetColor32(cmap, val, &val32);
546 pixSetMasked(pixs, pixt2, val32);
579l_int32 i, ncolors, index, tempindex;
587 return ERROR_INT(
"pixd not defined", __func__, 1);
588 if (pixGetDepth(pixd) != 8)
589 return ERROR_INT(
"pixd not 8 bpp", __func__, 1);
590 if ((cmap = pixGetColormap(pixd)) == NULL)
591 return ERROR_INT(
"cmap not found", __func__, 1);
593 return ERROR_INT(
"pixs not defined", __func__, 1);
594 ncolors = pixcmapGetCount(cmap);
595 if (finalcolors >= ncolors)
604 na = pixGetCmapHistogram(pixd, 1);
607 return ERROR_INT(
"nasi not made", __func__, 1);
609 numaGetIValue(nasi, finalcolors - 1, &tempindex);
610 pixcmapGetColor32(cmap, tempindex, &tempcolor);
611 tab = (l_int32 *)LEPT_CALLOC(256,
sizeof(l_int32));
612 for (i = finalcolors; i < ncolors; i++) {
613 numaGetIValue(nasi, i, &index);
617 pixm = pixMakeMaskFromLUT(pixd, tab);
627 pixSetMasked(pixd, pixm, tempcolor);
631 pixRemoveUnusedColors(pixd);
PIX * pixColorSegment(PIX *pixs, l_int32 maxdist, l_int32 maxcolors, l_int32 selsize, l_int32 finalcolors, l_int32 debugflag)
pixColorSegment()
static l_int32 pixColorSegmentTryCluster(PIX *pixd, PIX *pixs, l_int32 maxdist, l_int32 maxcolors, l_int32 debugflag)
pixColorSegmentTryCluster()
l_ok pixAssignToNearestColor(PIX *pixd, PIX *pixs, PIX *pixm, l_int32 level, l_int32 *countarray)
pixAssignToNearestColor()