通过trackBar调整值大小(最大值设置为255)。
using OpenCvSharp; using OpenCvSharp.Extensions; using DlibDotNet; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; namespace app { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } /// <summary> /// 色相 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void trackBarHue_Scroll(object sender, EventArgs e) { Mat src = new Mat(@"img\jzh.png", ImreadModes.Color); Cv2.Resize(src, src, new OpenCvSharp.Size(300, 300), 0, 0, InterpolationFlags.Linear); this.picSrc.Image = BitmapConverter.ToBitmap(src); lblHueNum.Text = trackBarHue.Value.ToString(); Mat hsv = new Mat(); Cv2.CvtColor(src, hsv, ColorConversionCodes.BGR2HSV); Mat[] channels = Cv2.Split(hsv); // 遍历所有像素 for (int i = 0; i < channels[0].Rows; i++) { for (int j = 0; j < channels[0].Cols; j++) { channels[0].Set(i, j, trackBarHue.Value); } } // 合并通道 Cv2.Merge(channels, hsv); Mat newImage = new Mat(); Cv2.CvtColor(hsv, newImage, ColorConversionCodes.HSV2BGR); this.picDest.Image = BitmapConverter.ToBitmap(newImage); } /// <summary> /// 饱和度 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void trackBarSaturation_Scroll(object sender, EventArgs e) { Mat src = new Mat(@"img\jzh.png", ImreadModes.Color); Cv2.Resize(src, src, new OpenCvSharp.Size(300, 300), 0, 0, InterpolationFlags.Linear); this.picSrc.Image = BitmapConverter.ToBitmap(src); lblSaturationNum.Text = trackBarSaturation.Value.ToString(); Mat hsv = new Mat(); Cv2.CvtColor(src, hsv, ColorConversionCodes.BGR2HSV); Mat[] channels = Cv2.Split(hsv); // 遍历所有像素 for (int i = 0; i < channels[1].Rows; i++) { for (int j = 0; j < channels[1].Cols; j++) { channels[1].Set(i, j, trackBarSaturation.Value); } } // 合并通道 Cv2.Merge(channels, hsv); Mat newImage = new Mat(); Cv2.CvtColor(hsv, newImage, ColorConversionCodes.HSV2BGR); this.picDest.Image = BitmapConverter.ToBitmap(newImage); } /// <summary> /// 亮度 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void trackBarLightness_Scroll(object sender, EventArgs e) { Mat src = new Mat(@"img\jzh.png", ImreadModes.Color); Cv2.Resize(src, src, new OpenCvSharp.Size(300, 300), 0, 0, InterpolationFlags.Linear); this.picSrc.Image = BitmapConverter.ToBitmap(src); lblLightnessNum.Text = trackBarLightness.Value.ToString(); Mat hsv = new Mat(); Cv2.CvtColor(src, hsv, ColorConversionCodes.BGR2HSV); Mat[] channels = Cv2.Split(hsv); // 遍历所有像素 for (int i = 0; i < channels[2].Rows; i++) { for (int j = 0; j < channels[2].Cols; j++) { channels[2].Set(i, j, trackBarLightness.Value); } } // 合并通道 Cv2.Merge(channels, hsv); Mat newImage = new Mat(); Cv2.CvtColor(hsv, newImage, ColorConversionCodes.HSV2BGR); this.picDest.Image = BitmapConverter.ToBitmap(newImage); } } }