preface

At present, I am preparing a beautifying camera project. This article will introduce some ideas of beautifying filter. The code has been uploaded to MagicCamera, your star and fork are the best support and motivation for me.

implementation

The flow chart

GPUImage

GPUImage is an open source GPU based image or video processing framework, on which we can easily achieve a variety of filter effects. The main implementation of this paper inherits from GPUImageThreeInputFilter’s three inputs: bilateral filtering, edge detection, original image

     // Bilateral filtering
     highp vec4 bilateral = texture2D(inputImageTexture, textureCoordinate);
     // Edge detection
     highp vec4 canny = texture2D(inputImageTexture2, textureCoordinate2);
     / / the original image
     highp vec4 source = texture2D(inputImageTexture3,textureCoordinate3);
Copy the code

Bilateral filtering: on the basis of Gaussian blur, gradient component is added to form weight information to achieve fuzzy smooth image, with the function of edge retention. Gaussian filtering can also be used.

High contrast retention algorithm

The MASK of skin details is obtained through high contrast. According to the location of detailed areas in the MASK, such as spots in the skin, the corresponding area in the original picture is treated with color reduction, so as to achieve the purpose of weakening spots, beautification and making the skin look smooth and natural. Formula = original image – Gaussian fuzzy graph, note: use two sides instead of Gaussian to avoid an additional input source

High contrast retention algorithm

// High contrast retention algorithm
highp vec4 highPass = source - bilateral;
Copy the code

After the high contrast, the image is dim, so I do a bright light operation here

Color = 2 * color1 * color2
mediump float intensity = 24.0;   // Strong light intensity
highPass.r = clamp(2.0 * highPass.r * highPass.r * intensity,0.0.1.0);
highPass.g = clamp(2.0 * highPass.g * highPass.g * intensity,0.0.1.0);
highPass.b = clamp(2.0 * highPass.b * highPass.b * intensity,0.0.1.0);
Copy the code

merge

// Blend -> skin
// Blue channel
mediump float value = clamp((min(source.b, bilateral.b) - 0.2) * 5.0.0.0.1.0);
// The maximum value of RGB
mediump float maxChannelColor = max(max(highPass.r, highPass.g), highPass.b);
// How much skin is worn
mediump float currentIntensity = (1.0 - maxChannelColor / (maxChannelColor + 0.2)) * value * buffingDegree;
/ / mixed
/ / mix = x ⋅ + y ⋅ (1 - a) a
highp vec4 fuse = vec4(mix(source.rgb,bilateral.rgb,currentIntensity), 1.0);
Copy the code

Skin color detection

// Accurate skin polish
lowp float r = source.r;
lowp float g = source.g;
lowp float b = source.b;

highp vec4 result;

if (canny.r < 0.2 && r > 0.3725 && g > 0.1568 && b > 0.0784 && r > b && (max(max(r, g), b) - min(min(r, g), b)) > 0.0588 && abs(r-g) > 0.0588) {
 result = fuse;
} else {
 result = source;
}
Copy the code

Details to adjust

Finally, USM sharpening, HSB enhancing edge details, tone, saturation, brightness, etc.

rendering

reference

Blog.csdn.net/Trent1985/a…

www.jianshu.com/p/945fc806a…

www.jianshu.com/p/5f860f14f…