#include <stdio.h>
#include <stdlib.h>
#include "allheaders.h"
Макросы | |
#define | DEBUG_HISTO 1 |
Функции | |
static l_int32 | pixColorSegmentTryCluster (PIX *pixd, PIX *pixs, l_int32 maxdist, l_int32 maxcolors) |
PIX * | pixColorSegment (PIX *pixs, l_int32 maxdist, l_int32 maxcolors, l_int32 selsize, l_int32 finalcolors) |
PIX * | pixColorSegmentCluster (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) |
PIX * | pixMakeRangeMaskHS (PIX *pixs, l_int32 huecenter, l_int32 huehw, l_int32 satcenter, l_int32 sathw, l_int32 regionflag) |
PIX * | pixMakeRangeMaskHV (PIX *pixs, l_int32 huecenter, l_int32 huehw, l_int32 valcenter, l_int32 valhw, l_int32 regionflag) |
PIX * | pixMakeRangeMaskSV (PIX *pixs, l_int32 satcenter, l_int32 sathw, l_int32 valcenter, l_int32 valhw, l_int32 regionflag) |
PIX * | pixMakeHistoHS (PIX *pixs, l_int32 factor, NUMA **pnahue, NUMA **pnasat) |
PIX * | pixMakeHistoHV (PIX *pixs, l_int32 factor, NUMA **pnahue, NUMA **pnaval) |
PIX * | pixMakeHistoSV (PIX *pixs, l_int32 factor, NUMA **pnasat, NUMA **pnaval) |
l_int32 | pixFindHistoPeaksHSV (PIX *pixs, l_int32 type, l_int32 width, l_int32 height, l_int32 npeaks, l_float32 erasefactor, PTA **ppta, NUMA **pnatot, PIXA **ppixa) |
PIX * | displayHSVColorRange (l_int32 hval, l_int32 sval, l_int32 vval, l_int32 huehw, l_int32 sathw, l_int32 nsamp, l_int32 factor) |
Переменные | |
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 |
#define DEBUG_HISTO 1 |
PIX* displayHSVColorRange | ( | l_int32 | hval, | |
l_int32 | sval, | |||
l_int32 | vval, | |||
l_int32 | huehw, | |||
l_int32 | sathw, | |||
l_int32 | nsamp, | |||
l_int32 | factor | |||
) |
Input: hval (hue center value; in range [0 ... 240] sval (saturation center value; in range [0 ... 255] vval (max intensity value; in range [0 ... 255] huehw (half-width of hue range; > 0) sathw (half-width of saturation range; > 0) nsamp (number of samplings in each half-width in hue and sat) factor (linear size of each color square, in pixels; > 3) Return: pixd (32 bpp set of color squares over input range), or null on error
Notes: (1) The total number of color samplings in each of the hue and saturation directions is 2 * nsamp + 1.
l_int32 pixAssignToNearestColor | ( | PIX * | pixd, | |
PIX * | pixs, | |||
PIX * | pixm, | |||
l_int32 | level, | |||
l_int32 * | countarray | |||
) |
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.
PIX* pixColorSegment | ( | PIX * | pixs, | |
l_int32 | maxdist, | |||
l_int32 | maxcolors, | |||
l_int32 | selsize, | |||
l_int32 | finalcolors | |||
) |
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.
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.
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!
Input: pixd (8 bpp, colormapped) pixs (24 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.
static l_int32 pixColorSegmentTryCluster | ( | PIX * | pixd, | |
PIX * | pixs, | |||
l_int32 | maxdist, | |||
l_int32 | maxcolors | |||
) | [static] |
Input: pixd pixs maxdist maxcolors Return: 0 if OK, 1 on error
Note: This function should only be called from pixColorSegCluster()
l_int32 pixFindHistoPeaksHSV | ( | PIX * | pixs, | |
l_int32 | type, | |||
l_int32 | width, | |||
l_int32 | height, | |||
l_int32 | npeaks, | |||
l_float32 | erasefactor, | |||
PTA ** | ppta, | |||
NUMA ** | pnatot, | |||
PIXA ** | ppixa | |||
) |
Input: pixs (32 bpp; HS, HV or SV histogram; not changed) type (L_HS_HISTO, L_HV_HISTO or L_SV_HISTO) width (half width of sliding window) height (half height of sliding window) npeaks (number of peaks to look for) erasefactor (ratio of erase window size to sliding window size) &pta (locations of maximum for each integrated peak area) &natot (integrated peak areas) &pixa (<optional return>=""> pixa for debugging; NULL to skip) Return: 0 if OK, 1 on error
Notes: (1) pixs is a 32 bpp histogram in a pair of HSV colorspace. It should be thought of as a single component with 32 bpc. (2) After each peak is found, the peak is erased with a window that is centered on the peak and scaled from the sliding window by . Typically, is chosen to be > 1.0. (3) Data for a maximum of is returned in and .
Input: pixs (HSV colorspace) factor (subsampling factor; integer) &nahue (<optional return>=""> hue histogram) &nasat (<optional return>=""> saturation histogram) Return: pixd (32 bpp histogram in hue and saturation), or null on error
Notes: (1) pixs is a 32 bpp image in HSV colorspace; hue is in the "red" byte, saturation is in the "green" byte. (2) In pixd, hue is displayed vertically; saturation horizontally. The dimensions of pixd are w = 256, h = 240, and the depth is 32 bpp. The value at each point is simply the number of pixels found at that value of hue and saturation.
Input: pixs (HSV colorspace) factor (subsampling factor; integer) &nahue (<optional return>=""> hue histogram) &naval (<optional return>=""> max intensity (value) histogram) Return: pixd (32 bpp histogram in hue and value), or null on error
Notes: (1) pixs is a 32 bpp image in HSV colorspace; hue is in the "red" byte, max intensity ("value") is in the "blue" byte. (2) In pixd, hue is displayed vertically; intensity horizontally. The dimensions of pixd are w = 256, h = 240, and the depth is 32 bpp. The value at each point is simply the number of pixels found at that value of hue and intensity.
Input: pixs (HSV colorspace) factor (subsampling factor; integer) &nasat (<optional return>=""> sat histogram) &naval (<optional return>=""> max intensity (value) histogram) Return: pixd (32 bpp histogram in sat and value), or null on error
Notes: (1) pixs is a 32 bpp image in HSV colorspace; sat is in the "green" byte, max intensity ("value") is in the "blue" byte. (2) In pixd, sat is displayed vertically; intensity horizontally. The dimensions of pixd are w = 256, h = 256, and the depth is 32 bpp. The value at each point is simply the number of pixels found at that value of saturation and intensity.
PIX* pixMakeRangeMaskHS | ( | PIX * | pixs, | |
l_int32 | huecenter, | |||
l_int32 | huehw, | |||
l_int32 | satcenter, | |||
l_int32 | sathw, | |||
l_int32 | regionflag | |||
) |
Input: pixs (24 bpp rgb) huecenter (center value of hue range) huehw (half-width of hue range) satcenter (center value of saturation range) sathw (half-width of saturation range) regionflag (L_INCLUDE_REGION, L_EXCLUDE_REGION) Return: pixd (1 bpp mask over selected pixels), or null on error
Notes: (1) The pixels are selected based on the specified ranges of hue and saturation. For selection or exclusion, the pixel HS component values must be within both ranges. Care must be taken in finding the hue range because of wrap-around. (2) Use == L_INCLUDE_REGION to take only those pixels within the rectangular region specified in HS space. Use == L_EXCLUDE_REGION to take all pixels except those within the rectangular region specified in HS space.
PIX* pixMakeRangeMaskHV | ( | PIX * | pixs, | |
l_int32 | huecenter, | |||
l_int32 | huehw, | |||
l_int32 | valcenter, | |||
l_int32 | valhw, | |||
l_int32 | regionflag | |||
) |
Input: pixs (24 bpp rgb) huecenter (center value of hue range) huehw (half-width of hue range) valcenter (center value of max intensity range) valhw (half-width of max intensity range) regionflag (L_INCLUDE_REGION, L_EXCLUDE_REGION) Return: pixd (1 bpp mask over selected pixels), or null on error
Notes: (1) The pixels are selected based on the specified ranges of hue and max intensity values. For selection or exclusion, the pixel HV component values must be within both ranges. Care must be taken in finding the hue range because of wrap-around. (2) Use == L_INCLUDE_REGION to take only those pixels within the rectangular region specified in HV space. Use == L_EXCLUDE_REGION to take all pixels except those within the rectangular region specified in HV space.
PIX* pixMakeRangeMaskSV | ( | PIX * | pixs, | |
l_int32 | satcenter, | |||
l_int32 | sathw, | |||
l_int32 | valcenter, | |||
l_int32 | valhw, | |||
l_int32 | regionflag | |||
) |
Input: pixs (24 bpp rgb) satcenter (center value of saturation range) sathw (half-width of saturation range) valcenter (center value of max intensity range) valhw (half-width of max intensity range) regionflag (L_INCLUDE_REGION, L_EXCLUDE_REGION) Return: pixd (1 bpp mask over selected pixels), or null on error
Notes: (1) The pixels are selected based on the specified ranges of saturation and max intensity (val). For selection or exclusion, the pixel SV component values must be within both ranges. (2) Use == L_INCLUDE_REGION to take only those pixels within the rectangular region specified in SV space. Use == L_EXCLUDE_REGION to take all pixels except those within the rectangular region specified in SV space.
const l_float32 DIST_EXPAND_FACT = 1.3 [static] |
const l_int32 LEVEL_IN_OCTCUBE = 4 [static] |
const l_int32 MAX_ALLOWED_ITERATIONS = 20 [static] |