trans_from_rgb🔗
Short description🔗
trans_from_rgb — Transform an image from the RGB color space to an arbitrary color space.
Signature🔗
trans_from_rgb( image ImageRed, image ImageGreen, image ImageBlue, out image ImageResult1, out image ImageResult2, out image ImageResult3, string ColorSpace )
Description🔗
trans_from_rgb transforms an image from the RGB color
space to an arbitrary color space (ColorSpace).
The three channels of the image are passed as three separate images on
input and output.
The operator trans_from_rgb supports the image types
byte, uint2, int4, and real.
In the case of real images, all values should lay within
0 and 1. If not, the results of the transformation may not be reasonable.
Certain scalings are performed accordingly to the image type:
-
Considering
byteanduint2images, the domain of color space values is generally mapped to the full domain of [0..255] or [0..65535], respectively. Because of this, the origin of signed values (e.g., CIELab) may not be at the center of the domain. -
Hue values are represented by angles of [0..2\(\pi\)[ and are coded for the particular image types differently:
-
byte-images map the angle domain to \([0..255]\). -
uint2/int4-images are coded in minutes of arc \([0..21600[\), except for the transformations 'cielchab' and 'cielchuv' forint4-images, where they are coded in seconds of arc \([0..1296000[\). -
real-images are coded in radians [0..2\(\pi\)[, except for the transformations 'cielchab' and 'cielchuv', where the standards ISO 11664-4:2008 and ISO 11664-5:2009 require the hue to be specified in degrees.
-
-
Saturation values are represented by percentages of \([0..100]\) and are coded differently for the particular image type:
-
byte-images map the saturation values to \([0..255]\). -
uint2/int4-images map the saturation values to \([0..10000]\). -
real-images map the saturation values to \([0..1]\).
-
Supported are the transformations listed below. Note, all domains are based on RGB values scaled to [0; 1]. To obtain the domain of a certain image type, they must be scaled accordingly with the value range. Due to different precision the values obtained using the given equations may slightly differ from the values returned by the operator.
-
‘yiq’
\[\begin{eqnarray*} \begin{array}{ccl} Y & = & 0.299 R + 0.587 G + 0.114 B\\ I & = & -0.27 (B - Y) + 0.74 (B - Y)\\ Q & = & 0.41 (B - Y) + 0.48 (R - Y) \end{array} \end{eqnarray*}\]Value range: \(Y\in[0; 1]\), \(I\in[-0.59947; 0.59947]\), \(Q\in[-0.52243; 0.52243]\)
-
‘yuv’
\[\begin{eqnarray*} \begin{array}{ccl} Y & = & 0.299 R + 0.587 G + 0.114 B\\ U & = & 0.493 (B - Y)\\ V & = & 0.877 (R - Y) \end{array} \end{eqnarray*}\]Note, this implies that \(Y\), \(U\), and \(V\) are not independent of each other.
Value range: \(Y\in [0.0; 1.0]\), \(U \in [-0.436798; 0.436798]\), \(V \in [-0.614777; 0.614777]\)
-
‘argyb’
\[\begin{eqnarray*} \left( \begin{array}{c} A \\ Rg \\ Yb \end{array} \right) = \left( \begin{array}{rrr} 0.30 & 0.59 & 0.11 \\ 0.50 & -0.50 & 0.00 \\ 0.25 & 0.25 & -0.50 \end{array} \right) \left( \begin{array}{c} R \\ G \\ B \end{array} \right) \end{eqnarray*}\]Value range: \(A\in[0; 1]\), \(Rg\in[-0.5; 0.5]\), \(Yb\in[-0.5; 0.5]\)
-
‘ciexyz’
\[\begin{eqnarray*} \left( \begin{array}{c} X \\ Y \\ Z \end{array} \right) = \left( \begin{array}{rrr}0.412453 & 0.357580 & 0.180423 \\ 0.212671 & 0.715160 & 0.072169 \\ 0.019334 & 0.119193 & 0.950227 \end{array} \right) \left( \begin{array}{c} R \\ G \\ B \end{array} \right) \end{eqnarray*}\]The primary colors used correspond to sRGB respectively CIE Rec. 709. D65 is used as white point.
-
Used primary colors \((x, y, z)\):
\[\begin{eqnarray*} \textrm{red}\mbox{:=} \left( \begin{array}{c} 0.6400\\0.3300\\0.0300 \end{array} \right), \quad \textrm{green}\mbox{:=}\left( \begin{array}{c} 0.3000\\0.6000\\0.1000 \end{array} \right), \quad \textrm{blue}\mbox{:=}\left( \begin{array}{c} 0.1500\\0.0600\\0.7900 \end{array} \right), \quad \textrm{white}_{65}\mbox{:=}\left( \begin{array}{c} 0.3127\\0.3290\\0.3583 \end{array} \right) \end{eqnarray*}\]
Value range: \(X\in[0; 0.950456]\), \(Y\in[0; 1]\), \(Z\in[0; 1.088754]\)
-
-
‘hls’
Min := min([R, G, B]) Max := max([R, G, B]) L := (Min + Max) / 2 if (Max == Min) H := 0 S := 0 else if (L > 0.5) S := (Max - Min) / (2 - Max - Min) else S := (Max - Min) / (Max + Min) endif if (R == Max) H := ((G - B) / (Max - Min)) * rad(60) elseif (G == Max) H := (2 + (B - R) / (Max - Min)) * rad(60) elseif (B == Max) H := (4 + (R - G) / (Max - Min)) * rad(60) endif endifValue range: \(H\in[0; 2\pi[\), \(L\in[0; 1]\), \(S\in[0; 1]\)
-
‘hsi’
\[\begin{eqnarray*} \begin{array}{l l l} \left( \begin{array}{c} M1 \\ M2 \\ I1 \end{array} \right) & = & \left( \begin{array}{rrr} \frac{2}{\sqrt{6}} & \frac{-1}{\sqrt{6}} & \frac{-1}{\sqrt{6}} \\ 0 & \frac{1}{\sqrt{2}} & \frac{-1}{\sqrt{2}} \\ \frac{1}{\sqrt{3}} & \frac{1}{\sqrt{3}} & \frac{1}{\sqrt{3}} \end{array} \right) \left( \begin{array}{c} R \\ G \\ B \end{array} \right) \\ \left( \begin{array}{c} H \\ S \\ I \end{array} \right) & = & \left( \begin{array}{c} {\rm atan2}\left(M2,M1\right) \\ \sqrt{M1^{2} + M2^{2}} \\ \frac{I1}{\sqrt{3}} \end{array} \right) \end{array} \end{eqnarray*}\]Value range: \(H\in[0; 2\pi[\), \(S\in[0; \sqrt{\frac{2}{3}}]\), \(I\in[0; 1]\)
Attention:
uint2/int4-images map the saturation values to \([0..65536]\). -
‘hsv’
Min := min([R, G, B]) Max := max([R, G, B]) V := Max if (Max == Min) S := 0 H := 0 else S := (Max - Min) / Max if (R == Max) H := ((G - B) / (Max - Min)) * rad(60) elseif (G == Max) H := (2 + (B - R) / (Max - Min)) * rad(60) elseif (B == Max) H := (4 + (R - G) / (Max - Min)) * rad(60) endif endifValue range: \(H\in[0; 2\pi[\), \(S\in[0; 1]\), \(V\in[0; 1]\)
-
‘ihs’
Min := min([R, G, B]) Max := max([R, G, B]) I := (R + G + B) / 3 if (I == 0) H := 0 S := 1 else S := 1 - Min / I if (S == 0) H := 0 else X := (R + R - G - B) / 2 Y := (R - G) * (R - G) + (R - B) * (G - B) Z := sqrt(Y) if (Z == 0) H := 0 else H := acos(X / Z) endif if (B > G) H := rad(360) - H endif endif endifValue range: \(I\in[0; 1]\), \(H\in[0; 1]\), \(S\in[0; 1]\)
-
‘cielab’
\[\begin{eqnarray*} \begin{array}{ccll} \left( \begin{array}{c} X \\ Y \\ Z \end{array} \right) & = & \left( \begin{array}{rrr} 0.412453 & 0.357580 & 0.180423 \\ 0.212671 & 0.715160 & 0.072169 \\ 0.019334 & 0.119193 & 0.950227 \end{array} \right) \left( \begin{array}{c} R \\ G \\ B \end{array} \right) \\ \\ L & = & 116 f(\frac{Y}{Y_{w}}) - 16 \\ a & = & 500 (f(\frac{X}{X_{w}}) - f(\frac{Y}{Y_{w}})) \\ b & = & 200 (f(\frac{Y}{Y_{w}}) - f(\frac{Z}{Z_{w}})) \end{array} \end{eqnarray*}\]where
\[\begin{eqnarray*} f(t) = \left\{ \begin{array}{c l} t^{\frac{1}{3}}, & \quad \textrm{if } t > (\frac{6}{29})^{3} \\ \frac{841}{108}*t + \frac{4}{29}, & \quad \textrm{otherwise}\\ \end{array} \right.\\ \end{eqnarray*}\]Black point B: (\(R_{B}\), \(G_{B}\), \(B_{B}\)) = (0, 0, 0)
White point W (according to image type):
-
byte: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (255, 255, 255) -
uint2: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (\(2^{16}-1\), \(2^{16}-1\), \(2^{16}-1\)) -
int4: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (\(2^{31}-1\), \(2^{31}-1\), \(2^{31}-1\)) -
real: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (1.0, 1.0, 1.0)
Value range: \(L\in[0; 100]\), \(a\in[-86.1813; 98.2352]\), \(b\in[-107.8617; 94.4758]\) (
byteanduint2: scaled to the maximum gray value.int4: \(L\) is scaled to the maximum gray value, \(a\) and \(b\) are scaled to the minimum gray value, such that the origin stays at 0.) -
-
‘cielchab’
\[\begin{eqnarray*} \begin{array}{ccll} \left( \begin{array}{c} X \\ Y \\ Z \end{array} \right) & = & \left( \begin{array}{rrr} 0.412453 & 0.357580 & 0.180423 \\ 0.212671 & 0.715160 & 0.072169 \\ 0.019334 & 0.119193 & 0.950227 \end{array} \right) \left( \begin{array}{c} R \\ G \\ B \end{array} \right) \\ \\ L & = & 116 f(\frac{Y}{Y_{w}}) - 16 \\ a & = & 500 (f(\frac{X}{X_{w}}) - f(\frac{Y}{Y_{w}})) \\ b & = & 200 (f(\frac{Y}{Y_{w}}) - f(\frac{Z}{Z_{w}})) \\ C_{ab} & = & \sqrt{a^2+b^2} \\ h_{ab} & = & {\rm atan2}\left(b,a\right) \end{array} \end{eqnarray*}\]where
\[\begin{eqnarray*} f(t) = \left\{ \begin{array}{c l} t^{\frac{1}{3}}, & \quad \textrm{if } t > (\frac{6}{29})^{3} \\ \frac{841}{108}*t + \frac{4}{29}, & \quad \textrm{otherwise}\\ \end{array} \right.\\ \end{eqnarray*}\]\(h_{ab}\) lies
-
between 0° and 90° if \(a\) and \(b\) are both positive,
-
between 90° and 180° if \(a\) is negative and \(b\) is positive,
-
between 180° and 270° if \(a\) and \(b\) are both negative, and
-
between 270° and 360° if \(a\) is positive and \(b\) is negative.
Black point B: (\(R_{B}\), \(G_{B}\), \(B_{B}\)) = (0, 0, 0)
White point W (according to image type):
-
byte: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (255, 255, 255) -
uint2: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (\(2^{16}-1\), \(2^{16}-1\), \(2^{16}-1\)) -
int4: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (\(2^{31}-1\), \(2^{31}-1\), \(2^{31}-1\)) -
real: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (1.0, 1.0, 1.0)
Value range: \(L\in[0; 100]\), \(C\in[0; 133.8086]\), \(h\in[0; 360[\) (
byte: scaled to the maximum gray value.uint2: \(L\) and \(C\) are scaled to the maximum gray value, \(h\) is given in minutes of arc.int4: \(L\) and \(C\) are scaled to the maximum gray value, \(h\) is given in seconds of arc.) -
-
‘cieluv’
\[\begin{eqnarray*} \begin{array}{ccll} \left( \begin{array}{c} X \\ Y \\ Z \end{array} \right) & = & \left( \begin{array}{rrr} 0.412453 & 0.357580 & 0.180423 \\ 0.212671 & 0.715160 & 0.072169 \\ 0.019334 & 0.119193 & 0.950227 \end{array} \right) \left( \begin{array}{c} R \\ G \\ B \end{array} \right) \\ \\ u' & = & 4 X / (X + 15 Y + 3 Z) \\ v' & = & 9 Y / (X + 15 Y + 3 Z) \\ u_{w}' & = & 4 X_{w} / (X_{w} + 15 Y_{w} + 3 Z_{w}) \\ v_{w}' & = & 9 Y_{w} / (X_{w} + 15 Y_{w} + 3 Z_{w}) \\ L & = & 116 f(\frac{Y}{Y_{w}}) - 16 \\ u & = & 13 L (u' - u_{w}') \\ v & = & 13 L (v' - v_{w}') \end{array} \end{eqnarray*}\]where
\[\begin{eqnarray*} f(t) = \left\{ \begin{array}{c l} t^{\frac{1}{3}}, & \quad \textrm{if } t > (\frac{6}{29})^{3} \\ \frac{841}{108}*t + \frac{4}{29} & \quad \textrm{otherwise}\\ \end{array} \right.\\ \end{eqnarray*}\]Black point B: (\(R_{B}\), \(G_{B}\), \(B_{B}\)) = (0, 0, 0)
White point W (according to image type):
-
byte: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (255, 255, 255) -
uint2: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (\(2^{16}-1\), \(2^{16}-1\), \(2^{16}-1\)) -
int4: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (\(2^{31}-1\), \(2^{31}-1\), \(2^{31}-1\)) -
real: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (1.0, 1.0, 1.0)
Value range: \(L\in[0; 100]\), \(u\in[-83.0774; 175.0148]\), \(v\in[-134.1008; 107.3923]\) (
byteanduint2: scaled to the maximum gray value.int4: \(L\) is scaled to the maximum gray value, \(u\) and \(v\) are scaled to the minimum gray value, such that the origin stays at 0.) -
-
‘cielchuv’
\[\begin{eqnarray*} \begin{array}{ccll} \left( \begin{array}{c} X \\ Y \\ Z \end{array} \right) & = & \left( \begin{array}{rrr} 0.412453 & 0.357580 & 0.180423 \\ 0.212671 & 0.715160 & 0.072169 \\ 0.019334 & 0.119193 & 0.950227 \end{array} \right) \left( \begin{array}{c} R \\ G \\ B \end{array} \right) \\ \\ u' & = & 4 X / (X + 15 Y + 3 Z) \\ v' & = & 9 Y / (X + 15 Y + 3 Z) \\ u_{w}' & = & 4 X_{w} / (X_{w} + 15 Y_{w} + 3 Z_{w}) \\ v_{w}' & = & 9 Y_{w} / (X_{w} + 15 Y_{w} + 3 Z_{w}) \\ L & = & 116 f(\frac{Y}{Y_{w}}) - 16 \\ u & = & 13 L (u' - u_{w}') \\ v & = & 13 L (v' - v_{w}') \\ C_{uv} & = & \sqrt{u^2+v^2} \\ h_{uv} & = & {\rm atan2}\left(v,u\right) \end{array} \end{eqnarray*}\]where
\[\begin{eqnarray*} f(t) = \left\{ \begin{array}{c l} t^{\frac{1}{3}}, & \quad \textrm{if } t > (\frac{6}{29})^{3} \\ \frac{841}{108}*t + \frac{4}{29}, & \quad \textrm{otherwise}\\ \end{array} \right.\\ \end{eqnarray*}\]Black point B: (\(R_{B}\), \(G_{B}\), \(B_{B}\)) = (0, 0, 0)
White point W (according to image type):
-
byte: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (255, 255, 255) -
uint2: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (\(2^{16}-1\), \(2^{16}-1\), \(2^{16}-1\)) -
int4: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (\(2^{31}-1\), \(2^{31}-1\), \(2^{31}-1\)) -
real: (\(R_{W}\), \(G_{W}\), \(B_{W}\)) = (1.0, 1.0, 1.0)
Value range: \(L\in[0; 100]\), \(C\in[0; 179.0402]\), \(h\in[0; 360[\) (
byte: scaled to the maximum gray value.uint2: \(L\) and \(C\) are scaled to the maximum gray value, \(h\) is given in minutes of arc.int4: \(L\) and \(C\) are scaled to the maximum gray value, \(h\) is given in seconds of arc.) -
-
‘i1i2i3’
\[\begin{eqnarray*} \left( \begin{array}{c} I1 \\ I2 \\ I3 \end{array} \right) = \left( \begin{array}{rrr} 0.333 & 0.333 & 0.333 \\ 1.0 & 0.0 & -1.0 \\ -0.5 & 1.0 & -0.5 \end{array} \right) \left( \begin{array}{c} R \\ G \\ B \end{array} \right) \end{eqnarray*}\]Value range: \(I1\in[0; 1]\), \(I2\in[-1; 1]\), \(I3\in[-1; 1]\)
-
‘ciexyz2’
\[\begin{eqnarray*} \left( \begin{array}{c} X \\ Y \\ Z \end{array} \right) = \left( \begin{array}{rrr} 0.639175 & 0.175258 & 0.185567 \\ 0.306931 & 0.584158 & 0.108911 \\ 0.000000 & 0.060773 & 0.939227 \end{array} \right) \left( \begin{array}{c} R \\ G \\ B \end{array} \right) \end{eqnarray*}\]Value range: \(X\in[0; 1.0]\), \(Y\in[0; 1.0]\), \(Z\in[0; 1.0]\)
-
‘ciexyz3’
\[\begin{eqnarray*} \left( \begin{array}{c} X \\ Y \\ Z \end{array} \right) = \left( \begin{array}{rrr} 0.618 & 0.177 & 0.205 \\ 0.299 & 0.587 & 0.114 \\ 0.000 & 0.056 & 0.944 \end{array} \right) \left( \begin{array}{c} R \\ G \\ B \end{array} \right) \end{eqnarray*}\]Value range: \(X\in[0; 1]\), \(Y\in[0; 1]\), \(Z\in[0; 1]\)
-
‘ciexyz4’
\[\begin{eqnarray*} \left( \begin{array}{c} X \\ Y \\ Z \end{array} \right) = \left( \begin{array}{rrr} 0.476 & 0.299 & 0.175 \\ 0.262 & 0.656 & 0.082 \\ 0.020 & 0.161 & 0.909 \end{array} \right) \left( \begin{array}{c} R \\ G \\ B \end{array} \right) \end{eqnarray*}\]-
Used primary colors \((x, y, z)\):
\[\begin{eqnarray*} \textrm{red}\mbox{:=}\left( \begin{array}{c} 0.628\\0.346\\0.026 \end{array} \right), \quad \textrm{green}\mbox{:=}\left( \begin{array}{c} 0.268\\0.588\\0.144 \end{array} \right), \textrm{blue}\mbox{:=}\left( \begin{array}{c} 0.150\\0.070\\0.780 \end{array} \right), \quad \textrm{white}_{65}\mbox{:=}\left( \begin{array}{c} 0.313\\0.329\\0.358 \end{array} \right) \end{eqnarray*}\]
Value range: \(X\in[0; 0.95]\), \(Y\in[0; 1]\), \(Z\in[0; 1.09]\)
-
-
‘lms’
\[\begin{eqnarray*} \left( \begin{array}{c} L \\ M \\ S \end{array} \right) = \left( \begin{array}{rrr}0.390469 & 0.549910 & 0.008901 \\ 0.070925 & 0.963118 & 0.001358 \\ 0.023143 & 0.128013 & 0.935976 \end{array} \right) \left( \begin{array}{c} R \\ G \\ B \end{array} \right) \end{eqnarray*}\]This conceptually is a transformation from RGB to CIE XYZ (see 'ciexyz' above) followed by a transformation from CIE XYZ to LMS.
Value range: \(L\in[0; 0.949280]\), \(M\in[0; 1.035401]\), \(S\in[0; 1.087132]\)
Attention🔗
As the calculations are made with a different numerical precision, the
OpenCL implementation of the cielab transformation for images of type int4
is slightly less accurate than the pure C version.
Execution information🔗
Execution information
-
Supports OpenCL compute devices.
-
Multithreading type: reentrant (runs in parallel with non-exclusive operators).
-
Multithreading scope: global (may be called from any thread).
-
Automatically parallelized on domain level.
Parameters🔗
ImageRed (input_object) singlechannelimage(-array) → object (byte / uint2 / int4 / real)
Input image (red channel).
ImageGreen (input_object) singlechannelimage(-array) → object (byte / uint2 / int4 / real)
Input image (green channel).
ImageBlue (input_object) singlechannelimage(-array) → object (byte / uint2 / int4 / real)
Input image (blue channel).
ImageResult1 (output_object) singlechannelimage(-array) → object (byte / uint2 / int4 / real)
Color-transformed output image (channel 1).
ImageResult2 (output_object) singlechannelimage(-array) → object (byte / uint2 / int4 / real)
Color-transformed output image (channel 1).
ImageResult3 (output_object) singlechannelimage(-array) → object (byte / uint2 / int4 / real)
Color-transformed output image (channel 1).
ColorSpace (input_control) string → (string)
Color space of the output image.
Default: 'hsv'
List of values: 'argyb', 'cielab', 'cielchab', 'cielchuv', 'cieluv', 'ciexyz', 'ciexyz2', 'ciexyz3', 'ciexyz4', 'hls', 'hsi', 'hsv', 'i1i2i3', 'ihs', 'lms', 'yiq', 'yuv'
List of values (for compute devices): 'cielab', 'cielchab', 'cielchuv', 'cieluv', 'hsi', 'hsv'
Example🔗
(HDevelop)
* Transformation from rgb to hsv and conversely
read_image(Image,'patras')
dev_display(Image)
decompose3(Image, Image1, Image2, Image3)
trans_from_rgb(Image1,Image2,Image3,ImageH,ImageS,ImageV,'hsv')
trans_to_rgb(ImageH,ImageS,ImageV,ImageR,ImageG,ImageB,'hsv')
compose3(ImageR,ImageG,ImageB,Multichannel)
dev_display(Multichannel)
(C)/* Transformation from rgb to hsv and conversely */
read_image(Image,"patras")\;
disp_color(Image,WindowHandle)\;
decompose3(Image,&Rimage,&Gimage,&Bimage)\;
trans_from_rgb(Rimage,Gimage,Bimage,&Image1,&Image2,&Image3,"hsv")\;
trans_to_rgb(Image1,Image2,Image3,&ImageRed,&ImageGreen,&ImageBlue,"hsv")\;
compose3(ImageRed,ImageGreen,ImageBlue,&Multichannel)\;
disp_color(Multichannel,WindowHandle)\;
Result🔗
trans_from_rgb returns 2 (H_MSG_TRUE) if all parameters are
correct. If the input is empty the behavior can be set via
set_system(::'no_object_result', <Result>:). If
necessary, an exception is raised.
Combinations with other operators🔗
Combinations
Possible predecessors
Possible successors
Alternatives
linear_trans_color, rgb1_to_gray, rgb3_to_gray
See also
References🔗
ITU-R BT.470-6: “Conventional Television Systems”, 1998.
ISO 11664-4:2008: ``Colorimetry — Part 4: CIE 1976 Lab* Colour Space’‘, 2008.
ISO 11664-5:2009: ``Colorimetry — Part 5: CIE 1976 Luv* Colour space and u’,v’ uniform chromaticity scale diagram’‘, 2009.
Module🔗
Foundation