namespace referencePiiColors
Functions for transforming colors.
Unless stated otherwise, color transform functions assume linear RGB. Almost all digitally stored image data are however gamma-adjusted and therefore non-linear. For example, JPEG and MPEG store colors in a non-linear form, and almost all consumer digital cameras apply gamma adjustment to the colors. Therefore, gamma correction needs to be applied to the input images. Otherwise, color transformation results will not be theoretically correct. It is up to the user if this matters in practice, but you have been warned. It can be assumed that digital cameras typically apply a gamma of 1/2.2. To linearize the colors, one needs to invert the gamma compression:
using namespace PiiColors; PiiColor<> nonlinearRgbColor; PiiColor<> linearRgbColor = correctGamma(nonlinearRgbColor, 2.2);
Classes
| class |
An adaptable binary function that performs gamma correction. |
| class |
An adaptable binary function that performs gamma correction on scaled image channels. |
| struct |
An adaptable binary function that multiplies a color with a 3-by-3 matrix. |
| struct |
An adaptable unary function that converts an HSV color to an RGB color. |
| struct |
An adaptable binary function that converts an L*a*b* color to an XYZ color, given the white point of the imaging equipment in XYZ. |
| struct |
A unary function for converting color distances to "likelihoods". |
| struct |
An adaptable unary function that inverses the order of color channels. |
| struct |
An adaptable unary function that converts an RGB color to an HSV color. |
| struct |
An adaptable binary function that converts a non-linear RGB color to Y709 luminance. |
| class |
An adaptable unary function for converting from non-linear RGB to Y'CbCr. |
| struct |
An adaptable unary function for converting from non-linear RGB to Y'PbPr. |
| struct |
An adaptable binary function that converts an XYZ color to an L*a*b* color, given the white point of the imaging equipment in XYZ. |
| struct |
An adaptable unary function for converting from Y'CbCr to non-linear RGB. |
| struct |
An adaptable unary function for converting from Y'PbPr to non-linear RGB. |
Functions
|
PII_COLORS_EXPORT PiiMatrix< float >
|
Calculate the autocorrelogram of an indexed color image. |
|
PII_COLORS_EXPORT PiiMatrix< float >
|
Calculate the autocorrelogram of an indexed olor image. |
|
template<class T>
T
|
(
Apply gamma correction to a color channel. |
|
template<class T>
PiiColor< T >
|
Apply gamma correction to all color channels of |
|
template<class T>
PiiColor4< T >
|
Apply gamma correction to the first three color channels of |
|
template<class T>
PiiColor< T >
|
Apply gamma correction to all channels of |
|
template<class T>
PiiColor4< T >
|
Apply gamma correction to the first three color channels of |
|
template<class T>
PiiMatrix< T >
|
Apply gamma correction to all pixels in |
|
template<class T>
T
|
Apply gamma correction to a color channel. |
|
template<class T>
PiiMatrix< T >
|
Apply gamma correction to all pixels in |
|
template<class Clr>
|
Convert all colors in an image with a generic conversion matrix. |
|
template<class Clr>
Clr
|
(
Convert a color in an HSV color space into an RGB color space. |
|
template<class Clr>
PiiMatrix< Clr >
|
Convert an HSV color image into an RGB color image. |
|
template<class Clr>
Clr
|
(
Convert a color in the CIE 1976 L*a*b* color space into the CIE XYZ color space. |
|
template<class Clr>
PiiMatrix< Clr >
|
Convert an L*a*b* color image into an XYZ color image. |
|
template<class ColorType>
void
|
Measure the distribution of colors in an image. |
|
template<class ColorType>
|
Project colors into a new base and translate to a new center. |
|
template<class T>
void
|
Convert an RGB color image to normalized RGB and separate two of the normalized color layers. |
|
template<class Clr>
Clr
|
(
Reverse the order of color channels. |
|
template<class Clr>
PiiMatrix< Clr >
|
Reverse the order of color channels in a color image. |
|
template<class Clr>
PiiMatrix< Clr >
|
Convert an RGB color image into an HSV color image. |
|
template<class Clr>
Clr
|
(
Convert a color in an RGB color space into the HSV color space. |
|
template<class Clr>
PiiMatrix< float >
|
Convert a color image from a non-linear RGB color space to Y709 luminance. |
|
template<class Clr>
float
|
(
Convert a color in a non-linear RGB color space to luminance as defined in ITU-R BT.709: |
|
template<class Clr>
Clr
|
(
Convert a non-linear (gamma-adjusted) RGB color into Y'CbCr. |
|
template<class T>
PiiMatrix< T >
|
Convert a color image in a non-linear RGB space into Y'CbCr. |
|
template<class Clr>
Clr
|
(
Convert a non-linear (gamma-adjusted) RGB color into Y'PbPr. |
|
template<class T>
PiiMatrix< T >
|
Convert a color image in a non-linear RGB space into Y'PbPr. |
|
template<class ColorType>
PiiMatrix< int >
|
Convert a color image into indexed colors. |
|
template<class Clr>
PiiMatrix< Clr >
|
Convert an XYZ color image into an L*a*b* color image. |
|
template<class Clr>
Clr
|
(
Convert a color in the CIE XYZ color space into the CIE 1976 L*a*b* color space. |
|
template<class T>
PiiMatrix< T >
|
Convert a color image in a Y'CbCr space to non-linear RGB. |
|
template<class Clr>
Clr
|
(
Convert a Y'CbCr color into non-linear RGB. |
|
template<class T>
PiiMatrix< T >
|
Convert a color image in a Y'PbPr space to non-linear RGB. |
|
template<class Clr>
Clr
|
(
Convert a Y'PbPr color into non-linear RGB. |
Variables
|
PII_COLORS_EXPORT PiiMatrix< float >
|
A conversion matrix from RGB to XYZ, assuming that the RGB is CIE RGB, and that the scene is illuminated with a D65 light source. |
|
PII_COLORS_EXPORT PiiMatrix< float >
|
A conversion matrix from RGB to the maximally independent color space of Ohta and Kanade. |
Function details
-
PII_COLORS_EXPORT PiiMatrix< float > autocorrelogram
#include <PiiColors.h>Calculate the autocorrelogram of an indexed color image.
Autocorrelogram describes the spatial distribution of colors in image. Each entry (d,c) in the autocorrelogram represents the probability that a pair of pixels, whose distance is d, both have color c. In practice, the correlogram is built as a histogram and must be normalized to obtain propabilities. The technique is described in detail in Jing Huang; Kumar, S.R.; Mitra, M.; Wei-Jing Zhu; Zabih, R.: "Image indexing using color correlograms", Computer Vision and Pattern Recognition, 1997. Proceedings., 1997 IEEE Computer Society Conference on, 1997, Pages: 762 -768. This implementation does not use the bogus "optimization" technique reported in the paper.
Parameters
- image
an indexed color image
- maxDistance
measure correlation between colors separated up to this many pixels. The autocorrelogram will be calculated for distances {1, ...,
maxDistance}.- levels
the number of indexed colors in
image. A non-positive number means auto-detect.
Returns
a 1 by
levels*maxDistancematrix representing the autocorrelogram (folded into a row matrix). Note that if the number of quantization levels is auto-detected, the size of the output may change in successive calls. -
PII_COLORS_EXPORT PiiMatrix< float > autocorrelogram
#include <PiiColors.h>Calculate the autocorrelogram of an indexed olor image.
This version of the function makes it possible to manually specify the distances for which the autocorrelogram will be calculated.
Parameters
- image
an indexed color image
- distances
a list of distances for which the autocorrelogram will be calculated.
- levels
the number of indexed colors in
image. A non-positive number means auto-detect.
Returns
a 1 by
levels*maxDistancematrix representing the autocorrelogram (folded into a row matrix). Note that if the number of quantization levels is auto-detected, the size of the output may change in successive calls. -
template<class T>
T correctGamma
(- T value
- double gamma
- double maximum
[inline]#include <PiiColors.h>Apply gamma correction to a color channel.
This version of the function compresses the color channel to [0, 1] before applying the correction and then expands it back to original scale.
Parameters
- value
the value of a color channel, in [0, maximum].
- gamma
the value of gamma, must be positive
- maximum
the maximum value of a color channel
Returns
gamma-adjusted value of the color channel, rescaled to original range. If
Tis an integer type, the result will be rounded to closest integer. -
#include <PiiColors.h>Apply gamma correction to all color channels of
clr.Color channels are assumed to be in [0, 1].
-
#include <PiiColors.h>Apply gamma correction to the first three color channels of
clr.Note that the fourth color channel (e.g. alpha) will not be touched. Color channels are assumed to be in [0, 1].
-
#include <PiiColors.h>Apply gamma correction to all channels of
clr.Color channels are assumed to be in [0,
maximum]. -
template<class T>
PiiColor4< T > correctGamma
#include <PiiColors.h>Apply gamma correction to the first three color channels of
clr.Note that the fourth color channel (e.g. alpha) will not be touched. Color channels are assumed to be in [0,
maximum]. -
#include <PiiColors.h>Apply gamma correction to all pixels in
image.The function works with both gray-level and color images. Color channels are assumed to be in [0, 1].
See also
correctGamma(T, double)
-
template<class T>
T correctGamma
[inline]#include <PiiColors.h>Apply gamma correction to a color channel.
Gamma correction is defined as
, where oandistand for output and input. Contemporary display devices are usually assumed to have a gamma of about 2.2. (This is in fact a rough approximation: LG flat panels, for example, measure closer to 1.97.) Linear colors must thus be compressed with a gamma of
to get linear intensity response on screen. Linear intensity response does not however mean linear lightness(perceptual luminance) response; the human vision is more sensitive to lower luminance.This function can be used only with floating-point color channels.
Parameters
- value
the value of a color channel, in [0, 1]. Usually, only positive values are used.
- gamma
the value of gamma, must be positive
- dummy
Returns
gamma-adjusted value of the color channel
-
#include <PiiColors.h>Apply gamma correction to all pixels in
image.The function works with both gray-level and color images. Color channels are assumed to be in [0,
maximum].See also
-
#include <PiiColors.h>Convert all colors in an image with a generic conversion matrix.
This function multiplies the three color channels by a 3-by-3 conversion matrix from left. The result type will always be PiiColor<float>. The function makes no checks. The caller must ensure the size of the conversion matrix.
using namespace PiiColors; PiiMatrix<PiiColor4<> > clrImage; PiiMatrix<PiiColor<float> > converted = genericConversion(clrImage, ohtaKanadeMatrix);
-
template<class Clr>
Clr hsvToRgb
(- const Clr & hsvColor
#include <PiiColors.h>Convert a color in an HSV color space into an RGB color space.
This function works inversely to rgbToHsv() and assumes the same conventions on scaling color channels. The output color channels will be in [0,1] with floating-point types and in [0,255] with integer types.
Parameters
- hsvColor
a color in an HSV color space.
Returns
the color in an RGB color space. The type of the returned color matches that of the input color.
-
#include <PiiColors.h>Convert an HSV color image into an RGB color image.
See also
hsvToRgb(Clr)
-
template<class Clr>
Clr labToXyz
(- const Clr & labColor
- const Clr & whitePoint
#include <PiiColors.h>Convert a color in the CIE 1976 L*a*b* color space into the CIE XYZ color space.
The conversion only works with floating-point color values.
Parameters
- labColor
the L*a*b* color to be converted to XYZ
- whitePoint
the XYZ coordinates of the white point of the imaging equipment. Make sure the white point has no zero color channels.
-
#include <PiiColors.h>Convert an L*a*b* color image into an XYZ color image.
See also
labToXyz(Clr, Clr)
-
template<class ColorType>
void measureColorDistribution
#include <PiiColors.h>Measure the distribution of colors in an image.
This function uses PCA to find the main axes of a three-dimensional color distribution. As a result, it gives the centroid of the distribution and three orthogonal vectors forming a new base. The first of these vectors points to the direction of the largest variance in the color distribution. The length of each vector is inversely proportional to the variance of the color distribution in that direction.
Information about the color distribution can be used to normalize colors and to measure the likelihood that a color belongs to a model distribution. Once you have measured the distribution of colors you can project the colors to the new base and achieve an approximately zero-mean and unit variance color distribution. The second norm of any color coordinates in the normalized coordinate system is inversely proportional to the likelihood of the color belongin to the model distribution.
This function is temporarily broken.
Parameters
- clrImage
- baseVectors
a 3-by-3 matrix return-value matrix in which columns represent the three main axes of the color distribution. If the three vectors are used as a new base for the color distribution, it'll have a unit variance in x, y, and z directions in the new base.
- center
the 1-by-3 return-value matrix that will store the center of mass of the color distribution.
- maxPixels
the maximum number of pixels to consider. If the input image is large, a subset of its pixels is usually enough to derive a reliable estimate of distribution. Use this paramater to limit the number of pixels used for estimation. -1 means that all pixels will be used.
See also
-
#include <PiiColors.h>Project colors into a new base and translate to a new center.
In the result image, the color distribution will have approximately zero mean and unit variance. Each color c in the input image will be treated as a row vector. The normalized color is given by
, where t is the translation vector (center) A is the matrix representing the new base (baseVectors).Parameters
- clrImage
the input image
- baseVectors
a 3-by-3 matrix in which rows represent a new base for the color system.
- center
a 1-by-3 translation vector
Returns
a new normalized color image
See also
-
template<class T>
void normalizedRgb
#include <PiiColors.h>Convert an RGB color image to normalized RGB and separate two of the normalized color layers.
Normalized RGB is obtained by dividing each channel by the sum of all channels. (
etc.) Note that only two channels are extracted because the third one is reduntant (
).PiiMatrix<PiiColor<> > img(100,100); PiiMatrix<unsigned char> red, green, blue; // Extract normalized RG with 8 bit resolution PiiColors::normalizedRgb(img, red, green); // Extract normalized RB with 5 bit resolution PiiColors::normalizedRgb(img, red, blue, 32, 0, 2);
Parameters
- image
The input image. Any color will do as the content type (PiiColor or PiiColor4).
- ch1
store the first extracted channel to this matrix
- ch2
store the second extracted channel to this matrix
- multiplier
multiply each normalized channel by this value before storing to the output matrix. This is useful as the result often needs to be quantized.
- ch1Index
the BGR index of the first color channel to extract. 0 = r, 1 = g, 2 = b. With PiiColor4, channel index may, in principle, be 3, but the fourth channel is never normalized.
- ch2Index
same for the second channel
-
template<class Clr>
Clr reverseColor
(- const Clr & color
[inline]#include <PiiColors.h>Reverse the order of color channels.
This function can be used to convert RGB to BGR or RGBA to BGRA and vice versa. It just swaps the first and third color channels.
-
#include <PiiColors.h>Reverse the order of color channels in a color image.
-
#include <PiiColors.h>Convert an RGB color image into an HSV color image.
See also
rgbToHsv(Clr)
-
template<class Clr>
Clr rgbToHsv
(- const Clr & rgbColor
#include <PiiColors.h>Convert a color in an RGB color space into the HSV color space.
HSV (hue, saturation, value) is a color space in which a piecewise linear function is used as an approximation to the (non-linear) hue channel value. See QColor for a detailed description of the HSV space.
This implementation deviates from the conventional way of scaling color channels. Typically, hue is represented with values in [0,359] as it is an angular variable. Furthermore, the value channel is often scaled into [0,100]. This convention has a couple of problems:
unsignedcharcan only store values up to 255.Scaling value down from 255 to 100 degrades accuracy.
With floating-point types, angles are conventionally represented as radians, not degrees.
unsignedchar- H, S, and V are all in [0,255]Other integer types - H is [0,359], S and V in [0,255]
Floating-point types - H is in [0,2*pi), S and V in [0,1].
Due to numerical inaccuracies, converting from RGB to HSV and back doesn't necessarily result into the color you started with. The back-and-forth conversion of floating-point types is accurate to at least 1e-6. The conversion error with
unsignedcharis at most three levels on any color channel. The conversion error with other integer types it at most two levels.PiiColor<> rgb(1,2,3); PiiColor<> hsv = PiiColors::rgbToHsv(rgb);
Parameters
- rgbColor
a color in an RGB color space. Floating-point color channels are assumed to be in [0,1]. Integer types are assumed to be in [0,255].
Returns
the color in an HSV color space. The type of the returned color matches that of the input color.
-
#include <PiiColors.h>Convert a color image from a non-linear RGB color space to Y709 luminance.
-
template<class Clr>
float rgbToY709
(- const Clr & clr
[inline]#include <PiiColors.h>Convert a color in a non-linear RGB color space to luminance as defined in ITU-R BT.709:
. -
template<class Clr>
Clr rgbToYcbcr
(- const Clr & clr
- double maximum = < Clr >::max()
[inline]#include <PiiColors.h>Convert a non-linear (gamma-adjusted) RGB color into Y'CbCr.
Y'CbCr is the same as Y'PbPr, but the chroma components are offset to non-negative values.
This function does not use the conventional BT.601 primaries. Instead, the more contemporary BT.709 (HDTV) primaries are used.
Parameters
- clr
a gamma-adjusted RGB color, channels in [0, maximum]
- maximum
maximum value of a color channel
Returns
a Y'CbCr color. Y' (luma), Cb (difference between blue and luma) and Cr (difference between red and luma) are all in [0,
maximum]. All color channels will be rounded to nearest integers. -
#include <PiiColors.h>Convert a color image in a non-linear RGB space into Y'CbCr.
-
template<class Clr>
Clr rgbToYpbpr
(- const Clr & clr
[inline]#include <PiiColors.h>Convert a non-linear (gamma-adjusted) RGB color into Y'PbPr.
Y'PbPr is the analog counterpart of Y'CbCr. The color channels in Y'PbPr are not quantized and thus not suitable for digital representation. Different encodings use different offsets and scaling factors for the color channels.
This function does not use the conventional BT.601 primaries. Instead, the more contemporary BT.709 (HDTV) primaries are used.
Parameters
- clr
a gamma-adjusted RGB color, channels in [0, X]
Returns
a Y'PbPr color. Y' (luma) is in [0, X], Pb (difference between blue and luma) and Pr (difference between red and luma) in [-0.5X, 0.5X]
-
#include <PiiColors.h>Convert a color image in a non-linear RGB space into Y'PbPr.
-
template<class ColorType>
PiiMatrix< int > toIndexed
#include <PiiColors.h>Convert a color image into indexed colors.
This function quantizes each color channel to the specified number of levels. The color index is calculated as
, where R, G, and B represent the quantized color channel values, and
is the number of quantization levels for channel X.Despite the color channel names used here, the function also works with color spaces other than RGB.
Parameters
- clrImage
the input image.
ColorTypemust be either PiiColor or PiiColor4. If color channels are floats, the maximum value is assumed to be 1.0. Otherwise, 255 will be used as max.- redLevels
the number of quantization levels for the red color channel.
- greenLevels
the number of quantization levels for the red color channel. 0 means same as
redLevels.- blueLevels
the number of quantization levels for the blue color channel. 0 means same as
redLevels.
-
#include <PiiColors.h>Convert an XYZ color image into an L*a*b* color image.
See also
xyzToLab(Clr, Clr)
-
template<class Clr>
Clr xyzToLab
(- const Clr & xyzColor
- const Clr & whitePoint
#include <PiiColors.h>Convert a color in the CIE XYZ color space into the CIE 1976 L*a*b* color space.
The conversion only works with floating-point color values.
Parameters
- xyzColor
the XYZ color to be converted to L*a*b*
- whitePoint
the XYZ coordinates of the white point of the imaging equipment. Make sure the white point has no zero color channels.
-
#include <PiiColors.h>Convert a color image in a Y'CbCr space to non-linear RGB.
-
template<class Clr>
Clr ycbcrToRgb
(- const Clr & clr
- double maximum = < Clr >::max()
[inline]#include <PiiColors.h>Convert a Y'CbCr color into non-linear RGB.
This function does not use the conventional BT.601 primaries. Instead, the more contemporary BT.709 (HDTV) primaries are used.
Parameters
- clr
a color in a Y'CbCr color space.
- maximum
the maximum value of a color channel
Returns
a color in a non-linear RGB space. All color channels are in [0, maximum] and rounded to nearest integers. To obtain linear RGB, use the correctGamma() function.
-
#include <PiiColors.h>Convert a color image in a Y'PbPr space to non-linear RGB.
-
template<class Clr>
Clr ypbprToRgb
(- const Clr & clr
[inline]#include <PiiColors.h>Convert a Y'PbPr color into non-linear RGB.
This function does not use the conventional BT.601 primaries. Instead, the more contemporary BT.709 (HDTV) primaries are used.
Parameters
- clr
a color in a Y'PbPr color space.
Returns
a color in a non-linear RGB space.
Variable details
-
PII_COLORS_EXPORT PiiMatrix< float > d65_709_XyzMatrix
A conversion matrix from RGB to XYZ, assuming that the RGB is CIE RGB, and that the scene is illuminated with a D65 light source.
If either of these assumptions does not hold, please do not pretend that you have XYZ.
-
PII_COLORS_EXPORT PiiMatrix< float > ohtaKanadeMatrix
A conversion matrix from RGB to the maximally independent color space of Ohta and Kanade.
Add a note
Not a single note added yet. Be the first, add yours.