Home >Web Front-end >JS Tutorial >Javascript Image Processing—Brightness and Contrast Application Case_Basic Knowledge

Javascript Image Processing—Brightness and Contrast Application Case_Basic Knowledge

WBOY
WBOYOriginal
2016-05-16 17:44:341759browse

Foreword

In the previous article , we explained the convolution operation and smoothing (that is, blur) processing in image processing. In this article we change the brightness and contrast.

Actually, what is brightness?

The brightness is quite eye-catching...

In fact, for the RGBA color space, brightening actually means increasing the three channels of R, G, and B at the same time, so darkening means decreasing at the same time.

This is easier to understand, because the darkest black is RGB(0,0,0), and the brightest white is RGB(255,255,255). Therefore, when brightening, each RGB channel should be increased.

So, what about contrast?

Contrast is actually a difference in color.

So for the RGBA color space, increasing the contrast is actually equal to multiplying the three channels of R, G, and B by a ratio at the same time, because in this way the gap between similar colors becomes larger, so reducing it means dividing by Yes.

For example, the original difference between RGB(23,44,55) and RGB(33,44,55) is only 10, but after multiplying by 2, it becomes RGB(46,88,110) and RGB(66,88,110)

, the difference becomes 20, that is, the "color difference" becomes larger.

Linear Model

newRGB = Contrast * RGB Brightness

The linear model satisfies the above formula, where Contrast represents the contrast coefficient, and Brightness represents the brightness coefficient.

The linear model is relatively simple to implement, but it is easy to adjust all-white or all-black pictures. For ordinary users, it is difficult to choose which one is better for Contrast and Brightness Sure.

So, what is actually used in Photoshop is not a linear model, but a nonlinear model.

Nonlinear model

The contrast increase in the nonlinear model is related to the threshold Threshold:

When Contrast >= 0:

newRGB = RGB (RGB - Threshold) * (1 / (1 - Contrast / 255) - 1)

When Contrast < 0:

newRGB = RGB (RGB - Threshold) * Contrast / 255

What about when contrast and brightness are adjusted 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, the opposite is true, adjust the contrast first, then adjust the brightness.

The last question is, what exactly is the threshold Threshold? In fact, this is the average gray level of the image.

Implementation code

Copy code The code is 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; ] 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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn