c#中用鼠标点击事件实现抠图

功能:读取图片到picturebox中,用鼠标左键点击 选点,右键点击时,开始绘制多边形。

软件语言:opencvsharp,C#

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            // Create pen.
            Pen pen = new Pen(Color.Red, 3);

            // Create points that define polygon.
            //Point point1 = new Point(50, 50);
            //Point point2 = new Point(100, 25);
            //Point point3 = new Point(200, 5);
            //Point point4 = new Point(250, 50);
            //Point point5 = new Point(300, 100);
            //Point point6 = new Point(550, 200);
            //Point point7 = new Point(250, 250);
            //Point[] curvePoints =
            //         {
            //     point1,
            //     point2,
            //     point3,
            //     point4,
            //     point5,
            //     point6,
            //     point7
            // };
            //画顶点
            if (polyPoints != null)
            {
                foreach (System.Drawing.Point p in polyPoints)
                {
                    e.Graphics.FillEllipse(Brushes.Red, new Rectangle(p.X - 2, p.Y - 2, 4, 4));
                }
            }
            // Draw polygon to screen.
            //画多边形
            if (paint==true&& polyPoints != null && polyPoints.ToArray().Length>=3)
            {
              e.Graphics.DrawPolygon(pen, polyPoints.ToArray());
            }
            paint = false;
            //polyPoints = null;
            //points_cv = null;
        }
        List<System.Drawing.Point> polyPoints = null;
        List<OpenCvSharp.Point> points_cv = null;
        bool cliceMenu = true;
        bool paint = false;
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (cliceMenu)
            {
                if (e.Button == MouseButtons.Left)
                {
                    if (polyPoints == null) {
                        polyPoints = new List<System.Drawing.Point>();
                        points_cv = new List<OpenCvSharp.Point>();
                    }
                    polyPoints.Add(e.Location);
                    //OpenCvSharp.Point a = new OpenCvSharp.Point(e.Location.X,e.Location.Y);
                    int X0, Y0;
                    ConvertCoordinates(pictureBox1, out X0, out Y0, e.X, e.Y);
                    points_cv.Add(convertToCVPoint(X0,Y0));

                    //foreach (System.Drawing.Point p in polyPoints)
                    //{
                    //    g.FillEllipse(Brushes.Red, new Rectangle(p.X - 2, p.Y - 2, 4, 4));
                    //}
                    pictureBox1.Invalidate();
                }
                else if (e.Button == MouseButtons.Right && polyPoints != null)
                {
                    //Pen pen = new Pen(Color.Red, 3);
                    paint = true;
                    pictureBox1.Invalidate();
                    //using (var g = pictureBox1.CreateGraphics())
                    {
                        //g.SmoothingMode = SmoothingMode.AntiAlias;
                        //g.Clear(pictureBox1.BackColor);

                        //using (SolidBrush br = new SolidBrush(Color.FromArgb(100, Color.Yellow)))
                        //{
                        //    g.FillPolygon(br, polyPoints.ToArray());
                        //}

                        //if (polyPoints.ToArray().Length >= 3)
                        //{
                        //    g.DrawPolygon(Pens.Red, polyPoints.ToArray());

                        //    foreach (System.Drawing.Point p in polyPoints)
                        //    {
                        //        g.FillEllipse(Brushes.Red, new Rectangle(p.X - 2, p.Y - 2, 4, 4));
                        //    }
                        //}


                        Mat pic = new Mat(srcImg_0.Size(), MatType.CV_8UC1, Scalar.All(0));

                        //int[] a = polyPoints.ToArray();

            //            List<OpenCvSharp.Point> pts1 = new List<OpenCvSharp.Point>
            //{
            //    new OpenCvSharp.Point(400,400),
            //    new OpenCvSharp.Point(400,500),
            //    new OpenCvSharp.Point(500,500),
            //    new OpenCvSharp.Point(500,400),
            //    //new OpenCvSharp.Point(400,400)
            //};

            //            List<OpenCvSharp.Point> pts2 = new List<OpenCvSharp.Point>
            //            {
            //    new OpenCvSharp.Point(10,10),
            //    new OpenCvSharp.Point(10,150),
            //    new OpenCvSharp.Point(150,150),
            //    new OpenCvSharp.Point(150,10),
            //    //new OpenCvSharp.Point(10,10)
            //};
                        List<List<OpenCvSharp.Point>> pts = new List<List<OpenCvSharp.Point>>() {points_cv};
                        Cv2.Polylines(pic, pts, true, 255);
                        Cv2.FillPoly(pic, pts, 255);
                        Cv2.Resize(pic, pic, new OpenCvSharp.Size(),0.2,0.2);
                        //Cv2.ImShow("111", pic);
                        Mat pic1 = new Mat();
                        Cv2.Resize(srcImg_0, pic1, new OpenCvSharp.Size(), 0.2, 0.2);
                        Mat ROI = new Mat();
                        Cv2.CopyTo(pic1, ROI, pic);
                        Cv2.ImShow("111", ROI);
                  
                        polyPoints = null;
                        points_cv = null;
                    }
                }
            }

        }
       public static OpenCvSharp.Point convertToCVPoint(int X,int Y)
        {
            return new OpenCvSharp.Point(X, Y);
        }
        Mat srcImg_0=null;
        private void button2_Click(object sender, EventArgs e)
        {
            string imgName = "";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                imgName = openFileDialog1.FileName;
                srcImg_0 = new Mat(imgName, ImreadModes.Color);
                Mat srcImg = srcImg_0.Clone();
                Bitmap bitmap = BitmapConverter.ToBitmap(srcImg);
                pictureBox1.Image = bitmap;
                //防止异常图片
                //public_img.no_center_img=NoCenterImageGet();
            }
            else
            {
                MessageBox.Show("读取图片失败", "提示");
            }
        }
//坐标系转化
        private static void ConvertCoordinates(PictureBox pic, out int X0, out int Y0, int x, int y)
        {
            int pic_hgt = pic.ClientSize.Height;
            int pic_wid = pic.ClientSize.Width;
            X0 = x;
            Y0 = y;
            if (pic.Image == null) return;
            int img_hgt = pic.Image.Height;
            int img_wid = pic.Image.Width;


            switch (pic.SizeMode)
            {
                case PictureBoxSizeMode.AutoSize:
                case PictureBoxSizeMode.StretchImage:
                    X0 = (int)(img_wid * x / (float)pic_wid);
                    Y0 = (int)(img_hgt * y / (float)pic_hgt);
                    break;
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值