c#中用鼠标点击事件实现抠图
生活随笔
收集整理的這篇文章主要介紹了
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;}}總結
以上是生活随笔為你收集整理的c#中用鼠标点击事件实现抠图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c# 通过鼠标点击绘制多边形
- 下一篇: “System.AccessViolat