| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117 |
- using OpenCvSharp;
- using PaintDotNet.Adjust;
- using PaintDotNet.Adjust.BaseImage;
- using PaintDotNet.Base.Functionodel;
- using PaintDotNet.CustomControl;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Point = System.Drawing.Point;
- namespace PaintDotNet.DedicatedAnalysis.NonferrousMetals.YYT098814_2016
- {
- internal class GrainContoursEditingDialog : Form
- {
- /// <summary>
- /// 公共按钮
- /// </summary>
- private CommonControlButtons commonControlButtons;
- /// <summary>
- /// 主控件
- /// </summary>
- private AppWorkspace appWorkspace;
- private DocumentWorkspaceWindow documentWorkspace;
- /// <summary>
- /// 外接矩形,或者是计算的矩形
- /// </summary>
- protected RectangleF rectangle = new RectangleF();
- /// <summary>
- /// 存在视场标记
- /// </summary>
- private bool existViewFlag = false;
- /// <summary>
- /// 选中图片的bitmap
- /// </summary>
- private Bitmap bitmap;
- /// <summary>
- /// 当前选择的图片
- /// </summary>
- private Mat mat;
- /// <summary>
- /// 用于get处理后的图像
- /// </summary>
- private Mat phaseMat;
- Color phaseColor;
- /// <summary>
- /// 操作类型
- /// 0,没有操作 1,折线添加 2,直线添加
- /// 11,多边形删除 12,圆形删除 13,矩形删除 14,椭圆删除
- /// </summary>
- private int pointKtype = 0;
- /// <summary>
- /// 起始点
- /// </summary>
- private Point startPoint = new Point(-1, -1);
- /// <summary>
- /// 结束点
- /// </summary>
- private Point endPoint = new Point(-1, -1);
- /// <summary>
- /// 检测到的轮廓
- /// </summary>
- OpenCvSharp.Point[][] contours;
- int contoursTop = 0;
- int contoursLeft = 0;
- int contoursWidth = 1;
- /// <summary>
- /// 折线点的集合
- /// </summary>
- public List<Point> pointArray = new List<Point>();
- //1013###19326 晶粒度的轮廓编辑页面,需要把辅助线带过去(客户编辑只处理辅助线位置的轮廓)
- /// <summary>
- /// 辅助线集成
- /// </summary>
- private GrainSizeStandard.IntegrationClass.GrainSizeGuideClass guideClass;
- public object SelectedItem;
- private GroupBox groupBox1;
- private GroupBox groupBox2;
- private GroupBox groupBox3;
- private Button button2;
- private Button button1;
- private Button button3;
- private Button button4;
- private Button button8;
- private Button button7;
- private Button button6;
- private Button button5;
- private Button button9;
- private GroupBox groupBox7;
- public GrainSizeStandard.IntegrationClass.GrainSizeGuideClass GuideClass
- {
- set
- {
- this.guideClass = value;
- }
- }
- public GrainContoursEditingDialog(AppWorkspace appWorkspace, int focusedItemIndex, Color phaseColor, Mat phaseMat, OpenCvSharp.Point[][] contours, int contoursTop, int contoursLeft, int contoursWidth)
- {
- this.appWorkspace = appWorkspace;
- InitializeComponent();
- InitializeLanguageText();
- //
- //初始化图像控件
- //
- this.documentWorkspace = new DocumentWorkspaceWindow(appWorkspace);
- this.documentWorkspace.Dock = DockStyle.Fill;
- this.documentWorkspace.HookMouseEvents();
- this.documentWorkspace.AuxiliaryLineEnabled = false;
- this.documentWorkspace.Visible = false;
- this.documentWorkspace.panel.MouseDown += OnMouseDown;
- this.documentWorkspace.panel.Paint += Panel_Paint;
- this.documentWorkspace.panel.MouseMove += onMouseMove;
- this.documentWorkspace.panel.MouseUp += onMouseUp;
- this.documentWorkspace.panel.Click += new EventHandler(this.pictureBox1_Click);
- this.groupBox7.Controls.Add(documentWorkspace);
- //
- //初始化操作按钮
- //
- this.commonControlButtons = new CommonControlButtons();
- this.commonControlButtons.Dock = DockStyle.Top;
- this.commonControlButtons.Height = 30;
- this.commonControlButtons.HideZoomToWindowAndActualSize();
- this.groupBox7.Controls.Add(commonControlButtons);
- this.phaseColor = phaseColor;
- {
- //
- //初始化相
- //
- PhaseModel model = new PhaseModel();
- model.choise = true;
- model.mat = null;
- model.color = phaseColor.ToArgb();// Color.Green/*panel2.BackColor*/.ToArgb();
- model.position = this.documentWorkspace.PhaseModels.Count + 1;
- model.name = PdnResources.GetString("Menu.Outlinetoreproduce.Text");
- this.documentWorkspace.PhaseModels.Add(model);
- }
- this.contours = contours;
- this.contoursTop = contoursTop;
- this.contoursLeft = contoursLeft;
- this.contoursWidth = contoursWidth;
- this.documentWorkspace.PhaseModels[0].mat = phaseMat;
- this.documentWorkspace.Refresh();
- existViewFlag = this.appWorkspace.DocumentWorkspaces[focusedItemIndex].GraphicsList.IsExsitView();
- this.bitmap = this.appWorkspace.DocumentWorkspaces[focusedItemIndex].CompositionSurface.CreateAliasedBitmap();
- if (!existViewFlag)
- {
- mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);
- }
- else
- {
- mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(this.appWorkspace.DocumentWorkspaces[focusedItemIndex].GetFullSizeWithRegion());
- }
- Document document = Document.FromImage(bitmap);
- this.documentWorkspace.Document = document;
- this.documentWorkspace.Visible = true;
- this.documentWorkspace.GraphicsList = this.appWorkspace.DocumentWorkspaces[focusedItemIndex].GraphicsList;
- InitCommonButtonEvent();
- }
- #region 矩形拖动
- /// <summary>
- /// 鼠标按下
- /// </summary>
- /// <param name="drawArea"></param>
- /// <param name="e"></param>
- private void OnMouseDown(object sender, MouseEventArgs e)
- {
- // 换算后的点
- PointF point1 = documentWorkspace.GetScalePoint(e.Location);
- if (point1.X <= this.documentWorkspace.CompositionSurface.Width
- && point1.X >= 0
- && point1.Y >= 0
- && point1.Y <= this.documentWorkspace.CompositionSurface.Height)
- {
- if (e.Button == MouseButtons.Left && this.pointKtype >= 1
- && startPoint.X == -1 && startPoint.Y == -1)
- startPoint = new Point((int)point1.X, (int)point1.Y);
- }
- }
- /// <summary>
- /// 移动
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void onMouseMove(object sender, MouseEventArgs e)
- {
- // 换算后的点
- PointF point1 = documentWorkspace.GetScalePoint(e.Location);
- if (e.Button == MouseButtons.Left)
- {
- // 椭圆删除/圆形删除
- if ((this.pointKtype == 14 || this.pointKtype == 12) && this.startPoint.X > -1 && this.startPoint.Y > -1)
- {
- endPoint = new Point((int)point1.X, (int)point1.Y);
- if (this.startPoint.X < this.endPoint.X)
- {
- this.rectangle.X = this.startPoint.X;
- this.rectangle.Width = this.endPoint.X - this.startPoint.X;
- }
- else
- {
- this.rectangle.X = this.endPoint.X;
- this.rectangle.Width = this.startPoint.X - this.endPoint.X;
- }
- if (this.startPoint.Y < this.endPoint.Y)
- {
- this.rectangle.Y = this.startPoint.Y;
- this.rectangle.Height = this.endPoint.Y - this.startPoint.Y;
- }
- else
- {
- this.rectangle.Y = this.endPoint.Y;
- this.rectangle.Height = this.startPoint.Y - this.endPoint.Y;
- }
- this.documentWorkspace.Refresh();
- }
- // 矩形删除
- if (this.pointKtype == 13 && this.startPoint.X > -1 && this.startPoint.Y > -1)
- {
- endPoint = new Point((int)point1.X, (int)point1.Y);
- if (this.startPoint.X < this.endPoint.X)
- {
- this.rectangle.X = this.startPoint.X;
- this.rectangle.Width = this.endPoint.X - this.startPoint.X;
- }
- else
- {
- this.rectangle.X = this.endPoint.X;
- this.rectangle.Width = this.startPoint.X - this.endPoint.X;
- }
- if (this.startPoint.Y < this.endPoint.Y)
- {
- this.rectangle.Y = this.startPoint.Y;
- this.rectangle.Height = this.endPoint.Y - this.startPoint.Y;
- }
- else
- {
- this.rectangle.Y = this.endPoint.Y;
- this.rectangle.Height = this.startPoint.Y - this.endPoint.Y;
- }
- this.documentWorkspace.Refresh();
- }
- // 直线添加
- if (this.pointKtype == 2 && this.startPoint.X > -1 && this.startPoint.Y > -1)
- {
- endPoint = new Point((int)point1.X, (int)point1.Y);
- this.documentWorkspace.Refresh();
- }
- }
- }
- /// <summary>
- /// 鼠标抬起
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void onMouseUp(object sender, MouseEventArgs e)
- {
- // 换算后的点
- PointF point1 = documentWorkspace.GetScalePoint(e.Location);
- if (point1.X <= this.documentWorkspace.CompositionSurface.Width
- && point1.X >= 0
- && point1.Y >= 0
- && point1.Y <= this.documentWorkspace.CompositionSurface.Height) { }
- else
- return;
- if (e.Button == MouseButtons.Right)
- {
- // 折线删除
- if (this.pointKtype == 11 && pointArray.Count > 1)
- {
- pointArray.Add(new Point((int)point1.X, (int)point1.Y));
- endPoint = new Point((int)point1.X, (int)point1.Y);
- List<PointF> pointFArray = new List<PointF>();
- foreach (var item in pointArray)
- {
- pointFArray.Add(new PointF(item.X, item.Y));
- }
- this.documentWorkspace.PhaseModels[0].mat = PreActionIntent.PolygonDelete(this.documentWorkspace.PhaseModels[0].mat, pointFArray);
- ////保存处理后的图片
- //Bitmap originalBit = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(this.documentWorkspace.PhaseModels[0].mat);
- //Bitmap newBit = originalBit.Clone(new Rectangle(0, 0, originalBit.Width, originalBit.Height), originalBit.PixelFormat);
- //Graphics graphics = Graphics.FromImage(newBit);
- //Draw(graphics);
- //this.documentWorkspace.PhaseModels[0].mat/* = phaseMat*/ = OpenCvSharp.Extensions.BitmapConverter.ToMat(newBit);
- this.rectangle.X = -1;
- this.rectangle.Y = -1;
- this.rectangle.Width = 0;
- this.rectangle.Height = 0;
- pointArray.Clear();
- startPoint = new Point(-1, -1);
- endPoint = new Point(-1, -1);
- this.pointKtype = 0;
- this.documentWorkspace.Refresh();
- }
- // 折线添加
- if (this.pointKtype == 1 && pointArray.Count > 1)
- {
- pointArray.Add(new Point((int)point1.X, (int)point1.Y));
- endPoint = new Point((int)point1.X, (int)point1.Y);
- //保存处理后的图片
- Bitmap originalBit = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(this.documentWorkspace.PhaseModels[0].mat);
- Bitmap newBit = originalBit.Clone(new Rectangle(0, 0, originalBit.Width, originalBit.Height), originalBit.PixelFormat);
- Graphics graphics = Graphics.FromImage(newBit);
- Draw(graphics);
- this.documentWorkspace.PhaseModels[0].mat/* = phaseMat*/ = OpenCvSharp.Extensions.BitmapConverter.ToMat(newBit);
- pointArray.Clear();
- startPoint = new Point(-1, -1);
- endPoint = new Point(-1, -1);
- this.pointKtype = 0;
- this.documentWorkspace.Refresh();
- }
- }
- if (e.Button == MouseButtons.Left)
- {
- // 圆形删除/椭圆删除
- if ((this.pointKtype == 14 || this.pointKtype == 12) && this.startPoint.X > -1 && this.startPoint.Y > -1)
- {
- endPoint = new Point((int)point1.X, (int)point1.Y);
- if (this.startPoint.X < this.endPoint.X)
- {
- this.rectangle.X = this.startPoint.X;
- this.rectangle.Width = this.endPoint.X - this.startPoint.X;
- }
- else
- {
- this.rectangle.X = this.endPoint.X;
- this.rectangle.Width = this.startPoint.X - this.endPoint.X;
- }
- if (this.startPoint.Y < this.endPoint.Y)
- {
- this.rectangle.Y = this.startPoint.Y;
- this.rectangle.Height = this.endPoint.Y - this.startPoint.Y;
- }
- else
- {
- this.rectangle.Y = this.endPoint.Y;
- this.rectangle.Height = this.startPoint.Y - this.endPoint.Y;
- }
- if (this.pointKtype == 14)
- {
- //循环处理轮廓的点
- if (contours != null && contours.Length > 0)
- {
- for (int i = 0; i < contours.Count(); i++)
- {
- OpenCvSharp.Point[] tempLine = contours[i];
- if (tempLine != null && tempLine.Length > 1)
- {
- List<OpenCvSharp.Point> pointsEdit = tempLine.ToList();
- for (int y = tempLine.Length - 1; y >= 0; y--)
- {
- if (BaseTools.isPointInOval(new System.Drawing.Point(tempLine[y].X + contoursLeft, tempLine[y].Y + contoursTop),
- new System.Drawing.PointF(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2),
- rectangle.Width / 2,
- rectangle.Height / 2
- ))
- {
- pointsEdit.RemoveAt(y);
- }
- }
- contours[i] = pointsEdit.ToArray();
- }
- }
- }
- this.documentWorkspace.PhaseModels[0].mat = PreActionIntent.OvalDelete(this.documentWorkspace.PhaseModels[0].mat, this.rectangle);
- }
- else
- {
- //循环处理轮廓的点
- if (contours != null && contours.Length > 0)
- {
- for (int i = 0; i < contours.Count(); i++)
- {
- OpenCvSharp.Point[] tempLine = contours[i];
- if (tempLine != null && tempLine.Length > 1)
- {
- List<OpenCvSharp.Point> pointsEdit = tempLine.ToList();
- for (int y = tempLine.Length - 1; y >= 0; y--)
- {
- RectangleF rectangleF2 = new RectangleF(this.rectangle.X, this.rectangle.Y, Math.Min(this.rectangle.Width, this.rectangle.Height), Math.Min(this.rectangle.Width, this.rectangle.Height));
- if (BaseTools.isPointInOval(new System.Drawing.Point(tempLine[y].X + contoursLeft, tempLine[y].Y + contoursTop),
- new System.Drawing.PointF(rectangleF2.X + rectangleF2.Width / 2, rectangleF2.Y + rectangleF2.Height / 2),
- rectangleF2.Width / 2,
- rectangleF2.Height / 2
- ))
- {
- pointsEdit.RemoveAt(y);
- }
- }
- contours[i] = pointsEdit.ToArray();
- }
- }
- }
- this.documentWorkspace.PhaseModels[0].mat = PreActionIntent.OvalDelete(this.documentWorkspace.PhaseModels[0].mat
- , new RectangleF(this.rectangle.X, this.rectangle.Y, Math.Min(this.rectangle.Width, this.rectangle.Height), Math.Min(this.rectangle.Width, this.rectangle.Height)));
- }
- this.rectangle.X = -1;
- this.rectangle.Y = -1;
- this.rectangle.Width = 0;
- this.rectangle.Height = 0;
- startPoint = new Point(-1, -1);
- endPoint = new Point(-1, -1);
- this.pointKtype = 0;
- this.documentWorkspace.Refresh();
- }
- // 矩形删除
- if (this.pointKtype == 13 && this.startPoint.X > -1 && this.startPoint.Y > -1)
- {
- endPoint = new Point((int)point1.X, (int)point1.Y);
- if (this.startPoint.X < this.endPoint.X)
- {
- this.rectangle.X = this.startPoint.X;
- this.rectangle.Width = this.endPoint.X - this.startPoint.X;
- }
- else
- {
- this.rectangle.X = this.endPoint.X;
- this.rectangle.Width = this.startPoint.X - this.endPoint.X;
- }
- if (this.startPoint.Y < this.endPoint.Y)
- {
- this.rectangle.Y = this.startPoint.Y;
- this.rectangle.Height = this.endPoint.Y - this.startPoint.Y;
- }
- else
- {
- this.rectangle.Y = this.endPoint.Y;
- this.rectangle.Height = this.startPoint.Y - this.endPoint.Y;
- }
- this.documentWorkspace.PhaseModels[0].mat = PreActionIntent.RectangleDelete(this.documentWorkspace.PhaseModels[0].mat, this.rectangle);
- //循环处理轮廓的点
- if (contours != null && contours.Length > 0)
- {
- for (int i = 0; i < contours.Count(); i++)
- {
- OpenCvSharp.Point[] tempLine = contours[i];
- if (tempLine != null && tempLine.Length > 1)
- {
- List<OpenCvSharp.Point> pointsEdit = tempLine.ToList();
- for (int y = tempLine.Length - 1; y >= 0; y--)
- {
- if (rectangle.Contains(tempLine[y].X + contoursLeft, tempLine[y].Y + contoursTop))
- {
- pointsEdit.RemoveAt(y);
- }
- }
- contours[i] = pointsEdit.ToArray();
- }
- }
- }
- //p.mat = PreActionIntent.RectangleDelete(p.mat, rect);
- ////保存处理后的图片
- //Bitmap originalBit = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(this.documentWorkspace.PhaseModels[0].mat);
- //Bitmap newBit = originalBit.Clone(new Rectangle(0, 0, originalBit.Width, originalBit.Height), originalBit.PixelFormat);
- //Graphics graphics = Graphics.FromImage(newBit);
- //Draw(graphics);
- //this.documentWorkspace.PhaseModels[0].mat/* = phaseMat*/ = OpenCvSharp.Extensions.BitmapConverter.ToMat(newBit);
- this.rectangle.X = -1;
- this.rectangle.Y = -1;
- this.rectangle.Width = 0;
- this.rectangle.Height = 0;
- startPoint = new Point(-1, -1);
- endPoint = new Point(-1, -1);
- this.pointKtype = 0;
- this.documentWorkspace.Refresh();
- }
- // 折线添加/折线删除
- if (this.pointKtype == 1 || this.pointKtype == 11)
- {
- pointArray.Add(new Point((int)point1.X, (int)point1.Y));
- if (this.startPoint.X > -1 && this.startPoint.Y > -1)
- {
- }
- else
- startPoint = new Point((int)point1.X, (int)point1.Y);
- //// 折线删除
- //if (this.pointKtype == 11 && pointArray.Count >= 2)
- //{
- // Pen pen = new Pen(this.phaseColor, 1);
- // pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
- // g.DrawPolygon(pen, pointArray.ToArray());
- // pen.Dispose();
- //}
- //// 折线添加
- //if (this.pointKtype == 1 && pointArray.Count >= 2)
- //{
- // Pen pen = new Pen(this.phaseColor, 1);
- // g.DrawLines(pen, pointArray.ToArray());
- // pen.Dispose();
- //}
- //循环处理轮廓的点
- if (this.pointKtype == 11 && contours != null && contours.Length > 0 && pointArray.Count >= 2)
- {
- List<OpenCvSharp.Point> temp111Src = new List<OpenCvSharp.Point>();
- foreach (Point itemP in pointArray)
- {
- temp111Src.Add(new OpenCvSharp.Point(itemP.X, itemP.Y));
- }
- OpenCvSharp.Point[] temp11Line = temp111Src.ToArray();
- //循环处理轮廓的点
- for (int i = 0; i < contours.Count(); i++)
- {
- OpenCvSharp.Point[] tempLine = contours[i];
- if (tempLine != null && tempLine.Length > 1)
- {
- List<OpenCvSharp.Point> pointsEdit = tempLine.ToList();
- for (int y = tempLine.Length - 1; y >= 0; y--)
- {
- if (Cv2.PointPolygonTest(temp11Line, new Point2f(tempLine[y].X + contoursLeft, tempLine[y].Y + contoursTop), false) >= 0)
- {
- pointsEdit.RemoveAt(y);
- }
- }
- contours[i] = pointsEdit.ToArray();
- }
- }
- }
- //循环处理轮廓的点
- if (this.pointKtype == 1 && contours != null && contours.Length > 0 && pointArray.Count >= 2)
- {
- List<OpenCvSharp.Point[]> listContours = contours.ToList();
- List<OpenCvSharp.Point> pointsEdit = new List<OpenCvSharp.Point>();
- foreach (Point itemP in pointArray)
- {
- pointsEdit.Add(new OpenCvSharp.Point(itemP.X, itemP.Y));
- }
- listContours.Add(pointsEdit.ToArray());
- contours = listContours.ToArray();
- }
- this.documentWorkspace.Refresh();
- }
- // 直线添加
- if (this.pointKtype == 2 && this.startPoint.X > -1 && this.startPoint.Y > -1)
- {
- endPoint = new Point((int)point1.X, (int)point1.Y);
- //保存处理后的图片
- Bitmap originalBit = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(this.documentWorkspace.PhaseModels[0].mat);
- Bitmap newBit = originalBit.Clone(new Rectangle(0, 0, originalBit.Width, originalBit.Height), originalBit.PixelFormat);
- Graphics graphics = Graphics.FromImage(newBit);
- Draw(graphics);
- this.documentWorkspace.PhaseModels[0].mat/* = phaseMat*/ = OpenCvSharp.Extensions.BitmapConverter.ToMat(newBit);
- //// 折线删除
- //if (this.pointKtype == 11 && pointArray.Count >= 2)
- //{
- // Pen pen = new Pen(this.phaseColor, 1);
- // pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
- // g.DrawPolygon(pen, pointArray.ToArray());
- // pen.Dispose();
- //}
- //// 折线添加
- //if (this.pointKtype == 1 && pointArray.Count >= 2)
- //{
- // Pen pen = new Pen(this.phaseColor, 1);
- // g.DrawLines(pen, pointArray.ToArray());
- // pen.Dispose();
- //}
- //// 直线添加
- //if (this.pointKtype == 2 && this.startPoint.X > -1 && this.startPoint.Y > -1 )
- //{
- // Pen pen = new Pen(this.phaseColor, 1);
- // //pen.DashStyle = DashStyle.Dash;
- // g.DrawLine(pen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
- // pen.Dispose();
- //}
- //循环处理轮廓的点
- if (contours != null && contours.Length > 0 && this.endPoint.X > -1 && this.endPoint.Y > -1)
- {
- List<OpenCvSharp.Point[]> listContours = contours.ToList();
- List<OpenCvSharp.Point> pointsEdit = new List<OpenCvSharp.Point>();
- pointsEdit.Add(new OpenCvSharp.Point(this.startPoint.X, this.startPoint.Y));
- pointsEdit.Add(new OpenCvSharp.Point(this.endPoint.X, this.endPoint.Y));
- listContours.Add(pointsEdit.ToArray());
- contours = listContours.ToArray();
- }
- startPoint = new Point(-1, -1);
- endPoint = new Point(-1, -1);
- this.pointKtype = 0;
- this.documentWorkspace.Refresh();
- }
- }
- }
- #endregion
- /// <summary>
- /// surfaceBox双击事件
- /// 目前是参考AxioVision
- /// 应该是把每次选的点都取最高和最低的BGR
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private unsafe void pictureBox1_Click(object sender, EventArgs e)
- {
- }
- /// <summary>
- /// 画布绘制
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Panel_Paint(object sender, PaintEventArgs e)
- {
- if (this.documentWorkspace.CompositionSurface != null)
- {
- //
- // 以下是计算绘制图片的位置和大小并绘制图片
- //
- System.Drawing.Rectangle rc = this.documentWorkspace.panel.ClientRectangle;
- int width = (int)(this.documentWorkspace.CompositionSurface.Width * this.documentWorkspace.ScaleFactor.Ratio);
- int height = (int)(this.documentWorkspace.CompositionSurface.Height * this.documentWorkspace.ScaleFactor.Ratio);
- int x = (rc.Width < width) ? this.documentWorkspace.panel.AutoScrollPosition.X : (rc.Width - width) / 2;
- int y = (rc.Height < height) ? this.documentWorkspace.panel.AutoScrollPosition.Y : (rc.Height - height) / 2;
- //
- // 以下是绘制网格、标注、测量、视场等开始
- //
- e.Graphics.TranslateTransform(x, y);
- e.Graphics.ScaleTransform((float)this.documentWorkspace.ScaleFactor.Ratio, (float)this.documentWorkspace.ScaleFactor.Ratio);
- //if (this.checkBox4_0.Checked)//显示网格
- Draw(e.Graphics);
- e.Graphics.ScaleTransform(1 / (float)this.documentWorkspace.ScaleFactor.Ratio, 1 / (float)this.documentWorkspace.ScaleFactor.Ratio);
- e.Graphics.TranslateTransform(-x, -y);
- }
- }
- /// <summary>
- /// 绘制
- /// </summary>
- private void Draw(Graphics g/*graphics*/)
- {
- // 抗锯齿
- g.SmoothingMode = SmoothingMode.AntiAlias;
- if (guideClass != null && guideClass.GrainSizeGuideAreaMethodModel != null)
- guideClass.DrawAreaGuide(g, SelectedItem, guideClass.guideModel);
- else if (guideClass != null && guideClass.GrainSizeGuideStyleModel != null)
- guideClass.DrawGuideLines(g, SelectedItem, guideClass.guideModel);
- //圆形删除/椭圆删除
- if ((this.pointKtype == 14 || this.pointKtype == 12) && (this.rectangle.Width > 0 || this.rectangle.Height > 0))
- {
- Pen pen = new Pen(this.phaseColor, 1);
- pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
- if (this.pointKtype == 14)
- g.DrawEllipse(pen, this.rectangle);
- else
- g.DrawEllipse(pen, new RectangleF(this.rectangle.X, this.rectangle.Y, Math.Min(this.rectangle.Width, this.rectangle.Height), Math.Min(this.rectangle.Width, this.rectangle.Height)));
- pen.Dispose();
- }
- //矩形删除
- if (this.pointKtype == 13 && (this.rectangle.Width > 0 || this.rectangle.Height > 0))
- {
- Pen pen = new Pen(this.phaseColor, 1);
- pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
- g.DrawRectangle(pen, new Rectangle((int)this.rectangle.X, (int)this.rectangle.Y, (int)this.rectangle.Width, (int)this.rectangle.Height));
- pen.Dispose();
- }
- // 折线删除
- if (this.pointKtype == 11 && pointArray.Count >= 2)
- {
- Pen pen = new Pen(this.phaseColor, 1);
- pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
- g.DrawPolygon(pen, pointArray.ToArray());
- pen.Dispose();
- }
- //float penwidth = contoursWidth * this.documentWorkspace.panel.Width
- // 直线添加
- if (this.pointKtype == 2 && this.startPoint.X > -1 && this.startPoint.Y > -1 && this.endPoint.X > -1 && this.endPoint.Y > -1)
- {
- Pen pen = new Pen(this.phaseColor, contoursWidth/*1*/);
- //pen.DashStyle = DashStyle.Dash;
- g.DrawLine(pen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
- pen.Dispose();
- }
- // 折线添加
- if (this.pointKtype == 1 && pointArray.Count >= 2)
- {
- Pen pen = new Pen(this.phaseColor, contoursWidth/*1*/);
- g.DrawLines(pen, pointArray.ToArray());
- pen.Dispose();
- }
- }
- private void InitializeLanguageText()
- {
- this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
- this.button9.Text = PdnResources.GetString("CommonAction.Undo");
- this.button2.Text = PdnResources.GetString("Menu.File.Close.Text");
- this.button1.Text = PdnResources.GetString("Form.OkButton.Text");
- this.groupBox2.Text = PdnResources.GetString("Menu.Addtheoutline.Text");
- this.button4.Text = PdnResources.GetString("Menu.Lineadd.text");
- this.button3.Text = PdnResources.GetString("Menu.Polylineadd.text");
- this.groupBox3.Text = PdnResources.GetString("Menu.Deletetheprofil.Text");
- this.button8.Text = PdnResources.GetString("Menu.Ellipsedelete.text");
- this.button7.Text = PdnResources.GetString("Menu.BinaryAction.RectangleDelete.Text");
- this.button6.Text = PdnResources.GetString("Menu.rounddelete.text");
- this.button5.Text = PdnResources.GetString("Menu.BinaryAction.PolygonleDelete.Text");
- this.groupBox7.Text = PdnResources.GetString("Menu.Preview.text");
- this.Text = PdnResources.GetString("Menu.Outlineoftheeditor.Text");
- }
- private void InitializeComponent()
- {
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.button9 = new System.Windows.Forms.Button();
- this.button2 = new System.Windows.Forms.Button();
- this.button1 = new System.Windows.Forms.Button();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- this.button4 = new System.Windows.Forms.Button();
- this.button3 = new System.Windows.Forms.Button();
- this.groupBox3 = new System.Windows.Forms.GroupBox();
- this.button8 = new System.Windows.Forms.Button();
- this.button7 = new System.Windows.Forms.Button();
- this.button6 = new System.Windows.Forms.Button();
- this.button5 = new System.Windows.Forms.Button();
- this.groupBox7 = new System.Windows.Forms.GroupBox();
- this.groupBox1.SuspendLayout();
- this.groupBox2.SuspendLayout();
- this.groupBox3.SuspendLayout();
- this.SuspendLayout();
- //
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.button9);
- this.groupBox1.Controls.Add(this.button2);
- this.groupBox1.Controls.Add(this.button1);
- this.groupBox1.Location = new System.Drawing.Point(12, 12);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(808, 45);
- this.groupBox1.TabIndex = 0;
- this.groupBox1.TabStop = false;
- //
- // button9
- //
- this.button9.Location = new System.Drawing.Point(468, 13);
- this.button9.Name = "button9";
- this.button9.Size = new System.Drawing.Size(91, 26);
- this.button9.TabIndex = 2;
- this.button9.UseVisualStyleBackColor = true;
- this.button9.Visible = false;
- this.button9.Click += new System.EventHandler(this.button9_Click);
- //
- // button2
- //
- this.button2.Location = new System.Drawing.Point(694, 13);
- this.button2.Name = "button2";
- this.button2.Size = new System.Drawing.Size(91, 26);
- this.button2.TabIndex = 1;
- this.button2.UseVisualStyleBackColor = true;
- this.button2.Click += new System.EventHandler(this.button2_Click);
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(582, 13);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(91, 26);
- this.button1.TabIndex = 0;
- this.button1.UseVisualStyleBackColor = true;
- this.button1.Click += new System.EventHandler(this.button1_Click);
- //
- // groupBox2
- //
- this.groupBox2.Controls.Add(this.button4);
- this.groupBox2.Controls.Add(this.button3);
- this.groupBox2.Location = new System.Drawing.Point(12, 63);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Size = new System.Drawing.Size(233, 106);
- this.groupBox2.TabIndex = 1;
- this.groupBox2.TabStop = false;
- //
- // button4
- //
- this.button4.Location = new System.Drawing.Point(135, 31);
- this.button4.Name = "button4";
- this.button4.Size = new System.Drawing.Size(73, 26);
- this.button4.TabIndex = 3;
- this.button4.UseVisualStyleBackColor = true;
- this.button4.Click += new System.EventHandler(this.button4_Click);
- //
- // button3
- //
- this.button3.Location = new System.Drawing.Point(28, 31);
- this.button3.Name = "button3";
- this.button3.Size = new System.Drawing.Size(73, 26);
- this.button3.TabIndex = 2;
- this.button3.UseVisualStyleBackColor = true;
- this.button3.Click += new System.EventHandler(this.button3_Click);
- //
- // groupBox3
- //
- this.groupBox3.Controls.Add(this.button8);
- this.groupBox3.Controls.Add(this.button7);
- this.groupBox3.Controls.Add(this.button6);
- this.groupBox3.Controls.Add(this.button5);
- this.groupBox3.Location = new System.Drawing.Point(12, 175);
- this.groupBox3.Name = "groupBox3";
- this.groupBox3.Size = new System.Drawing.Size(233, 106);
- this.groupBox3.TabIndex = 1;
- this.groupBox3.TabStop = false;
- //
- // button8
- //
- this.button8.Location = new System.Drawing.Point(135, 68);
- this.button8.Name = "button8";
- this.button8.Size = new System.Drawing.Size(73, 26);
- this.button8.TabIndex = 7;
- this.button8.UseVisualStyleBackColor = true;
- this.button8.Click += new System.EventHandler(this.button8_Click);
- //
- // button7
- //
- this.button7.Location = new System.Drawing.Point(28, 68);
- this.button7.Name = "button7";
- this.button7.Size = new System.Drawing.Size(73, 26);
- this.button7.TabIndex = 6;
- this.button7.UseVisualStyleBackColor = true;
- this.button7.Click += new System.EventHandler(this.button7_Click);
- //
- // button6
- //
- this.button6.Location = new System.Drawing.Point(135, 24);
- this.button6.Name = "button6";
- this.button6.Size = new System.Drawing.Size(73, 26);
- this.button6.TabIndex = 5;
- this.button6.UseVisualStyleBackColor = true;
- this.button6.Click += new System.EventHandler(this.button6_Click);
- //
- // button5
- //
- this.button5.Location = new System.Drawing.Point(28, 24);
- this.button5.Name = "button5";
- this.button5.Size = new System.Drawing.Size(73, 26);
- this.button5.TabIndex = 4;
- this.button5.UseVisualStyleBackColor = true;
- this.button5.Click += new System.EventHandler(this.button5_Click);
- //
- // groupBox7
- //
- this.groupBox7.Location = new System.Drawing.Point(251, 63);
- this.groupBox7.Name = "groupBox7";
- this.groupBox7.Size = new System.Drawing.Size(569, 502);
- this.groupBox7.TabIndex = 1;
- this.groupBox7.TabStop = false;
- //
- // GrainContoursEditingDialog
- //
- this.ClientSize = new System.Drawing.Size(832, 577);
- this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.groupBox3);
- this.Controls.Add(this.groupBox7);
- this.Controls.Add(this.groupBox1);
- this.MaximizeBox = false;
- this.MinimizeBox = false;
- this.Name = "GrainContoursEditingDialog";
- this.groupBox1.ResumeLayout(false);
- this.groupBox2.ResumeLayout(false);
- this.groupBox3.ResumeLayout(false);
- this.ResumeLayout(false);
- }
- /// <summary>
- /// 折线添加
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button3_Click(object sender, EventArgs e)
- {
- pointArray.Clear();
- startPoint = new Point(-1, -1);
- endPoint = new Point(-1, -1);
- pointKtype = 1;
- this.documentWorkspace.Refresh();
- }
- /// <summary>
- /// 直线添加
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button4_Click(object sender, EventArgs e)
- {
- startPoint = new Point(-1, -1);
- endPoint = new Point(-1, -1);
- pointKtype = 2;
- this.documentWorkspace.Refresh();
- }
- private void InitCommonButtonEvent()
- {
- this.commonControlButtons.zoomInButton.Click += new EventHandler(zoomInButton_Click);
- this.commonControlButtons.zoomOutButton.Click += new EventHandler(zoomOutButton_Click);
- this.commonControlButtons.zoomToWindowButton.Click += new EventHandler(zoomToWindowButton_Click);
- this.commonControlButtons.actualSizeButton.Click += new EventHandler(actualSizeButton_Click);
- this.commonControlButtons.pointerButton.Click += new EventHandler(pointerButton_Click);
- this.commonControlButtons.mobileModeButton.Click += new EventHandler(mobileModeButton_Click);
- }
- private void zoomInButton_Click(object sender, EventArgs e)
- {
- this.documentWorkspace.ZoomIn();
- }
- private void zoomOutButton_Click(object sender, EventArgs e)
- {
- this.documentWorkspace.ZoomOut();
- }
- private void zoomToWindowButton_Click(object sender, EventArgs e)
- {
- this.documentWorkspace.ZoomBasis = ZoomBasis.FitToWindow;
- }
- private void actualSizeButton_Click(object sender, EventArgs e)
- {
- this.documentWorkspace.ZoomBasis = ZoomBasis.ScaleFactor;
- this.documentWorkspace.ScaleFactor = ScaleFactor.OneToOne;
- }
- private void pointerButton_Click(object sender, EventArgs e)
- {
- this.documentWorkspace.ActiveTool = Annotation.Enum.DrawToolType.Pointer;
- }
- private void mobileModeButton_Click(object sender, EventArgs e)
- {
- this.documentWorkspace.ActiveTool = Annotation.Enum.DrawToolType.MoveMode;
- }
- //确定按钮点击事件
- private void button1_Click(object sender, EventArgs e)
- {
- this.PhaseMat = this.documentWorkspace.PhaseModels[0].mat;
- //Cv2.ImShow("dst", this.documentWorkspace.PhaseModels[0].mat);
- this.DialogResult = DialogResult.OK;
- this.Close();
- }
- /// <summary>
- /// 用于get处理后的图像
- /// </summary>
- public Mat PhaseMat
- {
- get
- {
- return this.phaseMat;
- }
- set
- {
- this.phaseMat = value;
- }
- }
- /// <summary>
- /// 用于get处理后的图像
- /// </summary>
- public OpenCvSharp.Point[][] PhaseContours
- {
- get
- {
- return this.contours.ToList().ToArray();
- }
- }
- /// <summary>
- /// 矩形删除
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button7_Click(object sender, EventArgs e)
- {
- startPoint = new Point(-1, -1);
- endPoint = new Point(-1, -1);
- this.rectangle.X = -1;
- this.rectangle.Y = -1;
- this.rectangle.Width = 0;
- this.rectangle.Height = 0;
- this.pointKtype = 13;
- this.documentWorkspace.Refresh();
- }
- /// <summary>
- /// 椭圆删除
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button8_Click(object sender, EventArgs e)
- {
- startPoint = new Point(-1, -1);
- endPoint = new Point(-1, -1);
- this.rectangle.X = -1;
- this.rectangle.Y = -1;
- this.rectangle.Width = 0;
- this.rectangle.Height = 0;
- this.pointKtype = 14;
- this.documentWorkspace.Refresh();
- }
- /// <summary>
- /// 圆形删除
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button6_Click(object sender, EventArgs e)
- {
- startPoint = new Point(-1, -1);
- endPoint = new Point(-1, -1);
- this.rectangle.X = -1;
- this.rectangle.Y = -1;
- this.rectangle.Width = 0;
- this.rectangle.Height = 0;
- this.pointKtype = 12;
- this.documentWorkspace.Refresh();
- }
- /// <summary>
- /// 多边形删除
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button5_Click(object sender, EventArgs e)
- {
- pointArray.Clear();
- startPoint = new Point(-1, -1);
- endPoint = new Point(-1, -1);
- this.rectangle.X = -1;
- this.rectangle.Y = -1;
- this.rectangle.Width = 0;
- this.rectangle.Height = 0;
- this.pointKtype = 11;
- this.documentWorkspace.Refresh();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- /// <summary>
- /// 撤销按钮点击事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button9_Click(object sender, EventArgs e)
- {
- }
- }
- }
|