123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753 |
- using OpenCvSharp;
- using PaintDotNet.Adjust.BaseImage;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- namespace PaintDotNet.Adjust
- {
- /// <summary>
- /// 预处理 - 交互操作功能
- /// </summary>
- public unsafe class PreActionIntent
- {
- /// <summary>
- /// 默认红色
- /// </summary>
- private static Vec4b vec4B;
- /// <summary>
- /// 全局的mat,用来做中间变量,但是需要注意销毁
- /// </summary>
- private static Mat phaseTemp, phaseTemp1;
- /// <summary>
- /// 目前用于删除时的中间变量颜色
- /// </summary>
- private static int colorTemp;
- /// <summary>
- /// 用于矩形删除的时候的中间变量
- /// </summary>
- private static RectangleF rectangle;
- /// <summary>
- /// 用于多边形删除的时候的中间变量
- /// </summary>
- private static List<System.Drawing.PointF> pointsTemp;
- private static Vec4b oldVec4B, newVec4B;
- #region 提取
- /// <summary>
- /// 单个提取
- /// </summary>
- /// <param name="source">源图像Mat</param>
- /// <param name="phase">选中的相的Mat,没有相或没有选中相为null</param>
- /// <param name="point">双击的坐标点</param>
- /// <returns></returns>
- public static Mat SingleExtract(Mat source, Mat phase, System.Drawing.PointF point, Vec4b vec4b)
- {
- try
- {
- vec4B = vec4b;
- phaseTemp1 = new Mat();
- if(phase==null)
- {
- phase = new Mat(source.Rows, source.Cols, MatType.CV_8UC4, new Scalar(0, 0, 0, 0));
- }
- phaseTemp = new Mat(source.Rows + 2, source.Cols + 2, MatType.CV_8U, Scalar.All(0));
- Mat sourceTemp = new Mat();
- source.CopyTo(sourceTemp);
- sourceTemp = sourceTemp.CvtColor(ColorConversionCodes.BGRA2BGR);
- phase.CopyTo(phaseTemp1);
- Rect rect = new Rect();
- Cv2.FloodFill(sourceTemp, phaseTemp, new OpenCvSharp.Point(point.X, point.Y), new Scalar(255), out rect, null, null, FloodFillFlags.Link8 | FloodFillFlags.MaskOnly);
- phaseTemp.ForEachAsByte(ForeachFunctionByteForChoise);
- phaseTemp1.CopyTo(phase);
- }
- catch (Exception)
- {
- }
- finally
- {
- if (phaseTemp != null && !phaseTemp.IsDisposed) phaseTemp.Dispose();
- if (phaseTemp1 != null && !phaseTemp1.IsDisposed) phaseTemp1.Dispose();
- }
- return phase;
- }
- /// <summary>
- /// 单个提取的循环
- /// </summary>
- /// <param name="value"></param>
- /// <param name="position"></param>
- public unsafe static void ForeachFunctionByteForChoise(byte* value, int* position)
- {
- int y = position[0];
- int x = position[1];
- byte intencity = *value;
- if (intencity == 1 && x > 0 && y > 0 && x < phaseTemp.Width - 1 && y < phaseTemp.Height - 1)
- {
- phaseTemp1.Set<Vec4b>(y - 1, x - 1, vec4B);
- }
- }
- #endregion
- #region 选择
- /// <summary>
- /// 应用
- /// </summary>
- /// <param name="phase"></param>
- /// <param name="color"></param>
- /// <returns></returns>
- public static Mat BinaryActionAntiConfirm(Mat phase, int color)
- {
- phaseTemp = new Mat();
- phase.CopyTo(phaseTemp);
- Color color1 = Color.FromArgb(color);
- Color color2 = Color.FromArgb(255-color1.R, 255 - color1.G, 255 - color1.B);
- oldVec4B = new Vec4b(color1.B, color1.G, color1.R, 255);
- newVec4B = new Vec4b(color2.B, color2.G, color2.R, 255);
- phase.ForEachAsVec4b(ForeachFunctionByteForConfirm);
- phaseTemp.CopyTo(phase);
- return phase;
- }
- private static void ForeachFunctionByteForConfirm(Vec4b* value, int* position)
- {
- int y = position[0];
- int x = position[1];
- if (value->Item0== oldVec4B.Item0 && value->Item1 == oldVec4B.Item1
- && value->Item2 == oldVec4B.Item2 && value->Item3 == oldVec4B.Item3)
- {
- phaseTemp.Set<Vec4b>(y, x, new Vec4b(0,0,0,0));
- }
- else if(value->Item0 == newVec4B.Item0 && value->Item1 == newVec4B.Item1
- && value->Item2 == newVec4B.Item2 && value->Item3 == newVec4B.Item3)
- {
- phaseTemp.Set<Vec4b>(y, x, oldVec4B);
- }
- }
- /// <summary>
- /// 单个选择
- /// </summary>
- /// <param name="phase"></param>
- /// <param name="point"></param>
- /// <param name="vec4b"></param>
- /// <returns></returns>
- public static Mat SingleChoise(Mat phase, int color, System.Drawing.Point point)
- {
- Color color1 = Color.FromArgb(color);
- Mat[] arr = phase.Split();
- Mat phase_temp = phase.CvtColor(ColorConversionCodes.BGRA2BGR);
- Cv2.FloodFill(phase_temp, new OpenCvSharp.Point(point.X, point.Y), new Scalar(255-color1.B, 255-color1.G, 255-color1.R));
- Mat[] arr1 = phase_temp.Split();
- arr[0] = arr1[0];
- arr[1] = arr1[1];
- arr[2] = arr1[2];
- Cv2.Merge(arr, phase);
- return phase;
- }
- /// <summary>
- /// 矩形选择
- /// </summary>
- /// <param name="phase"></param>
- /// <param name="rectangle"></param>
- /// <returns></returns>
- public static Mat SingleChoiseRectangle(Mat phase, int color, System.Drawing.RectangleF rectangle)
- {
- Color color1 = Color.FromArgb(color);
- Mat ImageROI = null, labels = null, stats = null, centroids = null;
- try
- {
- float ax = rectangle.X, ay = rectangle.Y;
- //处理矩形数据
- if (rectangle.Width <= 0 || rectangle.Height <= 0)
- {
- System.Drawing.PointF p1 = new System.Drawing.PointF(rectangle.X, rectangle.Y);
- System.Drawing.PointF p2 = new System.Drawing.PointF(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height);
- if (rectangle.Width <= 0 && rectangle.Height > 0)
- {
- ax = p2.X;
- ay = p1.Y;
- }
- else if (rectangle.Width > 0 && rectangle.Height <= 0)
- {
- ax = p1.X;
- ay = p2.Y;
- }
- else if (rectangle.Width <= 0 && rectangle.Height <= 0)
- {
- ax = p2.X;
- ay = p2.Y;
- }
- }
-
- Mat[] arr = phase.Split();
- //Cv2.Circle(phase, new OpenCvSharp.Point(ax, ay), 2, new Scalar(255), 3);
- ImageROI = new Mat(phase, new Rect((int)ax, (int)ay, (int)Math.Abs(rectangle.Width), (int)Math.Abs(rectangle.Height)));
- ImageROI = ImageROI.CvtColor(ColorConversionCodes.BGRA2GRAY);
- labels = new Mat();
- stats = new Mat();
- centroids = new Mat();
- phase = phase.CvtColor(ColorConversionCodes.BGRA2BGR);
- //获取矩形内的连通分量,图像传矩形内的图片
- int num = Cv2.ConnectedComponentsWithStats(ImageROI, labels, stats, centroids, PixelConnectivity.Connectivity8);
- if (centroids != null)
- {
- for (int y = 1; y < centroids.Height; y++)
- {
- OpenCvSharp.Point p = new OpenCvSharp.Point();
- p.X = Convert.ToInt32(centroids.At<double>(y, 0)) + (int)ax;
- p.Y = Convert.ToInt32(centroids.At<double>(y, 1)) + (int)ay;
- //Cv2.Circle(phase, p, 2, new Scalar(255), 3);
- if (arr[3].At<byte>(p.Y, p.X) == 255)
- {
- Rect rect = new Rect();
- Cv2.FloodFill(phase, p, new Scalar(255-color1.B, 255 - color1.G, 255 - color1.R), out rect, null, null, FloodFillFlags.Link8);
- }
- }
- }
- Mat[] arr1 = phase.Split();
- arr[0] = arr1[0];
- arr[1] = arr1[1];
- arr[2] = arr1[2];
- Cv2.Merge(arr, phase);
- }
- catch(Exception)
- {
- if (ImageROI != null && !ImageROI.IsDisposed) ImageROI.Dispose();
- if (labels != null && !labels.IsDisposed) labels.Dispose();
- if (stats != null && !stats.IsDisposed) stats.Dispose();
- if (centroids != null && !centroids.IsDisposed) centroids.Dispose();
- }
- finally
- {
- }
-
- return phase;
- }
- /// <summary>
- /// 椭圆选择
- /// </summary>
- /// <param name="phase"></param>
- /// <param name="rectangle"></param>
- /// <returns></returns>
- public static Mat SingleChoiseOval(Mat phase, int color, System.Drawing.RectangleF rect)
- {
- Color color1 = Color.FromArgb(color);
- //提取椭圆的数据
- Mat labels = null, stats = null, centroids = null;
- try
- {
- float ax = rectangle.X, ay = rectangle.Y;
- //处理矩形数据
- if (rectangle.Width <= 0 || rectangle.Height <= 0)
- {
- System.Drawing.PointF p1 = new System.Drawing.PointF(rectangle.X, rectangle.Y);
- System.Drawing.PointF p2 = new System.Drawing.PointF(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height);
- if (rectangle.Width <= 0 && rectangle.Height > 0)
- {
- ax = p2.X;
- ay = p1.Y;
- }
- else if (rectangle.Width > 0 && rectangle.Height <= 0)
- {
- ax = p1.X;
- ay = p2.Y;
- }
- else if (rectangle.Width <= 0 && rectangle.Height <= 0)
- {
- ax = p2.X;
- ay = p2.Y;
- }
- }
- rectangle = rect;
- vec4B = new Vec4b(255, 0, 0, 255);
- Mat[] arr = phase.Split();
- phaseTemp = new Mat(phase.Size(), phase.Type());
- phase.ForEachAsVec4b(ForeachFunctionByteForOvalChoise);
- labels = new Mat();
- stats = new Mat();
- centroids = new Mat();
- phaseTemp = phaseTemp.CvtColor(ColorConversionCodes.BGRA2GRAY);
- phase = phase.CvtColor(ColorConversionCodes.BGRA2BGR);
-
- //获取矩形内的连通分量,图像传矩形内的图片
- int num = Cv2.ConnectedComponentsWithStats(phaseTemp, labels, stats, centroids, PixelConnectivity.Connectivity8);
- if (centroids != null)
- {
- for (int y = 1; y < centroids.Height; y++)
- {
- OpenCvSharp.Point p = new OpenCvSharp.Point();
- p.X = Convert.ToInt32(centroids.At<double>(y, 0));// + ax;
- p.Y = Convert.ToInt32(centroids.At<double>(y, 1));// + ay;
- //Cv2.Circle(phase, p, 2, new Scalar(255,255,255), 3);
- if (arr[3].At<byte>(p.Y, p.X) == 255)
- {
- Rect rect1 = new Rect();
- Cv2.FloodFill(phase, p, new Scalar(255- color1.B, 255- color1.G, 255- color1.R), out rect1, null, null, FloodFillFlags.Link8);
- }
- }
- }
- Mat[] arr1 = phase.Split();
- arr[0] = arr1[0];
- arr[1] = arr1[1];
- arr[2] = arr1[2];
- Cv2.Merge(arr, phase);
- }
- catch (Exception)
- {
- if (phaseTemp != null && !phaseTemp.IsDisposed) phaseTemp.Dispose();
- if (labels != null && !labels.IsDisposed) labels.Dispose();
- if (stats != null && !stats.IsDisposed) stats.Dispose();
- if (centroids != null && !centroids.IsDisposed) centroids.Dispose();
- }
- finally
- {
- }
- return phase;
- }
- private static void ForeachFunctionByteForOvalChoise(Vec4b* value, int* position)
- {
- int y = position[0];
- int x = position[1];
- if (BaseTools.isPointInOval(new System.Drawing.Point(x, y),
- new System.Drawing.PointF(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2),
- rectangle.Width / 2,
- rectangle.Height / 2
- ))
- {
- if (value->Item3 > 0)
- {
- phaseTemp.Set<Vec4b>(y, x, new Vec4b(value->Item0, value->Item1, value->Item2, value->Item3));
- }
- }
- }
- //多边形选择
- //https://blog.csdn.net/qq_31839479/article/details/53025067
- public static Mat SingleChoisePolygon(Mat phase, int color, List<System.Drawing.PointF> points)
- {
- Color color1 = Color.FromArgb(color);
- //提取椭圆的数据
- Mat labels = null, stats = null, centroids = null;
- try
- {
- float ax = rectangle.X, ay = rectangle.Y;
- //处理矩形数据
- if (rectangle.Width <= 0 || rectangle.Height <= 0)
- {
- System.Drawing.PointF p1 = new System.Drawing.PointF(rectangle.X, rectangle.Y);
- System.Drawing.PointF p2 = new System.Drawing.PointF(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height);
- if (rectangle.Width <= 0 && rectangle.Height > 0)
- {
- ax = p2.X;
- ay = p1.Y;
- }
- else if (rectangle.Width > 0 && rectangle.Height <= 0)
- {
- ax = p1.X;
- ay = p2.Y;
- }
- else if (rectangle.Width <= 0 && rectangle.Height <= 0)
- {
- ax = p2.X;
- ay = p2.Y;
- }
- }
- pointsTemp = points;
- vec4B = new Vec4b(255, 0, 0, 255);
- Mat[] arr = phase.Split();
- phaseTemp = new Mat(phase.Size(), phase.Type());
- phase.ForEachAsVec4b(ForeachFunctionByteForPolygonChoise);
- labels = new Mat();
- stats = new Mat();
- centroids = new Mat();
- phaseTemp = phaseTemp.CvtColor(ColorConversionCodes.BGRA2GRAY);
- phase = phase.CvtColor(ColorConversionCodes.BGRA2BGR);
- //获取矩形内的连通分量,图像传矩形内的图片
- int num = Cv2.ConnectedComponentsWithStats(phaseTemp, labels, stats, centroids, PixelConnectivity.Connectivity8);
- if (centroids != null)
- {
- for (int y = 1; y < centroids.Height; y++)
- {
- OpenCvSharp.Point p = new OpenCvSharp.Point();
- p.X = Convert.ToInt32(centroids.At<double>(y, 0));// + ax;
- p.Y = Convert.ToInt32(centroids.At<double>(y, 1));// + ay;
- //Cv2.Circle(phase, p, 2, new Scalar(255,255,255), 3);
- if (arr[3].At<byte>(p.Y, p.X) == 255)
- {
- Rect rect1 = new Rect();
- Cv2.FloodFill(phase, p, new Scalar(255- color1.B, 255 - color1.G, 255 - color1.R), out rect1, null, null, FloodFillFlags.Link8);
- }
- }
- }
- Mat[] arr1 = phase.Split();
- arr[0] = arr1[0];
- arr[1] = arr1[1];
- arr[2] = arr1[2];
- Cv2.Merge(arr, phase);
- }
- catch (Exception)
- {
- if (phaseTemp != null && !phaseTemp.IsDisposed) phaseTemp.Dispose();
- if (labels != null && !labels.IsDisposed) labels.Dispose();
- if (stats != null && !stats.IsDisposed) stats.Dispose();
- if (centroids != null && !centroids.IsDisposed) centroids.Dispose();
- }
- finally
- {
- }
- return phase;
- }
- private static void ForeachFunctionByteForPolygonChoise(Vec4b* value, int* position)
- {
- int y = position[0];
- int x = position[1];
- //lock (obj)
- //原来用region判断,但是多线程崩溃,只能加锁,所以改成射线法判断点是否在
- //if (region.IsVisible(x, y))
- if (BaseTools.isPointInPolygon(new System.Drawing.Point(x, y), pointsTemp))
- {
- if (value->Item3 > 0)
- phaseTemp.Set<Vec4b>(y, x, vec4B);
- }
- }
- #endregion
- #region 删除
- /// <summary>
- /// 单个删除
- /// </summary>
- /// <returns></returns>
- public static Mat SingleDelete(Mat phase, System.Drawing.Point point, int color)
- {
- try
- {
- phaseTemp1 = new Mat();
- vec4B = new Vec4b(0, 0, 0, 0);
- Rect rect = new Rect();
- Mat[] arr = phase.Split();
- phaseTemp = phase.CvtColor(ColorConversionCodes.BGRA2BGR);
- if (color == 0) { colorTemp = 1; }
- else if (color == 255) { colorTemp = 254; }
- else { colorTemp = color + 1; }
- Cv2.FloodFill(phaseTemp, new OpenCvSharp.Point(point.X, point.Y), new Scalar(colorTemp, colorTemp, colorTemp), out rect, null, null, FloodFillFlags.Link8);
- Mat[] arr1 = phaseTemp.Split();
- arr[0] = arr1[0];
- arr[1] = arr1[1];
- arr[2] = arr1[2];
- Cv2.Merge(arr, phaseTemp);
- phaseTemp.CopyTo(phaseTemp1);
- phaseTemp.ForEachAsVec4b(ForeachFunctionByteForDelete);
- phaseTemp1.CopyTo(phase);
- }
- catch(Exception)
- {
- }
- finally
- {
- if (phaseTemp != null && !phaseTemp.IsDisposed) phaseTemp.Dispose();
- if (phaseTemp1 != null && !phaseTemp1.IsDisposed) phaseTemp1.Dispose();
- }
- return phase;
- }
- /// <summary>
- /// 矩形删除
- /// </summary>
- /// <param name="phase"></param>
- /// <param name="rect"></param>
- /// <returns></returns>
- public static Mat RectangleDelete(Mat phase, System.Drawing.RectangleF rect)
- {
- try
- {
- vec4B = new Vec4b(0, 0, 0, 0);
- rectangle = rect;
- phaseTemp = new Mat();
- phase.CopyTo(phaseTemp);
- phase.ForEachAsVec4b(ForeachFunctionByteForRectangleDelete);
- phaseTemp.CopyTo(phase);
- }
- catch(Exception)
- {
- }
- finally
- {
- if (phaseTemp != null && !phaseTemp.IsDisposed) phaseTemp.Dispose();
- }
- return phase;
- }
- /// <summary>
- /// 多边形删除
- /// </summary>
- /// <param name="phase"></param>
- /// <param name="rect"></param>
- /// <returns></returns>
- public static Mat PolygonDelete(Mat phase, List<System.Drawing.PointF> points)
- {
- try
- {
- pointsTemp = points;
- vec4B = new Vec4b(0, 0, 0, 0);
- phaseTemp = new Mat();
- phase.CopyTo(phaseTemp);
- phase.ForEachAsVec4b(ForeachFunctionByteForPolygonDelete);
- phaseTemp.CopyTo(phase);
- }
- catch(Exception)
- {
- }
- finally
- {
- if (phaseTemp != null && !phaseTemp.IsDisposed) phaseTemp.Dispose();
- pointsTemp = null;
- }
- return phase;
- }
- /// <summary>
- /// 椭圆删除
- /// </summary>
- /// <param name="phase"></param>
- /// <param name="rect"></param>
- /// <returns></returns>
- public static Mat OvalDelete(Mat phase, System.Drawing.RectangleF rect)
- {
- try
- {
- vec4B = new Vec4b(0, 0, 0, 0);
- rectangle = rect;
- phaseTemp = new Mat();
- phase.CopyTo(phaseTemp);
- phase.ForEachAsVec4b(ForeachFunctionByteForOvalDelete);
- phaseTemp.CopyTo(phase);
- }
- catch(Exception)
- {
- }
- finally
- {
- if (phaseTemp != null && !phaseTemp.IsDisposed) phaseTemp.Dispose();
- }
- return phase;
- }
- /// <summary>
- /// 椭圆删除
- /// </summary>
- /// <param name="value"></param>
- /// <param name="position"></param>
- private static void ForeachFunctionByteForOvalDelete(Vec4b* value, int* position)
- {
- int y = position[0];
- int x = position[1];
- if (BaseTools.isPointInOval(new System.Drawing.Point(x, y),
- new System.Drawing.PointF(rectangle.X + rectangle.Width/2, rectangle.Y + rectangle.Height / 2),
- rectangle.Width / 2,
- rectangle.Height / 2
- ))
- {
- phaseTemp.Set<Vec4b>(y, x, vec4B);
- }
- }
- /// <summary>
- /// 多边形删除
- /// </summary>
- /// <param name="value"></param>
- /// <param name="position"></param>
- private static void ForeachFunctionByteForPolygonDelete(Vec4b* value, int* position)
- {
- int y = position[0];
- int x = position[1];
- //lock (obj)
- //原来用region判断,但是多线程崩溃,只能加锁,所以改成射线法判断点是否在
- //if (region.IsVisible(x, y))
- if (BaseTools.isPointInPolygon(new System.Drawing.Point(x, y), pointsTemp))
- {
- phaseTemp.Set<Vec4b>(y, x, vec4B);
- }
- }
- /// <summary>
- /// 矩形删除
- /// </summary>
- /// <param name="value"></param>
- /// <param name="position"></param>
- private static void ForeachFunctionByteForRectangleDelete(Vec4b* value, int* position)
- {
- int y = position[0];
- int x = position[1];
- if(rectangle.Contains(x,y))
- {
- phaseTemp.Set<Vec4b>(y, x, vec4B);
- }
- }
- /// <summary>
- /// 单个删除的循环
- /// </summary>
- /// <param name="value"></param>
- /// <param name="position"></param>
- private static void ForeachFunctionByteForDelete(Vec4b* value, int* position)
- {
- int y = position[0];
- int x = position[1];
- if (value->Item0 == colorTemp)
- {
- phaseTemp1.Set<Vec4b>(y, x, vec4B);
- }
- }
- #endregion
- #region 分割
- /// <summary>
- /// 直线分割
- /// </summary>
- /// <param name="phase">相</param>
- /// <param name="point">点</param>
- /// <param name="color">颜色</param>
- /// <returns></returns>
- public static Mat LineSplit(Mat phase, System.Drawing.PointF start, System.Drawing.PointF end, int width)
- {
- Cv2.Line(phase,
- new OpenCvSharp.Point(start.X, start.Y),
- new OpenCvSharp.Point(end.X, end.Y), new Scalar(0, 0, 0, 0), width, LineTypes.Link8);
- return phase;
- }
- /// <summary>
- /// 椭圆分割
- /// </summary>
- /// <param name="phase"></param>
- public static Mat EllipseSplit(Mat phase, System.Drawing.RectangleF rectangle, int width)
- {
- Point2f point2F = new Point2f(rectangle.X+ rectangle.Width/2, rectangle.Y + rectangle.Height/2);
- Size2f size2F = new Size2f(Math.Abs(rectangle.Width), Math.Abs(rectangle.Height));
- Cv2.Ellipse(phase, new RotatedRect(point2F, size2F, 0), new Scalar(0, 0, 0, 0), width, LineTypes.Link8);
- return phase;
- }
- #endregion
- #region 连接
- /// <summary>
- /// 直线连接
- /// </summary>
- /// <param name="phase"></param>
- /// <param name="color"></param>
- /// <param name="start"></param>
- /// <param name="end"></param>
- /// <returns></returns>
- public static Mat LineConnection(Mat phase, int color, System.Drawing.PointF start, System.Drawing.PointF end, int width)
- {
- Color color1 = Color.FromArgb(color);
- Cv2.Line(phase,
- new OpenCvSharp.Point(start.X, start.Y),
- new OpenCvSharp.Point(end.X, end.Y), new Scalar(color1.B, color1.G, color1.R, 255), width, LineTypes.Link8);
- return phase;
- }
- /// <summary>
- /// 椭圆连接
- /// </summary>
- /// <param name="phase"></param>
- public static Mat EllipseConnection(Mat phase, int color, System.Drawing.RectangleF rectangle, int width)
- {
- Color color1 = Color.FromArgb(color);
- Point2f point2F = new Point2f(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2);
- Size2f size2F = new Size2f(Math.Abs(rectangle.Width), Math.Abs(rectangle.Height));
- Cv2.Ellipse(phase, new RotatedRect(point2F, size2F, 0), new Scalar(color1.B, color1.G, color1.R, 255), width, LineTypes.Link8);
- return phase;
- }
- #endregion
- #region 添加
- /// <summary>
- /// 椭圆添加
- /// </summary>
- /// <param name="phase"></param>
- public static Mat EllipseAdd(Mat phase, int color, System.Drawing.RectangleF rectangle)
- {
- Color color1 = Color.FromArgb(color);
- Point2f point2F = new Point2f(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2);
- Size2f size2F = new Size2f(Math.Abs(rectangle.Width), Math.Abs(rectangle.Height));
- Cv2.Ellipse(phase, new RotatedRect(point2F, size2F, 0), new Scalar(color1.B, color1.G, color1.R, 255), -1, LineTypes.Link8);
- return phase;
- }
- /// <summary>
- /// 矩形添加
- /// </summary>
- /// <param name="phase"></param>
- /// <param name="color"></param>
- /// <param name="rectangle"></param>
- /// <returns></returns>
- public static Mat RectangleAdd(Mat phase, int color, System.Drawing.RectangleF rectangle)
- {
- Color color1 = Color.FromArgb(color);
- Cv2.Rectangle(phase, new Rect((int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height), new Scalar(color1.B, color1.G, color1.R, 255), -1, LineTypes.Link8);
- return phase;
- }
- /// <summary>
- /// 多边形添加
- /// </summary>
- /// <param name="phase">相</param>
- /// <param name="color">相颜色</param>
- /// <param name="points">多边形顶点集合</param>
- /// <returns></returns>
- public static Mat PolygonAdd(Mat phase, int color, List<System.Drawing.PointF> points)
- {
- Color color1 = Color.FromArgb(color);
- OpenCvSharp.Point[][] arr = new OpenCvSharp.Point[1][];
- arr[0] = BaseTools.pointToPoint(points).ToArray();
- Cv2.FillPoly(phase, arr, new Scalar(color1.B, color1.G, color1.R, 255), LineTypes.Link8);
- return phase;
- }
- #endregion
- }
- }
|