| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 | using OpenCvSharp;using PaintDotNet.Adjust.BaseImage;using PaintDotNet.ImageCollect;using System;using System.Drawing;using System.Windows.Forms;namespace PaintDotNet.Instrument{    /// <summary>    /// 像素跟踪    /// </summary>    internal class PixelTrackingDialog : FloatingToolForm    {        #region 控件        private Label label6;        private Label label5;        private Label label4;        private Label label3;        private Label label2;        private Label label1;        private CustomControl.LinearColorPickerControl panel1;        private PictureBox pictureBox1;        private Label label7;        private Label label8;        private NumericUpDown numericUpDown1;        private TrackBar trackBar1;        private Label label9;        #endregion        private AppWorkspace appWorkspace;        /// <summary>        /// 原图、hls(可优化)、鹰眼图(从经过缩放的图片获取)、经过缩放的图片        /// </summary>        private Mat mat, ImageROI, bmat = new Mat();        /// <summary>        /// rgb像素点        /// </summary>        private Vec3b bgr;        /// <summary>        /// hls像素点        /// </summary>        private double h, s, v;        /// <summary>        /// 缩放倍数        /// </summary>        private int scale;        /// <summary>        /// 中间十字和圆的颜色        /// 根据左侧的panel的被选中的背景色变化        /// </summary>        private Color color = Color.Red;        public Mat Mat        {            set            {                this.mat = value;            }        }        private void trackBar1_ValueChanged(object sender, EventArgs e)        {            if (mat != null)            {                scale = this.trackBar1.Value;                this.numericUpDown1.Value = this.trackBar1.Value;                //Cv2.Resize(mat, bmat, new OpenCvSharp.Size(mat.Width * scale, mat.Height * scale));                //Mat temp = new Mat(new OpenCvSharp.Size(bmat.Width + 100, bmat.Height + 100), bmat.Type());                //Cv2.CopyMakeBorder(bmat, temp, 50, 50, 50, 50, BorderTypes.Constant, Scalar.All(255));                //bmat = temp;            }        }        private void numericUpDown1_ValueChanged(object sender, EventArgs e)        {            if (mat != null)            {                scale = (int)this.numericUpDown1.Value;                this.trackBar1.Value = scale;                //Cv2.Resize(mat, bmat, new OpenCvSharp.Size(mat.Width * scale, mat.Height * scale));                //Mat temp = new Mat(new OpenCvSharp.Size(bmat.Width + 100, bmat.Height + 100), bmat.Type());                //Cv2.CopyMakeBorder(bmat, temp, 50, 50, 50, 50, BorderTypes.Constant, Scalar.All(255));                //bmat = temp;            }        }        /// <summary>        /// 构造函数        /// </summary>        /// <param name="appWorkspace"></param>        public PixelTrackingDialog(AppWorkspace appWorkspace)        {            this.appWorkspace = appWorkspace;            InitializeComponent();            this.label9.Text = PdnResources.GetString("Menu.Times.text");            this.Text = PdnResources.GetString("Menu.Tools.PixelTracking.Text");            this.label7.Parent = this.pictureBox1;            this.label8.Parent = this.pictureBox1;            this.label7.Location = new System.Drawing.Point(0, 0);            this.label8.Location = new System.Drawing.Point(0, 17);            this.pictureBox1.Paint += new PaintEventHandler(PictureBox1_Paint);        }        private void PixelTrackingDialog_FormClosing(object sender, FormClosingEventArgs e)        {            this.appWorkspace.toolBar.RefreshBtnSelect(false, "PixelTracking");            this.appWorkspace.toolsPanel.RefreshBtnSelect(false, "PixelTracking");        }        /// <summary>        /// PictureBox的绘制事件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void PictureBox1_Paint(object sender, PaintEventArgs e)        {            e.Graphics.DrawLine(new Pen(color), 0, 50, 100, 50);            e.Graphics.DrawLine(new Pen(color), 50, 0, 50, 100);            e.Graphics.DrawEllipse(new Pen(color), new Rectangle(25, 25, 50, 50));        }        ///// <summary>        ///// 左侧panel,渐变背景色        ///// </summary>        ///// <param name="sender"></param>        ///// <param name="e"></param>        //private void Panel1_Paint(object sender, PaintEventArgs e)        //{        //    LinearGradientBrush brush = new LinearGradientBrush(e.ClipRectangle, Color.Yellow, Color.Blue, LinearGradientMode.Vertical);        //    ColorBlend colorBlend = new ColorBlend();        //    colorBlend.Colors = new Color[]{ Color.Yellow, Color.Red, Color.Green, Color.Blue };        //    colorBlend.Positions = new float[] { 0 / 3f, 1 / 3f, 2 / 3f, 3 / 3f };        //    brush.InterpolationColors = colorBlend;        //    e.Graphics.FillRectangle(brush, e.ClipRectangle);        //}        private void Panel1_ValueChanged(object sender, IndexEventArgs ce)        {            color = panel1.ValueToColor(panel1.Value);            pictureBox1.Refresh();        }        private void InitializeComponent()        {            this.pictureBox1 = new System.Windows.Forms.PictureBox();            this.label6 = new System.Windows.Forms.Label();            this.label5 = new System.Windows.Forms.Label();            this.label4 = new System.Windows.Forms.Label();            this.label3 = new System.Windows.Forms.Label();            this.label2 = new System.Windows.Forms.Label();            this.label1 = new System.Windows.Forms.Label();            this.panel1 = new PaintDotNet.CustomControl.LinearColorPickerControl();            this.label7 = new System.Windows.Forms.Label();            this.label8 = new System.Windows.Forms.Label();            this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();            this.trackBar1 = new System.Windows.Forms.TrackBar();            this.label9 = new System.Windows.Forms.Label();            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();            this.SuspendLayout();            //             // pictureBox1            //             this.pictureBox1.Location = new System.Drawing.Point(21, 8);            this.pictureBox1.Name = "pictureBox1";            this.pictureBox1.Size = new System.Drawing.Size(100, 100);            this.pictureBox1.TabIndex = 1;            this.pictureBox1.TabStop = false;            //             // label6            //             this.label6.AutoSize = true;            this.label6.Location = new System.Drawing.Point(124, 95);            this.label6.Name = "label6";            this.label6.Size = new System.Drawing.Size(11, 12);            this.label6.TabIndex = 14;            this.label6.Text = "V";            //             // label5            //             this.label5.AutoSize = true;            this.label5.Location = new System.Drawing.Point(124, 79);            this.label5.Name = "label5";            this.label5.Size = new System.Drawing.Size(11, 12);            this.label5.TabIndex = 13;            this.label5.Text = "S";            //             // label4            //             this.label4.AutoSize = true;            this.label4.Location = new System.Drawing.Point(124, 63);            this.label4.Name = "label4";            this.label4.Size = new System.Drawing.Size(11, 12);            this.label4.TabIndex = 12;            this.label4.Text = "H";            //             // label3            //             this.label3.AutoSize = true;            this.label3.Location = new System.Drawing.Point(124, 41);            this.label3.Name = "label3";            this.label3.Size = new System.Drawing.Size(11, 12);            this.label3.TabIndex = 11;            this.label3.Text = "B";            //             // label2            //             this.label2.AutoSize = true;            this.label2.Location = new System.Drawing.Point(124, 25);            this.label2.Name = "label2";            this.label2.Size = new System.Drawing.Size(11, 12);            this.label2.TabIndex = 10;            this.label2.Text = "G";            //             // label1            //             this.label1.AutoSize = true;            this.label1.Location = new System.Drawing.Point(124, 9);            this.label1.Name = "label1";            this.label1.Size = new System.Drawing.Size(35, 12);            this.label1.TabIndex = 9;            this.label1.Text = "R=255";            //             // panel1            //             this.panel1.Count = 1;            this.panel1.CustomGradient = new System.Drawing.Color[] {        System.Drawing.Color.White,        System.Drawing.Color.Yellow,        System.Drawing.Color.Red,        System.Drawing.Color.Green,        System.Drawing.Color.Blue,        System.Drawing.Color.Black};            this.panel1.DrawFarNub = true;            this.panel1.DrawNearNub = true;            this.panel1.Location = new System.Drawing.Point(3, 8);            this.panel1.MaxColor = System.Drawing.Color.White;            this.panel1.MinColor = System.Drawing.Color.Black;            this.panel1.Name = "panel1";            this.panel1.Orientation = System.Windows.Forms.Orientation.Vertical;            this.panel1.Size = new System.Drawing.Size(14, 100);            this.panel1.TabIndex = 17;            this.panel1.TabStop = false;            this.panel1.Value = 153;            this.panel1.ValueChanged += new PaintDotNet.IndexEventHandler(this.Panel1_ValueChanged);            //             // label7            //             this.label7.AutoSize = true;            this.label7.BackColor = System.Drawing.Color.Transparent;            this.label7.Location = new System.Drawing.Point(24, 11);            this.label7.Name = "label7";            this.label7.Size = new System.Drawing.Size(17, 12);            this.label7.TabIndex = 18;            this.label7.Text = "X=";            //             // label8            //             this.label8.AutoSize = true;            this.label8.BackColor = System.Drawing.Color.Transparent;            this.label8.Location = new System.Drawing.Point(24, 27);            this.label8.Name = "label8";            this.label8.Size = new System.Drawing.Size(17, 12);            this.label8.TabIndex = 19;            this.label8.Text = "Y=";            //             // numericUpDown1            //             this.numericUpDown1.Location = new System.Drawing.Point(4, 114);            this.numericUpDown1.Maximum = new decimal(new int[] {            10,            0,            0,            0});            this.numericUpDown1.Minimum = new decimal(new int[] {            1,            0,            0,            0});            this.numericUpDown1.Name = "numericUpDown1";            this.numericUpDown1.Size = new System.Drawing.Size(37, 21);            this.numericUpDown1.TabIndex = 20;            this.numericUpDown1.Value = new decimal(new int[] {            1,            0,            0,            0});            this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged);            //             // trackBar1            //             this.trackBar1.Location = new System.Drawing.Point(68, 114);            this.trackBar1.Minimum = 1;            this.trackBar1.Name = "trackBar1";            this.trackBar1.Size = new System.Drawing.Size(91, 45);            this.trackBar1.TabIndex = 21;            this.trackBar1.TickStyle = System.Windows.Forms.TickStyle.None;            this.trackBar1.Value = 1;            this.trackBar1.ValueChanged += new System.EventHandler(this.trackBar1_ValueChanged);            //             // label9            //             this.label9.AutoSize = true;            this.label9.Location = new System.Drawing.Point(45, 118);            this.label9.Name = "label9";            this.label9.Size = new System.Drawing.Size(0, 12);            this.label9.TabIndex = 22;            //             // PixelTrackingDialog            //             this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);            this.ClientSize = new System.Drawing.Size(164, 141);            this.Controls.Add(this.label9);            this.Controls.Add(this.trackBar1);            this.Controls.Add(this.numericUpDown1);            this.Controls.Add(this.label8);            this.Controls.Add(this.label7);            this.Controls.Add(this.panel1);            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.pictureBox1);            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;            this.Name = "PixelTrackingDialog";            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.PixelTrackingDialog_FormClosing);            this.Controls.SetChildIndex(this.pictureBox1, 0);            this.Controls.SetChildIndex(this.label1, 0);            this.Controls.SetChildIndex(this.label2, 0);            this.Controls.SetChildIndex(this.label3, 0);            this.Controls.SetChildIndex(this.label4, 0);            this.Controls.SetChildIndex(this.label5, 0);            this.Controls.SetChildIndex(this.label6, 0);            this.Controls.SetChildIndex(this.panel1, 0);            this.Controls.SetChildIndex(this.label7, 0);            this.Controls.SetChildIndex(this.label8, 0);            this.Controls.SetChildIndex(this.numericUpDown1, 0);            this.Controls.SetChildIndex(this.trackBar1, 0);            this.Controls.SetChildIndex(this.label9, 0);            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();            this.ResumeLayout(false);            this.PerformLayout();        }        public bool IsPreview()        {            bool iss = CameraPreviewDialog.cameraPreviewDialog != null && CameraPreviewDialog.cameraPreviewDialog.IsLeave() && CameraPreviewDialog.cameraPreviewDialog.documentWorkspace.CompositionSurface != null ;            return iss;        }                /// <summary>        /// 设置像素跟踪的数据        /// </summary>        public void SetImageAndData(System.Drawing.Point point)        {            if (IsPreview() || this.appWorkspace.ActiveDocumentWorkspace != null)            {                scale = int.Parse(this.numericUpDown1.Value.ToString());                if (IsPreview())                    mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(CameraPreviewDialog.cameraPreviewDialog.documentWorkspace.CompositionSurface.CreateAliasedBitmap());                else                    mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(this.appWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreateAliasedBitmap());                Cv2.Resize(mat, bmat, new OpenCvSharp.Size(mat.Width, mat.Height));                Mat temp = new Mat(new OpenCvSharp.Size(bmat.Width + 100, bmat.Height + 100), bmat.Type());                Cv2.CopyMakeBorder(bmat, temp, 50, 50, 50, 50, BorderTypes.Constant, Scalar.All(255));                bmat = temp;                if (point.Y < 0) point.Y = 0;                if (point.X < 0) point.X = 0;                this.label7.Text = "X=" + point.X.ToString();                this.label8.Text = "Y=" + point.Y.ToString();                if (point.X < mat.Width && point.Y < mat.Height)                {                    bgr = mat.At<Vec3b>(point.Y, point.X);                    this.label1.Text = "R=" + bgr[2];                    this.label2.Text = "G=" + bgr[1];                    this.label3.Text = "B=" + bgr[0];                    BaseTools.RgbToHsv(bgr, out h, out s, out v);                    this.label4.Text = "H=" + (int)(h / 2);                    this.label5.Text = "S=" + (int)(s * 255);                    this.label6.Text = "V=" + (int)(v * 255);                    //设置源图像ROI                    Rect roi = new Rect(point.X, point.Y, 100, 100);//首先要用个rect确定我们的兴趣区域在哪                    ImageROI = new Mat(bmat, roi);                    //按缩放比例截取小图像                    Rect roi1 = new Rect((100 - 100 / scale) / 2, (100 - 100 / scale) / 2, 100 / scale, 100 / scale);                    ImageROI = new Mat(ImageROI, roi1);                    //按固定宽高放大图像                    Cv2.Resize(ImageROI, ImageROI, new OpenCvSharp.Size(100, 100));                    this.pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(ImageROI);                }            }            GC.Collect();        }    }}
 |