#region 滚动条操作演示 -- 参数传递、亮度与对比度调整
static int Lightness = 50;
static int Contrast_Value = 100;
static void Main(string[] args)
{
Mat src = Cv2.ImRead("lenna.png", ImreadModes.AnyColor);
if (src.Empty())
{
Console.WriteLine("图像未成功加载...");
return;
}
Cv2.ImShow("src image", src);
TrackbarCallbackNative OnLightness = new TrackbarCallbackNative(Trackbar_Lightness);
TrackbarCallbackNative OnContrast = new TrackbarCallbackNative(Trackbar_Contrast);
Cv2.NamedWindow("亮度与对比度调整", WindowFlags.AutoSize);
System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(src);
IntPtr ptr = System.Runtime.InteropServices.GCHandle.ToIntPtr(handle);
Cv2.CreateTrackbar("亮度调整", "亮度与对比度调整", ref Lightness, 100, OnLightness, ptr);
Cv2.CreateTrackbar("对比度调整", "亮度与对比度调整", ref Contrast_Value, 200, OnContrast, ptr);
Cv2.WaitKey();
Cv2.DestroyAllWindows();
}
static void Trackbar_Lightness(int pos, IntPtr userData)
{
System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.FromIntPtr(userData);
Mat src = (Mat)handle.Target;
Mat Temp_Mat = new Mat(src.Height, src.Width, src.Type(), new Scalar(pos, pos, pos));
Mat output_Mat = new Mat(src.Height, src.Width, src.Type(), new Scalar(0, 0, 0));
Cv2.Add(src, Temp_Mat, output_Mat);
Cv2.ImShow("亮度与对比度调整", output_Mat);
}
static void Trackbar_Contrast(int pos, IntPtr userData)
{
System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.FromIntPtr(userData);
Mat src = (Mat)handle.Target;
Mat Temp_Mat = new Mat(src.Height, src.Width, src.Type(), new Scalar(0, 0, 0));
Mat output_Mat = new Mat(src.Height, src.Width, src.Type(), new Scalar(0, 0, 0));
double Contrast = pos / 100.0;
Cv2.AddWeighted(src, Contrast, Temp_Mat,0,0, output_Mat);
Cv2.ImShow("亮度与对比度调整", output_Mat);
}
cv2.addWeighted()函数说明
//public static void AddWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype = -1);
//cv2.addWeighted()函数可以将两张相同shape的图片按权重进行融合,
//dst = src1* alpha + src2* beta + gamma
//参数说明:
//src1 –—— 输入的第一张图片
//alpha —— 第一张图片的权重
//src2 —— 与第一张大小和通道数相同的图片(相同shape)
//beta —— 第二张图片的权重
//dst —— 输出,python中可以直接将dst放在前面作为输出
//gamma —— 加到每个总和上的标量,相当于调亮度
//dtype —— 输出阵列的可选深度,默认值为-1.当两个输入数组具有相同深度时,参数为默认值-1.即为src1.depth()。
效果如下: