Although I have seen the introduction of color space several years ago, I did not write code to do this until today. Although there are already many examples available on the Web, one is only suitable for floating-point data, and on the other hand, there are some optimizations in the implementation.
In addition to the most common RGB color models, there are HSB, YCbCr, XYZ, Lab, and more. HSB is generally used only as a temporary mode in image processing, YCBCR is often applied to image compression processing, while XYZ is distributed strictly according to the sensitivity of the human eye to the light signal.
The lab model will be a bit of a discussion here. Many of the presentations on the web say that lab is based on XYZ, so people generally can only find the conversion between XYZ and lab, whereas RGB to lab conversions can only be done indirectly using XYZ as an intermediate mode. Unfortunately, this situation stems from misunderstanding. In image processing software (such as Photoshop), a simpler algorithm is often used.
We can observe the conversion of RGB to XYZ first:
[ x, y, z] = [M] * [R,g,b]
where M is a 3x3 matrix:
[M] = [0.4125, 0.3576, 0.1805;
0.2126, 0.7152, 0.0722;
0.0193, 0.1192, 0.9505],
RGB is a gamma-corrected color component: R=g (R), g=g (g), B=g (B).
Where RGB is the original color component.
G is the gamma correction function:
When x < 0.018,
g (x) = 4.5318 * x When x >= 0.018,
g (x) = 1.099 * d^0.45-0.099
RGB and RGB values range from [0,1]. When the calculation is complete, the range of XYZ values varies, namely: [0, 0.9506), [0, 1], [0, 1.0890].
and the conversion of XYZ to Lab:
L = * * F (Y1)-16 A = * (f (X1)-F (Y1)) b = * (f (Y1)-F (Z1))
Where f is a correction function similar to the Gamma function:
When x > 0.008856,
f (x) = x^ (1/3) When x <= 0.008856,
f (x) = (7.787 * x) + (16/116) X1, Y1, Z1, respectively, are the XYZ values after linear normalization, that is, their range is [0, 1]. In addition, the range of function f is also the same as the argument [0, 1].
When the calculation is complete, the range of values for L is [0, 100], while A and b are about [-169, +169] and [-160, +160].
Before observing these seemingly complex transformations, one hypothesis we must make is that in image processing software, the absolute value of non-RGB color data is not important, and it is important that they be able to restore as accurately as possible to RGB images to display on related devices such as screens. This hypothesis is the reason why our simplification has been established.
The above conversion from XYZ to lab seems strange at first glance, but if you look closely, it's not hard to see that L and Y1 are just a simple same-interval mapping, which is actually optional (if the mapping is not necessarily caused by the color scale loss).
Thus, the first simplification we have made is:
L = Y1。
Next, look at the mapping process for A and B. It is not difficult to find that A and B are actually a chromatic aberration signal (similar to the nature of CB and CR). As for their conversion coefficients of 500 and 200, we can completely forget that their range is not in line with the expression of the 8-bit integer value. We will calculate the appropriate factor later so that both A and B are within the range of [0, 255].
Because XYZ has to be normalized to x1y1z1, we can actually make this change in the transformation matrix m, multiplying each row by a factor so that the sum of each number of rows is 1:
[M1] = [0.4339, 0.3762 0.1899;
0.2126, 0.7152, 0.0722;
0.0177, 0.1095, 0.8728]
As a result, we get a semi-finished product:
L = Y1 = 0.2126 * R + 0.7152 * G + 0.0722 * B
A = Fa * (x1-y1) + Da
b = Fb * (Y1-Z1) + Db
The FX is the coefficient used to adjust the range, and the DX is a positive number to eliminate negative values of a and B. The selection of FX and DX must make A and b satisfy the distribution of the range on [0, 255].
Next we'll determine the FX and DX values. By M1 it is easy to calculate the range of x1-y1 (extreme cases) to [-86.784, +86.784], while the range of Y1-Z1 is [-204.9536, +204.9536]. As a 1.4749,FB, the value of FA is 0.6245;DA and DB is 128.
In this case, the M1 are:
L = Y1 = 0.2126 * R + 0.7152 * G + 0.0722 * B
A = 1.4749 * (0.2213 * R-0.3390 * G + 0.1177 * B) + 128
b = 0.6245 * (0.1949 * R + 0.6057 * G-0.8006 * B) + 128
The range of RGB and lab values is [0,255].
The last bit of work is the optimization of the algorithm. We can convert this equation group into a constant integer multiplication and shift method (equivalent to using fixed-point numbers). To facilitate reading, I still write the shift as division.
So our final result is:
L = Y1 = (13933 * R + 46871 * G + 4732 * B) div 2^16
A = 377 * (14503 * R-22218 * G + 7714 * B) div 2^24 + 128
b = * (12773 * R + 39695 * G-52468 * B) div 2^24 + 128
The inverse transformation can be deduced in a similar way:
Set l1=l,a1= (a-128) *174,b1= (b-128) *410, with:
R = L1 + (A1 * 100922 + B1 * 17790) div 2^23
G = L1-(A1 * 30176 + B1 * 1481) div 2^23
B = L1 + (A1 * 1740-b1 * 37719) div 2^23
Where the range of RGB and lab values is [0,255], then the inverse gamma function to obtain the original RGB
The above algorithm is compiled and passed in Delphi. After testing, the histogram of the operation is very similar to the image and the results of my Photoshop CS, but there are some differences in amplitude, and I can slowly scrutinize later.
In order to search for a simple RGB direct to the lab algorithm and find all over the network is not, the last resort to self-reliance. Although the time is time-consuming, fortunately there is a slight gain. This is a temporary note in order to benefit posterity. In the meantime, it may be unavoidable mistakes, but also look for the most generous advice.
MSI (weixing_1976<at>163.com) Wednesday, September 1, 2010 12:25 did not understand, ask a question:
The following Rgb2lab formula in the R,g,b is after the gamma correction or the original RGB? How does gamma correction work? Your judgment of the above corrections is the formula in XYZ space.
{
L = Y1 = (13933 * R + 46871 * G + 4732 * B) div 2^16
A = 377 * (14503 * R-22218 * G + 7714 * B) div 2^24 + 128
b = * (12773 * R + 39695 * G-52468 * B) div 2^24 + 128
}
As for the inverse transformation formula
{
Set l1=l,a1= (a-128) *174,b1= (b-128) *410, with:
R = L1 + (A1 * 100922 + B1 * 17790) div 2^23
G = L1-(A1 * 30176 + B1 * 1481) div 2^23
B = L1 + (A1 * 1740-b1 * 37719) div 2^23
}
Where RGB and lab range are [0,255], and then the inverse gamma function to obtain the original RGB????, how to inverse gama?
I finished writing two functions, none of the gamma processing has been transformed, and then the inverse transformation of the resulting image is destroyed.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.