Objective
In the previous article , we explained the convolution operation and smoothing (i.e. fuzzy) processing in the image processing, this article we carry on the brightness and the contrast change.
Actually, what is brightness?
Brightness is more bright eye slightly ...
In fact, for RGBA color space, brightening is equal to R, G, b three channels at the same time increase, then the dimming is equal to the reduction at the same time.
This is better understood because the darkest black is RGB (0,0,0), and the brightest white is RGB (255,255,255). So lighten should be RGB each channel to increase.
So, what's the contrast?
Contrast, in fact, is the color of the poor.
So for the RGBA color space, the contrast is actually equal to the R, G, b three channels multiplied by a ratio, because such a similar color gap between the larger, then the reduction is at the same time divided by slightly.
For example, the original RGB (23,44,55) and RGB (33,44,55) difference is only 10, but multiplied by 2, it becomes RGB (46,88,110) and rgb (66,88,110)
, the difference has become 20, that is, "color difference" has become larger.
Linear model
Newrgb = Contrast * RGB + brightness
The linear model satisfies the above formula, wherein contrast represents the contrast coefficient, and thebrightness represents the brightness coefficient.
Linear model implementation is relatively simple, but it is easy to call out the whole white or black picture, for ordinary users contrast,brightness Choose how much better also more difficult to determine.
So instead of using linear models in Photoshop, it's not a linear model.
Non-linear model
The contrast increase in the non-linear model is related to the threshold threshold:
When contrast >= 0 o'clock:
Newrgb = RGB + (rgb-threshold) * (1/(1-contrast/255)-1)
When contrast < 0 o'clock:
Newrgb = RGB + (rgb-threshold) * contrast/255
So what happens when contrast and brightness adjust at the same time?
If the contrast is greater than 0, adjust the brightness first, then adjust the contrast, when the contrast is less than 0 o'clock, the contrast, the first adjustment, and then adjust the brightness.
The last question is what the threshold threshold is, in fact, this is the grayscale average of the image.
Implementation code
Copy Code code as follows:
var brightnesscontrast = function (__src, __brightness, __contrast) {
__SRC | | Error (Arguments.callee, is_undefined_or_null/* {line} */);
if (__src.type = = "Cv_rgba") {
var sData = __src.data,
width = __src.col,
Height = __src.row,
DST = new Mat (height, width, cv_rgba),
Ddata = Dst.data,
Brightness = Math.max ( -255, Math.min (255, __brightness | | 0)),
contrast = Math.max ( -255, Math.min (255, __contrast | | 0));
var gray = Cvtcolor (__SRC, Cv_rgba2gray),
Allvalue = 0,
GData = Gray.data;
var y, X, C;
for (y = height; y--;) {
for (x = width; x--;) {
Allvalue + + + gdata[y * width + x];
}
}
var r, G, B, offset, gaverage = (Allvalue/(Height * width)) | 0;
for (y = height; y--;) {
for (x = width; x--;) {
offset = (Y * width + x) * 4;
Ddata[offset] = Sdata[offset] + brightness;
Ddata[offset + 1] = Sdata[offset + 1] + brightness;
Ddata[offset + 2] = Sdata[offset + 2] + brightness;
If (contrast >= 0) {
for (c = 3; c--;) {
if (Ddata[offset + c] >= gaverage) {
Ddata[offset + c] = Ddata[offset + c] + (255-gaverage) * contrast/255;
}else{
Ddata[offset + c] = Ddata[offset + c]-(gaverage * contrast/255);
}
}
}else{
Ddata[offset] = Ddata[offset] + (Ddata[offset]-gaverage) * contrast/255;
Ddata[offset + 1] = Ddata[offset + 1] + (Ddata[offset + 1]-gaverage) * contrast/255;
Ddata[offset + 2] = Ddata[offset + 2] + (Ddata[offset + 2]-gaverage) * contrast/255;
}
Ddata[offset + 3] = 255;
}
}
}else{
Error (Arguments.callee, unspport_data_type/* {line} */);
}
return DST;
};
effect