如何使用 Emgu C# 对图像进行去饱和处理

How to desaturate an image using Emgu c#

我知道我们通过降低饱和度通道中的值来降低图像的饱和度。我想使用 c#emgu

来完成这个

例如这里是使用 opencvc++ 代码:

Mat Image = imread("images/any.jpg");
// Specify scaling factor
float saturationScale = 0.01;

Mat hsvImage;
// Convert to HSV color space
cv::cvtColor(Image,hsvImage,COLOR_BGR2HSV);

// Convert to float32
hsvImage.convertTo(hsvImage,CV_32F);

vector<Mat>channels(3);
// Split the channels
split(hsvImage,channels);
// Multiply S channel by scaling factor 
channels[1] = channels[1] * saturationScale;

// Clipping operation performed to limit pixel values 
// between 0 and 255
min(channels[1],255,channels[1]);
max(channels[1],0,channels[1]);

// Merge the channels 
merge(channels,hsvImage);

// Convert back from float32
hsvImage.convertTo(hsvImage,CV_8UC3);

Mat imSat;
// Convert to BGR color space
cv::cvtColor(hsvImage,imSat,COLOR_HSV2BGR);

// Display the images
Mat combined;
cv::hconcat(Image, imSat, combined);
namedWindow("Original Image   --   Desaturated Image", CV_WINDOW_AUTOSIZE);

在 c# 中我有:

var img = new Image<Gray, byte>("images/any.jpg");
var imhHsv = img.Convert<Hsv, byte>();
var channels = imhHsv.Split();
// Multiply S channel by scaling factor and clip (limit)
channels[1] = (channels[1] * saturationScale);

我不确定如何将修改后的饱和通道与 imHsv 合并,如果我这样做:

CvInvoke.Merge(channels, imhHsv); 

有错误:

cannot convert 'Emgu.CV.Image[]' to 'Emgu.CV.IInputArrayOfArrays'

我将 VectorOfMat 放入 CvInvoke.Merge 中,它起作用了。

Mat[] m = new Mat[3];
m[0] = CvInvoke.CvArrToMat(channels[0]);
m[1] = CvInvoke.CvArrToMat(channels[1]);
m[2] = CvInvoke.CvArrToMat(channels[2]);

VectorOfMat vm = new VectorOfMat(m);

CvInvoke.Merge(vm, imhHsv);