Below is the C# Logic for changing saturation of image. Referred from here
Note: We are using this saturation matrix
R G B A W
R [sr+s sr sr 0 0]
G [ sg sg+s sg 0 0]
B [ sb sb sb+s 0 0]
A [ 0 0 0 1 0]
W [ 0 0 0 0 1]
s = saturation
sr = (1 - s) * lumR
sg = (1 - s) * lumG
sb = (1 - s) * lumB
lumR = 0.3086 or 0.2125
lumG = 0.6094 or 0.7154
lumB = 0.0820 or 0.0721
Below is the basic snippet of code from c#.
saturation = 0.1 //sample value
float sr = (1 - saturation) * lumR;
float sb = (1 - saturation) * lumG;
float sg = (1 - saturation) * lumB;
float[][] ptsArray = {
new float[] {sr + saturation, sr, sr, 0, 0},
new float[] {sg, sg + saturation, sg, 0, 0},
new float[] {sb, sb, sb + saturation, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
};
// Create ColorMatrix
ColorMatrix clrMatrix = new ColorMatrix(ptsArray);
// Create ImageAttributes
ImageAttributes imgAttribs = new ImageAttributes();
// Set color matrix
imgAttribs.SetColorMatrix(clrMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
Below is code from Python with OpenCV
import cv2
import numpy as np
img = cv2.imread('faceimg.jpg')
imghsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV).astype("float32")
saturation = imghsv[:, :, 1].mean()
print("orginal",saturation)
(h, s, v) = cv2.split(imghsv)
print(cv2.split(imghsv))
s = s*0.1
s = np.clip(s,0,255)
imghsv = cv2.merge([h,s,v])
saturation = imghsv[:, :, 1].mean()
print("after change", saturation)
Checked the result image using OpenCV and C#. Output from C# is more darker in color as compare to python's output when saturation value is 0.1
ColorMatrix approch is high performant and it's easy to implement as our main logic is in C# only.
Question: How to get output as OpenCV-python's saturation output from C#'s color matrix approch