C# OpenCvSharp改变指定目标颜色

使用OpenCvSharp中的Cv2.ColorChange方法,改变指定目标的颜色

效果如下:

全部代码:

using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace Tools
{
    public partial class RectROI : Form
    {
        private Bitmap originalImage;
        private float currentScale = 1.0f;
        private RectangleF roi = RectangleF.Empty;
        private bool isDragging = false;
        private bool isResizing = false;
        private bool isDrawing = false;
        private PointF dragStartPosition;
        private RectangleF dragStartRoi;
        private ResizeHandle resizeHandle = ResizeHandle.None;
        private StatusStrip statusStrip1;
        private ToolStripStatusLabel toolStripStatusLabel1;
        private Button button2;
        private CheckBox checkBox1;
        private TrackBar trackBar1;
        private TrackBar trackBar2;
        private TrackBar trackBar3;
        private Label label1;
        private Label label2;
        private Label label3;
        private Label label4;
        private Label label5;
        private Label label6;
        private PictureBox pictureBox1;
        private Button button1;
        private PointF drawStartPoint;

        Mat src;
        Mat dst;
        int r = 100, g = 100, b = 100;
        Rect rect;

        public RectROI()
        {
            InitializeComponent();
            pictureBox.MouseWheel += PictureBox_MouseWheel;
            pictureBox.MouseDown += PictureBox_MouseDown;
            pictureBox.MouseMove += PictureBox_MouseMove;
            pictureBox.MouseUp += PictureBox_MouseUp;
            pictureBox.Paint += PictureBox_Paint;
            pictureBox.SizeMode = PictureBoxSizeMode.Normal;
            panel.AutoScroll = true;
            this.DoubleBuffered = true;
        }

        private void LoadImageButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                originalImage = new Bitmap(openFile.FileName);
                currentScale = 1.0f;
                pictureBox.Size = originalImage.Size;
                roi = RectangleF.Empty;
                pictureBox.Invalidate();
            }
        }

        private void PictureBox_MouseWheel(object sender, MouseEventArgs e)
        {
            if (originalImage == null) return;

            float scaleFactor = e.Delta > 0 ? 1.1f : 0.9f;
            currentScale *= scaleFactor;
            currentScale = Math.Max(0.1f, Math.Min(currentScale, 10f));

            int newWidth = (int)(originalImage.Width * currentScale);
            int newHeight = (int)(originalImage.Height * currentScale);
            pictureBox.Size = new System.Drawing.Size(newWidth, newHeight);
            pictureBox.Invalidate();
        }

        private void PictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (originalImage == null) return;

            float originalX = e.X / currentScale;
            float originalY = e.Y / currentScale;
            PointF mousePos = new PointF(originalX, originalY);

            if (roi.IsEmpty)
            {
                isDrawing = true;
                drawStartPoint = mousePos;
                roi = new RectangleF(drawStartPoint.X, drawStartPoint.Y, 0, 0);
            }
            else
            {
                resizeHandle = GetResizeHandle(mousePos);
                if (resizeHandle != ResizeHandle.None)
                {
                    isResizing = true;
                    dragStartPosition = mousePos;
                    dragStartRoi = roi;
                }
                else if (roi.Contains(mousePos))
                {
                    isDragging = true;
                    dragStartPosition = mousePos;
                    dragStartRoi = roi;
                }
            }

            if (isResizing || isDragging || isDrawing)
                pictureBox.Capture = true;
        }

        private ResizeHandle GetResizeHandle(PointF mousePos)
        {
            float handleSize = 8 / currentScale;
            RectangleF[] handles = new RectangleF[8];
            // 四个角控制点
            handles[0] = new RectangleF(roi.Left - handleSize / 2, roi.Top - handleSize / 2, handleSize, handleSize);
            handles[1] = new RectangleF(roi.Right - handleSize / 2, roi.Top - handleSize / 2, handleSize, handleSize);
            handles[2] = new RectangleF(roi.Left - handleSize / 2, roi.Bottom - handleSize / 2, handleSize, handleSize);
            handles[3] = new RectangleF(roi.Right - handleSize / 2, roi.Bottom - handleSize / 2, handleSize, handleSize);

            // 四个边中点控制点
            handles[4] = new RectangleF(roi.Left + roi.Width / 2 - handleSize / 2, roi.Top- handleSize / 2, handleSize, handleSize);//TOP
            handles[5] = new RectangleF(roi.Left + roi.Width / 2 - handleSize / 2, roi.Bottom - handleSize / 2, handleSize, handleSize);//BOTTOM
            handles[6] = new RectangleF(roi.Left - handleSize / 2, roi.Top+roi.Height/2- handleSize / 2, handleSize, handleSize);//LEFT
            handles[7] = new RectangleF(roi.Right - handleSize / 2, roi.Top + roi.Height / 2 - handleSize / 2, handleSize, handleSize);//RIGHT

            for (int i = 0; i < handles.Length; i++)
                if (handles[i].Contains(mousePos))
                    return (ResizeHandle)(i + 1);

            return ResizeHandle.None;
        }

        private void PictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (originalImage == null) return;

            float originalX = e.X / currentScale;
            float originalY = e.Y / currentScale;
            PointF currentMousePos = new PointF(originalX, originalY);

            if (isDrawing)
            {
                roi.X = Math.Min(drawStartPoint.X, currentMousePos.X);
                roi.Y = Math.Min(drawStartPoint.Y, currentMousePos.Y);
                roi.Width = Math.Abs(currentMousePos.X - drawStartPoint.X);
                roi.Height = Math.Abs(currentMousePos.Y - drawStartPoint.Y);
                pictureBox.Invalidate();
                GetROI(this.toolStripStatusLabel1);
            }
            else if (isResizing)
            {
                float deltaX = currentMousePos.X - dragStartPosition.X;
                float deltaY = currentMousePos.Y - dragStartPosition.Y;

                switch (resizeHandle)
                {
                    case ResizeHandle.TopLeft:
                        roi.X = Math.Max(0, dragStartRoi.X + deltaX);
                        roi.Y = Math.Max(0, dragStartRoi.Y + deltaY);
                        roi.Width = Math.Max(1, dragStartRoi.Width - deltaX);
                        roi.Height = Math.Max(1, dragStartRoi.Height - deltaY);
                        break;
                    case ResizeHandle.TopRight:
                        roi.Y = Math.Max(0, dragStartRoi.Y + deltaY);
                        roi.Width = Math.Max(1, dragStartRoi.Width + deltaX);
                        roi.Height = Math.Max(1, dragStartRoi.Height - deltaY);
                        break;
                    case ResizeHandle.BottomLeft:
                        roi.X = Math.Max(0, dragStartRoi.X + deltaX);
                        roi.Width = Math.Max(1, dragStartRoi.Width - deltaX);
                        roi.Height = Math.Max(1, currentMousePos.Y - roi.Y);
                        break;
                    case ResizeHandle.BottomRight:
                        roi.Width = Math.Max(1, currentMousePos.X - roi.X);
                        roi.Height = Math.Max(1, currentMousePos.Y - roi.Y);
                        break;
                    // 上边中点
                    case ResizeHandle.Top:
                        roi.Y = Math.Max(0, dragStartRoi.Y + deltaY);
                        roi.Height = Math.Max(1, dragStartRoi.Height - deltaY);
                        break;
                    // 下边中点
                    case ResizeHandle.Bottom:
                        roi.Height = Math.Max(1, currentMousePos.Y - roi.Y);
                        break;
                    // 左边中点
                    case ResizeHandle.Left:
                        roi.X = Math.Max(0, dragStartRoi.X + deltaX);
                        roi.Width = Math.Max(1, dragStartRoi.Width - deltaX);
                        break;
                    // 右边中点
                    case ResizeHandle.Right:
                        roi.Width = Math.Max(1, currentMousePos.X - roi.X);
                        break;
                }

                if (roi.Right > originalImage.Width)
                    roi.Width = originalImage.Width - roi.X;
                if (roi.Bottom > originalImage.Height)
                    roi.Height = originalImage.Height - roi.Y;

                pictureBox.Invalidate();
                GetROI(this.toolStripStatusLabel1);
            }
            else if (isDragging)
            {
                float deltaX = currentMousePos.X - dragStartPosition.X;
                float deltaY = currentMousePos.Y - dragStartPosition.Y;

                float newX = dragStartRoi.X + deltaX;
                float newY = dragStartRoi.Y + deltaY;

                newX = Math.Max(0, Math.Min(newX, originalImage.Width - roi.Width));
                newY = Math.Max(0, Math.Min(newY, originalImage.Height - roi.Height));

                roi.X = newX;
                roi.Y = newY;

                pictureBox.Invalidate();
                GetROI(this.toolStripStatusLabel1);
            }
        }

        private void PictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            isResizing = false;
            isDragging = false;
            isDrawing = false;
            pictureBox.Capture = false;

            if (roi.Width < 1 || roi.Height < 1)
                roi = RectangleF.Empty;
        }

        private void PictureBox_Paint(object sender, PaintEventArgs e)
        {
            if (originalImage == null) return;

            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.DrawImage(originalImage, 0, 0, pictureBox.Width, pictureBox.Height);

            if (!roi.IsEmpty)
            {
                float scaledX = roi.X * currentScale;
                float scaledY = roi.Y * currentScale;
                float scaledWidth = roi.Width * currentScale;
                float scaledHeight = roi.Height * currentScale;
                // 绘制半透明填充
                using (var brush = new SolidBrush(Color.FromArgb(30, 0, 255, 0)))
                {
                    e.Graphics.FillRectangle(brush, scaledX, scaledY, scaledWidth, scaledHeight);
                }
                using (Pen pen = new Pen(Color.GreenYellow, 2))
                {
                    e.Graphics.DrawRectangle(pen, scaledX, scaledY, scaledWidth, scaledHeight);
                }

                float handleSize = 8;
                DrawControlPoint(e.Graphics, scaledX, scaledY, handleSize);
                DrawControlPoint(e.Graphics, scaledX + scaledWidth, scaledY, handleSize);
                DrawControlPoint(e.Graphics, scaledX, scaledY + scaledHeight, handleSize);
                DrawControlPoint(e.Graphics, scaledX + scaledWidth, scaledY + scaledHeight, handleSize);
                // 添加中点控制点绘制
                DrawControlPoint(e.Graphics, scaledX + scaledWidth / 2, scaledY, handleSize);
                DrawControlPoint(e.Graphics, scaledX + scaledWidth / 2, scaledY + scaledHeight, handleSize);
                DrawControlPoint(e.Graphics, scaledX, scaledY + scaledHeight / 2, handleSize);
                DrawControlPoint(e.Graphics, scaledX + scaledWidth, scaledY + scaledHeight / 2, handleSize);
            }
        }

        private void DrawControlPoint(Graphics g, float x, float y, float size)
        {
                g.FillRectangle(Brushes.White, x - size / 2, y - size / 2, size, size);
                g.DrawRectangle(Pens.Black, x - size / 2, y - size / 2, size, size);
        }

        private void CaptureButton_Click(object sender, EventArgs e)
        {
            if (originalImage == null || roi.IsEmpty)
            {
                MessageBox.Show("请加载图片并绘制ROI区域。");
                return;
            }

            Rectangle roiRect = new Rectangle(
                (int)roi.X, (int)roi.Y,
                (int)roi.Width, (int)roi.Height);

            roiRect.X = Math.Max(0, roiRect.X);
            roiRect.Y = Math.Max(0, roiRect.Y);
            roiRect.Width = Math.Min(originalImage.Width - roiRect.X, roiRect.Width);
            roiRect.Height = Math.Min(originalImage.Height - roiRect.Y, roiRect.Height);

            if (roiRect.Width <= 0 || roiRect.Height <= 0)
            {
                MessageBox.Show("无效的ROI区域。");
                return;
            }


            try
            {
                Bitmap cropped = originalImage.Clone(roiRect, originalImage.PixelFormat);
                Clipboard.Clear();
                Clipboard.SetImage(cropped);
                if (!checkBox1.Checked)
                {
                    SaveFileDialog saveDialog = new SaveFileDialog();
                    saveDialog.Filter = "PNG图像|*.png";
                    if (saveDialog.ShowDialog() == DialogResult.OK)
                    {
                        cropped.Save(saveDialog.FileName, ImageFormat.Png);
                        MessageBox.Show("保存成功!");
                    }
                }
                else
                {
                    string filePath = "D:" + "\\" + "Pictures"+"\\";
                    if(!Directory.Exists(filePath))
                        Directory.CreateDirectory(filePath);
                    string fileName = filePath + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0') + "_" +
                        DateTime.Now.Hour.ToString().PadLeft(2, '0') + DateTime.Now.Minute.ToString().PadLeft(2, '0') + DateTime.Now.Second.ToString().PadLeft(2, '0') + ".png";
                    cropped.Save(fileName,ImageFormat.Png);
                    this.toolStripStatusLabel1.Text = "保存成功!"+fileName;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存失败: {ex.Message}");
            }


        }

        private enum ResizeHandle { None, TopLeft, TopRight, BottomLeft, BottomRight, Top, Bottom, Left, Right }
        private void GetROI(object control)
        {
            var statusLabel = control as ToolStripStatusLabel;
            statusLabel.Text = $"ROI:X={roi.X},Y={roi.Y},Width={roi.Width},Height={roi.Height}";
        }

        // Windows窗体设计器生成的代码
        private Panel panel;
        private PictureBox pictureBox;
        private Button loadImageButton;
        private Button captureButton;

        private void InitializeComponent()
        {
            this.panel = new System.Windows.Forms.Panel();
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
            this.pictureBox = new System.Windows.Forms.PictureBox();
            this.loadImageButton = new System.Windows.Forms.Button();
            this.captureButton = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.trackBar1 = new System.Windows.Forms.TrackBar();
            this.trackBar2 = new System.Windows.Forms.TrackBar();
            this.trackBar3 = new System.Windows.Forms.TrackBar();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.button1 = new System.Windows.Forms.Button();
            this.panel.SuspendLayout();
            this.statusStrip1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar3)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // panel
            // 
            this.panel.AutoScroll = true;
            this.panel.BackColor = System.Drawing.Color.Gainsboro;
            this.panel.Controls.Add(this.statusStrip1);
            this.panel.Controls.Add(this.pictureBox);
            this.panel.Location = new System.Drawing.Point(12, 41);
            this.panel.Name = "panel";
            this.panel.Size = new System.Drawing.Size(681, 581);
            this.panel.TabIndex = 0;
            // 
            // statusStrip1
            // 
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripStatusLabel1});
            this.statusStrip1.Location = new System.Drawing.Point(0, 559);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(681, 22);
            this.statusStrip1.TabIndex = 1;
            this.statusStrip1.Text = "statusStrip1";
            // 
            // toolStripStatusLabel1
            // 
            this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
            this.toolStripStatusLabel1.Size = new System.Drawing.Size(0, 17);
            // 
            // pictureBox
            // 
            this.pictureBox.Location = new System.Drawing.Point(0, 0);
            this.pictureBox.Name = "pictureBox";
            this.pictureBox.Size = new System.Drawing.Size(289, 200);
            this.pictureBox.TabIndex = 0;
            this.pictureBox.TabStop = false;
            // 
            // loadImageButton
            // 
            this.loadImageButton.Location = new System.Drawing.Point(12, 12);
            this.loadImageButton.Name = "loadImageButton";
            this.loadImageButton.Size = new System.Drawing.Size(75, 23);
            this.loadImageButton.TabIndex = 1;
            this.loadImageButton.Text = "加载图片";
            this.loadImageButton.UseVisualStyleBackColor = true;
            this.loadImageButton.Click += new System.EventHandler(this.LoadImageButton_Click);
            // 
            // captureButton
            // 
            this.captureButton.Location = new System.Drawing.Point(93, 12);
            this.captureButton.Name = "captureButton";
            this.captureButton.Size = new System.Drawing.Size(75, 23);
            this.captureButton.TabIndex = 2;
            this.captureButton.Text = "截图";
            this.captureButton.UseVisualStyleBackColor = true;
            this.captureButton.Click += new System.EventHandler(this.CaptureButton_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(252, 12);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 4;
            this.button2.Text = "清除ROI";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // checkBox1
            // 
            this.checkBox1.AutoSize = true;
            this.checkBox1.Checked = true;
            this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
            this.checkBox1.Location = new System.Drawing.Point(174, 16);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(72, 16);
            this.checkBox1.TabIndex = 8;
            this.checkBox1.Text = "自动命名";
            this.checkBox1.UseVisualStyleBackColor = true;
            // 
            // trackBar1
            // 
            this.trackBar1.Location = new System.Drawing.Point(729, 41);
            this.trackBar1.Maximum = 255;
            this.trackBar1.Name = "trackBar1";
            this.trackBar1.Size = new System.Drawing.Size(280, 45);
            this.trackBar1.TabIndex = 9;
            this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
            // 
            // trackBar2
            // 
            this.trackBar2.Location = new System.Drawing.Point(1052, 41);
            this.trackBar2.Maximum = 255;
            this.trackBar2.Name = "trackBar2";
            this.trackBar2.Size = new System.Drawing.Size(280, 45);
            this.trackBar2.TabIndex = 10;
            this.trackBar2.Scroll += new System.EventHandler(this.trackBar2_Scroll);
            // 
            // trackBar3
            // 
            this.trackBar3.Location = new System.Drawing.Point(729, 107);
            this.trackBar3.Maximum = 255;
            this.trackBar3.Name = "trackBar3";
            this.trackBar3.Size = new System.Drawing.Size(280, 45);
            this.trackBar3.TabIndex = 11;
            this.trackBar3.Scroll += new System.EventHandler(this.trackBar3_Scroll);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(701, 41);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(11, 12);
            this.label1.TabIndex = 12;
            this.label1.Text = "R";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(1024, 50);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(11, 12);
            this.label2.TabIndex = 13;
            this.label2.Text = "G";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(701, 116);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(11, 12);
            this.label3.TabIndex = 14;
            this.label3.Text = "B";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(853, 26);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(0, 12);
            this.label4.TabIndex = 15;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(1176, 26);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(0, 12);
            this.label5.TabIndex = 16;
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(853, 92);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(0, 12);
            this.label6.TabIndex = 17;
            // 
            // pictureBox1
            // 
            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pictureBox1.Location = new System.Drawing.Point(703, 142);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(629, 480);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            this.pictureBox1.TabIndex = 18;
            this.pictureBox1.TabStop = false;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(344, 12);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 19;
            this.button1.Text = "保存";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // RectROI
            // 
            this.ClientSize = new System.Drawing.Size(1422, 646);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.trackBar3);
            this.Controls.Add(this.trackBar2);
            this.Controls.Add(this.trackBar1);
            this.Controls.Add(this.checkBox1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.captureButton);
            this.Controls.Add(this.loadImageButton);
            this.Controls.Add(this.panel);
            this.Name = "RectROI";
            this.Text = "ColorChange";
            this.Load += new System.EventHandler(this.RectROI_Load);
            this.panel.ResumeLayout(false);
            this.panel.PerformLayout();
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar3)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        

        private void button2_Click(object sender, EventArgs e)
        {
            this.roi = RectangleF.Empty;
            pictureBox.Invalidate();
        }
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            label4.Text=trackBar1.Value.ToString();
            r = trackBar1.Value;
            colorChange();
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            label5.Text = trackBar2.Value.ToString();
            g = trackBar2.Value;
            colorChange();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                Cv2.ImWrite($"{DateTime.Now.ToString("yyyyMMddHHmmss")}.jpg", dst);
            else
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "jpg|*.jpg|png|*.png";
                if(saveFileDialog.ShowDialog()==DialogResult.OK)
                {
                    string fileName=saveFileDialog.FileName;
                    Cv2.ImWrite(fileName, dst);
                }
            }
            
        }

        private void RectROI_Load(object sender, EventArgs e)
        {
            trackBar1.Value=r; trackBar2.Value=g;trackBar3.Value=b;
        }

        private void trackBar3_Scroll(object sender, EventArgs e)
        {
            label6.Text = trackBar3.Value.ToString();
            b = trackBar3.Value;
            colorChange();
        }
        private void colorChange()
        {
            src=originalImage.ToMat();
            rect=new Rect(new OpenCvSharp.Point(roi.X,roi.Y),new OpenCvSharp.Size(roi.Size.Width,roi.Size.Height));
            dst = new Mat();
            Mat mask = Mat.Zeros(src.Size(), src.Type());
            Cv2.Rectangle(mask,rect,Scalar.All(255),-1);
            Cv2.ColorChange(src, mask, dst, r / 100, g / 100, b / 100);
            pictureBox1.Image=dst.ToBitmap();
            src.Dispose();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值