PreActionIntent.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. using OpenCvSharp;
  2. using PaintDotNet.Adjust.BaseImage;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Linq;
  7. namespace PaintDotNet.Adjust
  8. {
  9. /// <summary>
  10. /// 预处理 - 交互操作功能
  11. /// </summary>
  12. public unsafe class PreActionIntent
  13. {
  14. /// <summary>
  15. /// 默认红色
  16. /// </summary>
  17. private static Vec4b vec4B;
  18. /// <summary>
  19. /// 全局的mat,用来做中间变量,但是需要注意销毁
  20. /// </summary>
  21. private static Mat phaseTemp, phaseTemp1;
  22. /// <summary>
  23. /// 目前用于删除时的中间变量颜色
  24. /// </summary>
  25. private static int colorTemp;
  26. /// <summary>
  27. /// 用于矩形删除的时候的中间变量
  28. /// </summary>
  29. private static RectangleF rectangle;
  30. /// <summary>
  31. /// 用于多边形删除的时候的中间变量
  32. /// </summary>
  33. private static List<System.Drawing.PointF> pointsTemp;
  34. private static Vec4b oldVec4B, newVec4B;
  35. #region 提取
  36. /// <summary>
  37. /// 单个提取
  38. /// </summary>
  39. /// <param name="source">源图像Mat</param>
  40. /// <param name="phase">选中的相的Mat,没有相或没有选中相为null</param>
  41. /// <param name="point">双击的坐标点</param>
  42. /// <returns></returns>
  43. public static Mat SingleExtract(Mat source, Mat phase, System.Drawing.PointF point, Vec4b vec4b)
  44. {
  45. try
  46. {
  47. vec4B = vec4b;
  48. phaseTemp1 = new Mat();
  49. if(phase==null)
  50. {
  51. phase = new Mat(source.Rows, source.Cols, MatType.CV_8UC4, new Scalar(0, 0, 0, 0));
  52. }
  53. phaseTemp = new Mat(source.Rows + 2, source.Cols + 2, MatType.CV_8U, Scalar.All(0));
  54. Mat sourceTemp = new Mat();
  55. source.CopyTo(sourceTemp);
  56. sourceTemp = sourceTemp.CvtColor(ColorConversionCodes.BGRA2BGR);
  57. phase.CopyTo(phaseTemp1);
  58. Rect rect = new Rect();
  59. Cv2.FloodFill(sourceTemp, phaseTemp, new OpenCvSharp.Point(point.X, point.Y), new Scalar(255), out rect, null, null, FloodFillFlags.Link8 | FloodFillFlags.MaskOnly);
  60. phaseTemp.ForEachAsByte(ForeachFunctionByteForChoise);
  61. phaseTemp1.CopyTo(phase);
  62. }
  63. catch (Exception)
  64. {
  65. }
  66. finally
  67. {
  68. if (phaseTemp != null && !phaseTemp.IsDisposed) phaseTemp.Dispose();
  69. if (phaseTemp1 != null && !phaseTemp1.IsDisposed) phaseTemp1.Dispose();
  70. }
  71. return phase;
  72. }
  73. /// <summary>
  74. /// 单个提取的循环
  75. /// </summary>
  76. /// <param name="value"></param>
  77. /// <param name="position"></param>
  78. public unsafe static void ForeachFunctionByteForChoise(byte* value, int* position)
  79. {
  80. int y = position[0];
  81. int x = position[1];
  82. byte intencity = *value;
  83. if (intencity == 1 && x > 0 && y > 0 && x < phaseTemp.Width - 1 && y < phaseTemp.Height - 1)
  84. {
  85. phaseTemp1.Set<Vec4b>(y - 1, x - 1, vec4B);
  86. }
  87. }
  88. #endregion
  89. #region 选择
  90. /// <summary>
  91. /// 应用
  92. /// </summary>
  93. /// <param name="phase"></param>
  94. /// <param name="color"></param>
  95. /// <returns></returns>
  96. public static Mat BinaryActionAntiConfirm(Mat phase, int color)
  97. {
  98. phaseTemp = new Mat();
  99. phase.CopyTo(phaseTemp);
  100. Color color1 = Color.FromArgb(color);
  101. Color color2 = Color.FromArgb(255-color1.R, 255 - color1.G, 255 - color1.B);
  102. oldVec4B = new Vec4b(color1.B, color1.G, color1.R, 255);
  103. newVec4B = new Vec4b(color2.B, color2.G, color2.R, 255);
  104. phase.ForEachAsVec4b(ForeachFunctionByteForConfirm);
  105. phaseTemp.CopyTo(phase);
  106. return phase;
  107. }
  108. private static void ForeachFunctionByteForConfirm(Vec4b* value, int* position)
  109. {
  110. int y = position[0];
  111. int x = position[1];
  112. if (value->Item0== oldVec4B.Item0 && value->Item1 == oldVec4B.Item1
  113. && value->Item2 == oldVec4B.Item2 && value->Item3 == oldVec4B.Item3)
  114. {
  115. phaseTemp.Set<Vec4b>(y, x, new Vec4b(0,0,0,0));
  116. }
  117. else if(value->Item0 == newVec4B.Item0 && value->Item1 == newVec4B.Item1
  118. && value->Item2 == newVec4B.Item2 && value->Item3 == newVec4B.Item3)
  119. {
  120. phaseTemp.Set<Vec4b>(y, x, oldVec4B);
  121. }
  122. }
  123. /// <summary>
  124. /// 单个选择
  125. /// </summary>
  126. /// <param name="phase"></param>
  127. /// <param name="point"></param>
  128. /// <param name="vec4b"></param>
  129. /// <returns></returns>
  130. public static Mat SingleChoise(Mat phase, int color, System.Drawing.Point point)
  131. {
  132. Color color1 = Color.FromArgb(color);
  133. Mat[] arr = phase.Split();
  134. Mat phase_temp = phase.CvtColor(ColorConversionCodes.BGRA2BGR);
  135. Cv2.FloodFill(phase_temp, new OpenCvSharp.Point(point.X, point.Y), new Scalar(255-color1.B, 255-color1.G, 255-color1.R));
  136. Mat[] arr1 = phase_temp.Split();
  137. arr[0] = arr1[0];
  138. arr[1] = arr1[1];
  139. arr[2] = arr1[2];
  140. Cv2.Merge(arr, phase);
  141. return phase;
  142. }
  143. /// <summary>
  144. /// 矩形选择
  145. /// </summary>
  146. /// <param name="phase"></param>
  147. /// <param name="rectangle"></param>
  148. /// <returns></returns>
  149. public static Mat SingleChoiseRectangle(Mat phase, int color, System.Drawing.RectangleF rectangle)
  150. {
  151. Color color1 = Color.FromArgb(color);
  152. Mat ImageROI = null, labels = null, stats = null, centroids = null;
  153. try
  154. {
  155. float ax = rectangle.X, ay = rectangle.Y;
  156. //处理矩形数据
  157. if (rectangle.Width <= 0 || rectangle.Height <= 0)
  158. {
  159. System.Drawing.PointF p1 = new System.Drawing.PointF(rectangle.X, rectangle.Y);
  160. System.Drawing.PointF p2 = new System.Drawing.PointF(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height);
  161. if (rectangle.Width <= 0 && rectangle.Height > 0)
  162. {
  163. ax = p2.X;
  164. ay = p1.Y;
  165. }
  166. else if (rectangle.Width > 0 && rectangle.Height <= 0)
  167. {
  168. ax = p1.X;
  169. ay = p2.Y;
  170. }
  171. else if (rectangle.Width <= 0 && rectangle.Height <= 0)
  172. {
  173. ax = p2.X;
  174. ay = p2.Y;
  175. }
  176. }
  177. Mat[] arr = phase.Split();
  178. //Cv2.Circle(phase, new OpenCvSharp.Point(ax, ay), 2, new Scalar(255), 3);
  179. ImageROI = new Mat(phase, new Rect((int)ax, (int)ay, (int)Math.Abs(rectangle.Width), (int)Math.Abs(rectangle.Height)));
  180. ImageROI = ImageROI.CvtColor(ColorConversionCodes.BGRA2GRAY);
  181. labels = new Mat();
  182. stats = new Mat();
  183. centroids = new Mat();
  184. phase = phase.CvtColor(ColorConversionCodes.BGRA2BGR);
  185. //获取矩形内的连通分量,图像传矩形内的图片
  186. int num = Cv2.ConnectedComponentsWithStats(ImageROI, labels, stats, centroids, PixelConnectivity.Connectivity8);
  187. if (centroids != null)
  188. {
  189. for (int y = 1; y < centroids.Height; y++)
  190. {
  191. OpenCvSharp.Point p = new OpenCvSharp.Point();
  192. p.X = Convert.ToInt32(centroids.At<double>(y, 0)) + (int)ax;
  193. p.Y = Convert.ToInt32(centroids.At<double>(y, 1)) + (int)ay;
  194. //Cv2.Circle(phase, p, 2, new Scalar(255), 3);
  195. if (arr[3].At<byte>(p.Y, p.X) == 255)
  196. {
  197. Rect rect = new Rect();
  198. Cv2.FloodFill(phase, p, new Scalar(255-color1.B, 255 - color1.G, 255 - color1.R), out rect, null, null, FloodFillFlags.Link8);
  199. }
  200. }
  201. }
  202. Mat[] arr1 = phase.Split();
  203. arr[0] = arr1[0];
  204. arr[1] = arr1[1];
  205. arr[2] = arr1[2];
  206. Cv2.Merge(arr, phase);
  207. }
  208. catch(Exception)
  209. {
  210. if (ImageROI != null && !ImageROI.IsDisposed) ImageROI.Dispose();
  211. if (labels != null && !labels.IsDisposed) labels.Dispose();
  212. if (stats != null && !stats.IsDisposed) stats.Dispose();
  213. if (centroids != null && !centroids.IsDisposed) centroids.Dispose();
  214. }
  215. finally
  216. {
  217. }
  218. return phase;
  219. }
  220. /// <summary>
  221. /// 椭圆选择
  222. /// </summary>
  223. /// <param name="phase"></param>
  224. /// <param name="rectangle"></param>
  225. /// <returns></returns>
  226. public static Mat SingleChoiseOval(Mat phase, int color, System.Drawing.RectangleF rect)
  227. {
  228. Color color1 = Color.FromArgb(color);
  229. //提取椭圆的数据
  230. Mat labels = null, stats = null, centroids = null;
  231. try
  232. {
  233. float ax = rectangle.X, ay = rectangle.Y;
  234. //处理矩形数据
  235. if (rectangle.Width <= 0 || rectangle.Height <= 0)
  236. {
  237. System.Drawing.PointF p1 = new System.Drawing.PointF(rectangle.X, rectangle.Y);
  238. System.Drawing.PointF p2 = new System.Drawing.PointF(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height);
  239. if (rectangle.Width <= 0 && rectangle.Height > 0)
  240. {
  241. ax = p2.X;
  242. ay = p1.Y;
  243. }
  244. else if (rectangle.Width > 0 && rectangle.Height <= 0)
  245. {
  246. ax = p1.X;
  247. ay = p2.Y;
  248. }
  249. else if (rectangle.Width <= 0 && rectangle.Height <= 0)
  250. {
  251. ax = p2.X;
  252. ay = p2.Y;
  253. }
  254. }
  255. rectangle = rect;
  256. vec4B = new Vec4b(255, 0, 0, 255);
  257. Mat[] arr = phase.Split();
  258. phaseTemp = new Mat(phase.Size(), phase.Type());
  259. phase.ForEachAsVec4b(ForeachFunctionByteForOvalChoise);
  260. labels = new Mat();
  261. stats = new Mat();
  262. centroids = new Mat();
  263. phaseTemp = phaseTemp.CvtColor(ColorConversionCodes.BGRA2GRAY);
  264. phase = phase.CvtColor(ColorConversionCodes.BGRA2BGR);
  265. //获取矩形内的连通分量,图像传矩形内的图片
  266. int num = Cv2.ConnectedComponentsWithStats(phaseTemp, labels, stats, centroids, PixelConnectivity.Connectivity8);
  267. if (centroids != null)
  268. {
  269. for (int y = 1; y < centroids.Height; y++)
  270. {
  271. OpenCvSharp.Point p = new OpenCvSharp.Point();
  272. p.X = Convert.ToInt32(centroids.At<double>(y, 0));// + ax;
  273. p.Y = Convert.ToInt32(centroids.At<double>(y, 1));// + ay;
  274. //Cv2.Circle(phase, p, 2, new Scalar(255,255,255), 3);
  275. if (arr[3].At<byte>(p.Y, p.X) == 255)
  276. {
  277. Rect rect1 = new Rect();
  278. Cv2.FloodFill(phase, p, new Scalar(255- color1.B, 255- color1.G, 255- color1.R), out rect1, null, null, FloodFillFlags.Link8);
  279. }
  280. }
  281. }
  282. Mat[] arr1 = phase.Split();
  283. arr[0] = arr1[0];
  284. arr[1] = arr1[1];
  285. arr[2] = arr1[2];
  286. Cv2.Merge(arr, phase);
  287. }
  288. catch (Exception)
  289. {
  290. if (phaseTemp != null && !phaseTemp.IsDisposed) phaseTemp.Dispose();
  291. if (labels != null && !labels.IsDisposed) labels.Dispose();
  292. if (stats != null && !stats.IsDisposed) stats.Dispose();
  293. if (centroids != null && !centroids.IsDisposed) centroids.Dispose();
  294. }
  295. finally
  296. {
  297. }
  298. return phase;
  299. }
  300. private static void ForeachFunctionByteForOvalChoise(Vec4b* value, int* position)
  301. {
  302. int y = position[0];
  303. int x = position[1];
  304. if (BaseTools.isPointInOval(new System.Drawing.Point(x, y),
  305. new System.Drawing.PointF(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2),
  306. rectangle.Width / 2,
  307. rectangle.Height / 2
  308. ))
  309. {
  310. if (value->Item3 > 0)
  311. {
  312. phaseTemp.Set<Vec4b>(y, x, new Vec4b(value->Item0, value->Item1, value->Item2, value->Item3));
  313. }
  314. }
  315. }
  316. //多边形选择
  317. //https://blog.csdn.net/qq_31839479/article/details/53025067
  318. public static Mat SingleChoisePolygon(Mat phase, int color, List<System.Drawing.PointF> points)
  319. {
  320. Color color1 = Color.FromArgb(color);
  321. //提取椭圆的数据
  322. Mat labels = null, stats = null, centroids = null;
  323. try
  324. {
  325. float ax = rectangle.X, ay = rectangle.Y;
  326. //处理矩形数据
  327. if (rectangle.Width <= 0 || rectangle.Height <= 0)
  328. {
  329. System.Drawing.PointF p1 = new System.Drawing.PointF(rectangle.X, rectangle.Y);
  330. System.Drawing.PointF p2 = new System.Drawing.PointF(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height);
  331. if (rectangle.Width <= 0 && rectangle.Height > 0)
  332. {
  333. ax = p2.X;
  334. ay = p1.Y;
  335. }
  336. else if (rectangle.Width > 0 && rectangle.Height <= 0)
  337. {
  338. ax = p1.X;
  339. ay = p2.Y;
  340. }
  341. else if (rectangle.Width <= 0 && rectangle.Height <= 0)
  342. {
  343. ax = p2.X;
  344. ay = p2.Y;
  345. }
  346. }
  347. pointsTemp = points;
  348. vec4B = new Vec4b(255, 0, 0, 255);
  349. Mat[] arr = phase.Split();
  350. phaseTemp = new Mat(phase.Size(), phase.Type());
  351. phase.ForEachAsVec4b(ForeachFunctionByteForPolygonChoise);
  352. labels = new Mat();
  353. stats = new Mat();
  354. centroids = new Mat();
  355. phaseTemp = phaseTemp.CvtColor(ColorConversionCodes.BGRA2GRAY);
  356. phase = phase.CvtColor(ColorConversionCodes.BGRA2BGR);
  357. //获取矩形内的连通分量,图像传矩形内的图片
  358. int num = Cv2.ConnectedComponentsWithStats(phaseTemp, labels, stats, centroids, PixelConnectivity.Connectivity8);
  359. if (centroids != null)
  360. {
  361. for (int y = 1; y < centroids.Height; y++)
  362. {
  363. OpenCvSharp.Point p = new OpenCvSharp.Point();
  364. p.X = Convert.ToInt32(centroids.At<double>(y, 0));// + ax;
  365. p.Y = Convert.ToInt32(centroids.At<double>(y, 1));// + ay;
  366. //Cv2.Circle(phase, p, 2, new Scalar(255,255,255), 3);
  367. if (arr[3].At<byte>(p.Y, p.X) == 255)
  368. {
  369. Rect rect1 = new Rect();
  370. Cv2.FloodFill(phase, p, new Scalar(255- color1.B, 255 - color1.G, 255 - color1.R), out rect1, null, null, FloodFillFlags.Link8);
  371. }
  372. }
  373. }
  374. Mat[] arr1 = phase.Split();
  375. arr[0] = arr1[0];
  376. arr[1] = arr1[1];
  377. arr[2] = arr1[2];
  378. Cv2.Merge(arr, phase);
  379. }
  380. catch (Exception)
  381. {
  382. if (phaseTemp != null && !phaseTemp.IsDisposed) phaseTemp.Dispose();
  383. if (labels != null && !labels.IsDisposed) labels.Dispose();
  384. if (stats != null && !stats.IsDisposed) stats.Dispose();
  385. if (centroids != null && !centroids.IsDisposed) centroids.Dispose();
  386. }
  387. finally
  388. {
  389. }
  390. return phase;
  391. }
  392. private static void ForeachFunctionByteForPolygonChoise(Vec4b* value, int* position)
  393. {
  394. int y = position[0];
  395. int x = position[1];
  396. //lock (obj)
  397. //原来用region判断,但是多线程崩溃,只能加锁,所以改成射线法判断点是否在
  398. //if (region.IsVisible(x, y))
  399. if (BaseTools.isPointInPolygon(new System.Drawing.Point(x, y), pointsTemp))
  400. {
  401. if (value->Item3 > 0)
  402. phaseTemp.Set<Vec4b>(y, x, vec4B);
  403. }
  404. }
  405. #endregion
  406. #region 删除
  407. /// <summary>
  408. /// 单个删除
  409. /// </summary>
  410. /// <returns></returns>
  411. public static Mat SingleDelete(Mat phase, System.Drawing.Point point, int color)
  412. {
  413. try
  414. {
  415. phaseTemp1 = new Mat();
  416. vec4B = new Vec4b(0, 0, 0, 0);
  417. Rect rect = new Rect();
  418. Mat[] arr = phase.Split();
  419. phaseTemp = phase.CvtColor(ColorConversionCodes.BGRA2BGR);
  420. if (color == 0) { colorTemp = 1; }
  421. else if (color == 255) { colorTemp = 254; }
  422. else { colorTemp = color + 1; }
  423. Cv2.FloodFill(phaseTemp, new OpenCvSharp.Point(point.X, point.Y), new Scalar(colorTemp, colorTemp, colorTemp), out rect, null, null, FloodFillFlags.Link8);
  424. Mat[] arr1 = phaseTemp.Split();
  425. arr[0] = arr1[0];
  426. arr[1] = arr1[1];
  427. arr[2] = arr1[2];
  428. Cv2.Merge(arr, phaseTemp);
  429. phaseTemp.CopyTo(phaseTemp1);
  430. phaseTemp.ForEachAsVec4b(ForeachFunctionByteForDelete);
  431. phaseTemp1.CopyTo(phase);
  432. }
  433. catch(Exception)
  434. {
  435. }
  436. finally
  437. {
  438. if (phaseTemp != null && !phaseTemp.IsDisposed) phaseTemp.Dispose();
  439. if (phaseTemp1 != null && !phaseTemp1.IsDisposed) phaseTemp1.Dispose();
  440. }
  441. return phase;
  442. }
  443. /// <summary>
  444. /// 矩形删除
  445. /// </summary>
  446. /// <param name="phase"></param>
  447. /// <param name="rect"></param>
  448. /// <returns></returns>
  449. public static Mat RectangleDelete(Mat phase, System.Drawing.RectangleF rect)
  450. {
  451. try
  452. {
  453. vec4B = new Vec4b(0, 0, 0, 0);
  454. rectangle = rect;
  455. phaseTemp = new Mat();
  456. phase.CopyTo(phaseTemp);
  457. phase.ForEachAsVec4b(ForeachFunctionByteForRectangleDelete);
  458. phaseTemp.CopyTo(phase);
  459. }
  460. catch(Exception)
  461. {
  462. }
  463. finally
  464. {
  465. if (phaseTemp != null && !phaseTemp.IsDisposed) phaseTemp.Dispose();
  466. }
  467. return phase;
  468. }
  469. /// <summary>
  470. /// 多边形删除
  471. /// </summary>
  472. /// <param name="phase"></param>
  473. /// <param name="rect"></param>
  474. /// <returns></returns>
  475. public static Mat PolygonDelete(Mat phase, List<System.Drawing.PointF> points)
  476. {
  477. try
  478. {
  479. pointsTemp = points;
  480. vec4B = new Vec4b(0, 0, 0, 0);
  481. phaseTemp = new Mat();
  482. phase.CopyTo(phaseTemp);
  483. phase.ForEachAsVec4b(ForeachFunctionByteForPolygonDelete);
  484. phaseTemp.CopyTo(phase);
  485. }
  486. catch(Exception)
  487. {
  488. }
  489. finally
  490. {
  491. if (phaseTemp != null && !phaseTemp.IsDisposed) phaseTemp.Dispose();
  492. pointsTemp = null;
  493. }
  494. return phase;
  495. }
  496. /// <summary>
  497. /// 椭圆删除
  498. /// </summary>
  499. /// <param name="phase"></param>
  500. /// <param name="rect"></param>
  501. /// <returns></returns>
  502. public static Mat OvalDelete(Mat phase, System.Drawing.RectangleF rect)
  503. {
  504. try
  505. {
  506. vec4B = new Vec4b(0, 0, 0, 0);
  507. rectangle = rect;
  508. phaseTemp = new Mat();
  509. phase.CopyTo(phaseTemp);
  510. phase.ForEachAsVec4b(ForeachFunctionByteForOvalDelete);
  511. phaseTemp.CopyTo(phase);
  512. }
  513. catch(Exception)
  514. {
  515. }
  516. finally
  517. {
  518. if (phaseTemp != null && !phaseTemp.IsDisposed) phaseTemp.Dispose();
  519. }
  520. return phase;
  521. }
  522. /// <summary>
  523. /// 椭圆删除
  524. /// </summary>
  525. /// <param name="value"></param>
  526. /// <param name="position"></param>
  527. private static void ForeachFunctionByteForOvalDelete(Vec4b* value, int* position)
  528. {
  529. int y = position[0];
  530. int x = position[1];
  531. if (BaseTools.isPointInOval(new System.Drawing.Point(x, y),
  532. new System.Drawing.PointF(rectangle.X + rectangle.Width/2, rectangle.Y + rectangle.Height / 2),
  533. rectangle.Width / 2,
  534. rectangle.Height / 2
  535. ))
  536. {
  537. phaseTemp.Set<Vec4b>(y, x, vec4B);
  538. }
  539. }
  540. /// <summary>
  541. /// 多边形删除
  542. /// </summary>
  543. /// <param name="value"></param>
  544. /// <param name="position"></param>
  545. private static void ForeachFunctionByteForPolygonDelete(Vec4b* value, int* position)
  546. {
  547. int y = position[0];
  548. int x = position[1];
  549. //lock (obj)
  550. //原来用region判断,但是多线程崩溃,只能加锁,所以改成射线法判断点是否在
  551. //if (region.IsVisible(x, y))
  552. if (BaseTools.isPointInPolygon(new System.Drawing.Point(x, y), pointsTemp))
  553. {
  554. phaseTemp.Set<Vec4b>(y, x, vec4B);
  555. }
  556. }
  557. /// <summary>
  558. /// 矩形删除
  559. /// </summary>
  560. /// <param name="value"></param>
  561. /// <param name="position"></param>
  562. private static void ForeachFunctionByteForRectangleDelete(Vec4b* value, int* position)
  563. {
  564. int y = position[0];
  565. int x = position[1];
  566. if(rectangle.Contains(x,y))
  567. {
  568. phaseTemp.Set<Vec4b>(y, x, vec4B);
  569. }
  570. }
  571. /// <summary>
  572. /// 单个删除的循环
  573. /// </summary>
  574. /// <param name="value"></param>
  575. /// <param name="position"></param>
  576. private static void ForeachFunctionByteForDelete(Vec4b* value, int* position)
  577. {
  578. int y = position[0];
  579. int x = position[1];
  580. if (value->Item0 == colorTemp)
  581. {
  582. phaseTemp1.Set<Vec4b>(y, x, vec4B);
  583. }
  584. }
  585. #endregion
  586. #region 分割
  587. /// <summary>
  588. /// 直线分割
  589. /// </summary>
  590. /// <param name="phase">相</param>
  591. /// <param name="point">点</param>
  592. /// <param name="color">颜色</param>
  593. /// <returns></returns>
  594. public static Mat LineSplit(Mat phase, System.Drawing.PointF start, System.Drawing.PointF end, int width)
  595. {
  596. Cv2.Line(phase,
  597. new OpenCvSharp.Point(start.X, start.Y),
  598. new OpenCvSharp.Point(end.X, end.Y), new Scalar(0, 0, 0, 0), width, LineTypes.Link8);
  599. return phase;
  600. }
  601. /// <summary>
  602. /// 椭圆分割
  603. /// </summary>
  604. /// <param name="phase"></param>
  605. public static Mat EllipseSplit(Mat phase, System.Drawing.RectangleF rectangle, int width)
  606. {
  607. Point2f point2F = new Point2f(rectangle.X+ rectangle.Width/2, rectangle.Y + rectangle.Height/2);
  608. Size2f size2F = new Size2f(Math.Abs(rectangle.Width), Math.Abs(rectangle.Height));
  609. Cv2.Ellipse(phase, new RotatedRect(point2F, size2F, 0), new Scalar(0, 0, 0, 0), width, LineTypes.Link8);
  610. return phase;
  611. }
  612. #endregion
  613. #region 连接
  614. /// <summary>
  615. /// 直线连接
  616. /// </summary>
  617. /// <param name="phase"></param>
  618. /// <param name="color"></param>
  619. /// <param name="start"></param>
  620. /// <param name="end"></param>
  621. /// <returns></returns>
  622. public static Mat LineConnection(Mat phase, int color, System.Drawing.PointF start, System.Drawing.PointF end, int width)
  623. {
  624. Color color1 = Color.FromArgb(color);
  625. Cv2.Line(phase,
  626. new OpenCvSharp.Point(start.X, start.Y),
  627. new OpenCvSharp.Point(end.X, end.Y), new Scalar(color1.B, color1.G, color1.R, 255), width, LineTypes.Link8);
  628. return phase;
  629. }
  630. /// <summary>
  631. /// 椭圆连接
  632. /// </summary>
  633. /// <param name="phase"></param>
  634. public static Mat EllipseConnection(Mat phase, int color, System.Drawing.RectangleF rectangle, int width)
  635. {
  636. Color color1 = Color.FromArgb(color);
  637. Point2f point2F = new Point2f(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2);
  638. Size2f size2F = new Size2f(Math.Abs(rectangle.Width), Math.Abs(rectangle.Height));
  639. Cv2.Ellipse(phase, new RotatedRect(point2F, size2F, 0), new Scalar(color1.B, color1.G, color1.R, 255), width, LineTypes.Link8);
  640. return phase;
  641. }
  642. #endregion
  643. #region 添加
  644. /// <summary>
  645. /// 椭圆添加
  646. /// </summary>
  647. /// <param name="phase"></param>
  648. public static Mat EllipseAdd(Mat phase, int color, System.Drawing.RectangleF rectangle)
  649. {
  650. Color color1 = Color.FromArgb(color);
  651. Point2f point2F = new Point2f(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2);
  652. Size2f size2F = new Size2f(Math.Abs(rectangle.Width), Math.Abs(rectangle.Height));
  653. Cv2.Ellipse(phase, new RotatedRect(point2F, size2F, 0), new Scalar(color1.B, color1.G, color1.R, 255), -1, LineTypes.Link8);
  654. return phase;
  655. }
  656. /// <summary>
  657. /// 矩形添加
  658. /// </summary>
  659. /// <param name="phase"></param>
  660. /// <param name="color"></param>
  661. /// <param name="rectangle"></param>
  662. /// <returns></returns>
  663. public static Mat RectangleAdd(Mat phase, int color, System.Drawing.RectangleF rectangle)
  664. {
  665. Color color1 = Color.FromArgb(color);
  666. 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);
  667. return phase;
  668. }
  669. /// <summary>
  670. /// 多边形添加
  671. /// </summary>
  672. /// <param name="phase">相</param>
  673. /// <param name="color">相颜色</param>
  674. /// <param name="points">多边形顶点集合</param>
  675. /// <returns></returns>
  676. public static Mat PolygonAdd(Mat phase, int color, List<System.Drawing.PointF> points)
  677. {
  678. Color color1 = Color.FromArgb(color);
  679. OpenCvSharp.Point[][] arr = new OpenCvSharp.Point[1][];
  680. arr[0] = BaseTools.pointToPoint(points).ToArray();
  681. Cv2.FillPoly(phase, arr, new Scalar(color1.B, color1.G, color1.R, 255), LineTypes.Link8);
  682. return phase;
  683. }
  684. #endregion
  685. }
  686. }