Leptonica 1.68
C Image Processing Library

colorseg.c File Reference

Unsupervised color segmentation; selection and display of color content in HSV. More...

#include "allheaders.h"

Go to the source code of this file.

Functions

static l_int32 pixColorSegmentTryCluster (PIX *pixd, PIX *pixs, l_int32 maxdist, l_int32 maxcolors)
PIXpixColorSegment (PIX *pixs, l_int32 maxdist, l_int32 maxcolors, l_int32 selsize, l_int32 finalcolors)
PIXpixColorSegmentCluster (PIX *pixs, l_int32 maxdist, l_int32 maxcolors)
l_int32 pixAssignToNearestColor (PIX *pixd, PIX *pixs, PIX *pixm, l_int32 level, l_int32 *countarray)
l_int32 pixColorSegmentClean (PIX *pixs, l_int32 selsize, l_int32 *countarray)
l_int32 pixColorSegmentRemoveColors (PIX *pixd, PIX *pixs, l_int32 finalcolors)

Variables

static const l_int32 MAX_ALLOWED_ITERATIONS = 20
static const l_float32 DIST_EXPAND_FACT = 1.3
static const l_int32 LEVEL_IN_OCTCUBE = 4

Detailed Description

Unsupervised color segmentation; selection and display of color content in HSV.

Definition in file colorseg.c.


Function Documentation

static l_int32 pixColorSegmentTryCluster ( PIX pixd,
PIX pixs,
l_int32  maxdist,
l_int32  maxcolors 
) [static]

pixColorSegmentTryCluster()

Input: pixd pixs maxdist maxcolors Return: 0 if OK, 1 on error

Note: This function should only be called from pixColorSegCluster()

Definition at line 237 of file colorseg.c.

References COLOR_BLUE, COLOR_GREEN, COLOR_RED, ERROR_INT, FALSE, GET_DATA_BYTE, L_INFO_INT, pixcmapAddNewColor(), pixcmapClear(), pixcmapGetCount(), pixcmapResetColor(), pixGetColormap(), pixGetData(), pixGetHeight(), pixGetWidth(), pixGetWpl(), PROCNAME, SET_DATA_BYTE, and TRUE.

Referenced by pixColorSegmentCluster().

PIX* pixColorSegment ( PIX pixs,
l_int32  maxdist,
l_int32  maxcolors,
l_int32  selsize,
l_int32  finalcolors 
)

pixColorSegment()

Input: pixs (32 bpp; 24-bit color) maxdist (max euclidean dist to existing cluster) maxcolors (max number of colors allowed in first pass) selsize (linear size of sel for closing to remove noise) finalcolors (max number of final colors allowed after 4th pass) Return: pixd (8 bit with colormap), or null on error

Color segmentation proceeds in four phases:

Phase 1: pixColorSegmentCluster() The image is traversed in raster order. Each pixel either becomes the representative for a new cluster or is assigned to an existing cluster. Assignment is greedy. The data is stored in a colormapped image. Three auxiliary arrays are used to hold the colors of the representative pixels, for fast lookup. The average color in each cluster is computed.

Phase 2. pixAssignToNearestColor() A second (non-greedy) clustering pass is performed, where each pixel is assigned to the nearest cluster (average). We also keep track of how many pixels are assigned to each cluster.

Phase 3. pixColorSegmentClean() For each cluster, starting with the largest, do a morphological closing to eliminate small components within larger ones.

Phase 4. pixColorSegmentRemoveColors() Eliminate all colors except the most populated 'finalcolors'. Then remove unused colors from the colormap, and reassign those pixels to the nearest remaining cluster, using the original pixel values.

Notes: (1) The goal is to generate a small number of colors. Typically this would be specified by 'finalcolors', a number that would be somewhere between 3 and 6. The parameter 'maxcolors' specifies the maximum number of colors generated in the first phase. This should be larger than finalcolors, perhaps twice as large. If more than 'maxcolors' are generated in the first phase using the input 'maxdist', the distance is repeatedly increased by a multiplicative factor until the condition is satisfied. The implicit relation between 'maxdist' and 'maxcolors' is thus adjusted programmatically. (2) As a very rough guideline, given a target value of 'finalcolors', here are approximate values of 'maxdist' and 'maxcolors' to start with:

finalcolors maxcolors maxdist ----------- --------- ------- 3 6 100 4 8 90 5 10 75 6 12 60

For a given number of finalcolors, if you use too many maxcolors, the result will be noisy. If you use too few, the result will be a relatively poor assignment of colors.

Definition at line 113 of file colorseg.c.

References CALLOC, ERROR_PTR, FREE, LEVEL_IN_OCTCUBE, NULL, pixAssignToNearestColor(), pixColorSegmentClean(), pixColorSegmentCluster(), pixColorSegmentRemoveColors(), pixGetDepth(), and PROCNAME.

Referenced by main().

PIX* pixColorSegmentCluster ( PIX pixs,
l_int32  maxdist,
l_int32  maxcolors 
)

pixColorSegmentCluster()

Input: pixs (32 bpp; 24-bit color) maxdist (max euclidean dist to existing cluster) maxcolors (max number of colors allowed in first pass) Return: pixd (8 bit with colormap), or null on error

Notes: (1) This is phase 1. See description in pixColorSegment(). (2) Greedy unsupervised classification. If the limit 'maxcolors' is exceeded, the computation is repeated with a larger allowed cluster size. (3) On each successive iteration, 'maxdist' is increased by a constant factor. See comments in pixColorSegment() for a guideline on parameter selection. Note that the diagonal of the 8-bit rgb color cube is about 440, so for 'maxdist' = 440, you are guaranteed to get 1 color!

Definition at line 172 of file colorseg.c.

References DIST_EXPAND_FACT, ERROR_PTR, FALSE, L_INFO_INT2, L_WARNING_INT, MAX_ALLOWED_ITERATIONS, NULL, pixcmapCreate(), pixcmapGetCount(), pixColorSegmentTryCluster(), pixCopyResolution(), pixCreate(), pixDestroy(), pixGetDepth(), pixGetHeight(), pixGetWidth(), pixSetColormap(), PROCNAME, and TRUE.

Referenced by pixColorSegment().

l_int32 pixAssignToNearestColor ( PIX pixd,
PIX pixs,
PIX pixm,
l_int32  level,
l_int32 countarray 
)

pixAssignToNearestColor()

Input: pixd (8 bpp, colormapped) pixs (32 bpp; 24-bit color) pixm (<optional> 1 bpp) level (of octcube used for finding nearest color in cmap) countarray (<optional> ptr to array, in which we can store the number of pixels found in each color in the colormap in pixd) Return: 0 if OK, 1 on error

Notes: (1) This is used in phase 2 of color segmentation, where pixs is the original input image to pixColorSegment(), and pixd is the colormapped image returned from pixColorSegmentCluster(). It is also used, with a mask, in phase 4. (2) This is an in-place operation. (3) The colormap in pixd is unchanged. (4) pixs and pixd must be the same size (w, h). (5) The selection mask pixm can be null. If it exists, it must be the same size as pixs and pixd, and only pixels corresponding to fg in pixm are assigned. Set to NULL if all pixels in pixd are to be assigned. (6) The countarray can be null. If it exists, it is pre-allocated and of a size at least equal to the size of the colormap in pixd. (7) This does a best-fit (non-greedy) assignment of pixels to existing clusters. Specifically, it assigns each pixel in pixd to the color index in the pixd colormap that has a color closest to the corresponding rgb pixel in pixs. (8) 'level' is the octcube level used to quickly find the nearest color in the colormap for each pixel. For color segmentation, this parameter is set to LEVEL_IN_OCTCUBE. (9) We build a mapping table from octcube to colormap index so that this function can run in a time (otherwise) independent of the number of colors in the colormap. This avoids a brute-force search for the closest colormap color to each pixel in the image.

Definition at line 373 of file colorseg.c.

References COLOR_BLUE, COLOR_GREEN, COLOR_RED, ERROR_INT, FREE, GET_DATA_BIT, GET_DATA_BYTE, getOctcubeIndexFromRGB(), L_MANHATTAN_DISTANCE, makeRGBToIndexTables(), NULL, pixcmapToOctcubeLUT(), pixGetColormap(), pixGetData(), pixGetDepth(), pixGetHeight(), pixGetWidth(), pixGetWpl(), PROCNAME, and SET_DATA_BYTE.

Referenced by pixColorSegment(), and pixColorSegmentRemoveColors().

l_int32 pixColorSegmentClean ( PIX pixs,
l_int32  selsize,
l_int32 countarray 
)

pixColorSegmentClean()

Input: pixs (8 bpp, colormapped) selsize (for closing) countarray (ptr to array containing the number of pixels found in each color in the colormap) Return: 0 if OK, 1 on error

Notes: (1) This operation is in-place. (2) This is phase 3 of color segmentation. It is the first part of a two-step noise removal process. Colors with a large population are closed first; this operation absorbs small sets of intercolated pixels of a different color.

Definition at line 466 of file colorseg.c.

References ERROR_INT, L_SORT_DECREASING, NULL, numaAddNumber(), numaCreate(), numaDestroy(), numaGetIValue(), numaGetSortIndex(), pixCloseSafeCompBrick(), pixcmapGetColor32(), pixcmapGetCount(), pixDestroy(), pixGenerateMaskByValue(), pixGetColormap(), pixGetDepth(), pixSetMasked(), pixXor(), and PROCNAME.

Referenced by pixColorSegment().

l_int32 pixColorSegmentRemoveColors ( PIX pixd,
PIX pixs,
l_int32  finalcolors 
)

pixColorSegmentRemoveColors()

Input: pixd (8 bpp, colormapped) pixs (32 bpp rgb, with initial pixel values) finalcolors (max number of colors to retain) Return: 0 if OK, 1 on error

Notes: (1) This operation is in-place. (2) This is phase 4 of color segmentation, and the second part of the 2-step noise removal. Only 'finalcolors' different colors are retained, with colors with smaller populations being replaced by the nearest color of the remaining colors. For highest accuracy, for pixels that are being replaced, we find the nearest colormap color to the original rgb color.

Definition at line 535 of file colorseg.c.

References CALLOC, ERROR_INT, FREE, L_SORT_DECREASING, LEVEL_IN_OCTCUBE, NULL, numaDestroy(), numaGetIValue(), numaGetSortIndex(), pixAssignToNearestColor(), pixcmapGetColor32(), pixcmapGetCount(), pixDestroy(), pixGetCmapHistogram(), pixGetColormap(), pixGetDepth(), pixMakeMaskFromLUT(), pixRemoveUnusedColors(), pixSetMasked(), and PROCNAME.

Referenced by pixColorSegment().


Variable Documentation

const l_int32 MAX_ALLOWED_ITERATIONS = 20 [static]

Definition at line 32 of file colorseg.c.

Referenced by pixColorSegmentCluster().

const l_float32 DIST_EXPAND_FACT = 1.3 [static]

Definition at line 35 of file colorseg.c.

Referenced by pixColorSegmentCluster().

const l_int32 LEVEL_IN_OCTCUBE = 4 [static]

Definition at line 41 of file colorseg.c.

Referenced by pixColorSegment(), and pixColorSegmentRemoveColors().

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines