AppCommon.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. using Microsoft.Win32;
  2. using OpenCvSharp;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8. namespace PaintDotNet.DedicatedAnalysis.DuctileIron.Common
  9. {
  10. internal partial class AppCommon
  11. {
  12. List<OpenCvSharp.Point[][]> points;//各个视场石墨轮廓集合
  13. List<List<int>> lstAttribute;//各个视场属性集合,索引0-横坐标,1-纵坐标,2-视场宽度,3-视场高度,4-视场面积
  14. List<int> attribute;//单视场属性集合
  15. List<HierarchyIndex[]> hierarchyIndices;
  16. /// <summary>
  17. /// 获取视场相关信息
  18. /// </summary>
  19. /// <param name="appWorkspace">主控件</param>
  20. /// <param name="binarizationMat">相</param>
  21. /// <param name="listView1">图片集合</param>
  22. /// <param name="documentWorkspace">画布</param>
  23. /// <param name="points">石墨轮廓集合</param>
  24. /// <param name="lstAttribute">各个视场属性集合,索引0-横坐标,1-纵坐标,2-视场宽度,3-视场高度,4-视场面积</param>
  25. /// <param name="sumFieldl">视场个数</param>
  26. public void MultiFieldView(AppWorkspace appWorkspace,Mat binarizationMat, ListView listView1, DocumentWorkspaceWindow documentWorkspace,out List<OpenCvSharp.Point[][]> points,out List<List<int>> lstAttribute,out int sumFieldl)
  27. {
  28. points = new List<OpenCvSharp.Point[][]>();
  29. lstAttribute = new List<List<int>>();
  30. hierarchyIndices = new List<HierarchyIndex[]>();
  31. //寻找并标记区域个数
  32. Mat labelMat = new Mat();
  33. Mat stats = new Mat();
  34. Mat centroids = new Mat();
  35. Bitmap bitmap = appWorkspace.DocumentWorkspaces[listView1.FocusedItem.Index].GetCutSizeWithColorWhiteOrTransparentColor(false);
  36. Mat oldMap = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);
  37. int nonenum = Cv2.ConnectedComponentsWithStats(BinaryClass.BGRA2GRAY(oldMap)/*.CvtColor(ColorConversionCodes.BGR2GRAY)*/, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  38. //视场个数
  39. sumFieldl = centroids.Height - 1;
  40. if (documentWorkspace.GraphicsList.IsExsitView())
  41. {
  42. for (int h = 1; h < centroids.Height; h++)
  43. {
  44. attribute = new List<int>();
  45. int x = stats.At<int>(h, 0);
  46. int y = stats.At<int>(h, 1);
  47. int width = stats.At<int>(h, 2);
  48. int height = stats.At<int>(h, 3);
  49. int areaField1 = stats.At<int>(h, 4);
  50. attribute.Add(x);
  51. attribute.Add(y);
  52. attribute.Add(width);
  53. attribute.Add(height);
  54. attribute.Add(areaField1);
  55. lstAttribute.Add(attribute);
  56. //double areaField = areaField1 * unitLength * unitLength;
  57. if (x < 0 || y < 0 || width > bitmap.Width || height > bitmap.Height)
  58. {
  59. continue;
  60. }
  61. Rect roi1 = new Rect();
  62. Mat ImageROI1 = new Mat();
  63. try
  64. {
  65. roi1 = new Rect(x, y, width, height);
  66. ImageROI1 = new Mat(binarizationMat, roi1).Clone();
  67. for (int label_y = 0; label_y < height; label_y++)
  68. {
  69. for (int label_x = 0; label_x < width; label_x++)
  70. {
  71. if (labelMat.At<int>(y + label_y, x + label_x) != h)
  72. {
  73. ImageROI1.Set(label_y, label_x, new Vec4b(0, 0, 0, 0));
  74. }
  75. }
  76. }
  77. }
  78. catch (Exception)
  79. {
  80. ImageROI1 = oldMap;
  81. }
  82. OpenCvSharp.Point[][] contours = null;
  83. HierarchyIndex[] hierarchy = null;
  84. Mat mat = Mat.FromImageData(ImageROI1.ToBytes(), ImreadModes.Grayscale);
  85. Cv2.FindContours(mat, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  86. points.Add(contours);
  87. hierarchyIndices.Add(hierarchy);
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// 获取视场相关信息
  93. /// </summary>
  94. /// <param name="appWorkspace">主控件</param>
  95. /// <param name="binarizationMat">相</param>
  96. /// <param name="listView1">图片集合</param>
  97. /// <param name="documentWorkspace">画布</param>
  98. /// <param name="points">石墨轮廓集合</param>
  99. /// <param name="lstAttribute">各个视场属性集合,索引0-横坐标,1-纵坐标,2-视场宽度,3-视场高度,4-视场面积</param>
  100. /// <param name="sumFieldl">视场个数</param>
  101. public void MultiFieldView(AppWorkspace appWorkspace, Mat binarizationMat, ListView listView1, DocumentWorkspaceWindow documentWorkspace, out List<OpenCvSharp.Point[][]> points, out List<List<int>> lstAttribute, out int sumFieldl, out List<HierarchyIndex[]> hierarchyIndices)
  102. {
  103. points = new List<OpenCvSharp.Point[][]>();
  104. lstAttribute = new List<List<int>>();
  105. hierarchyIndices = new List<HierarchyIndex[]>();
  106. //寻找并标记区域个数
  107. Mat labelMat = new Mat();
  108. Mat stats = new Mat();
  109. Mat centroids = new Mat();
  110. Bitmap bitmap = appWorkspace.DocumentWorkspaces[listView1.FocusedItem.Index].GetCutSizeWithColorWhiteOrTransparentColor(false);
  111. Mat oldMap = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);
  112. int nonenum = Cv2.ConnectedComponentsWithStats(BinaryClass.BGRA2GRAY(oldMap)/*.CvtColor(ColorConversionCodes.BGR2GRAY)*/, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  113. //视场个数
  114. sumFieldl = centroids.Height - 1;
  115. if (documentWorkspace.GraphicsList.IsExsitView())
  116. {
  117. for (int h = 1; h < centroids.Height; h++)
  118. {
  119. attribute = new List<int>();
  120. int x = stats.At<int>(h, 0);
  121. int y = stats.At<int>(h, 1);
  122. int width = stats.At<int>(h, 2);
  123. int height = stats.At<int>(h, 3);
  124. int areaField1 = stats.At<int>(h, 4);
  125. attribute.Add(x);
  126. attribute.Add(y);
  127. attribute.Add(width);
  128. attribute.Add(height);
  129. attribute.Add(areaField1);
  130. lstAttribute.Add(attribute);
  131. //double areaField = areaField1 * unitLength * unitLength;
  132. if (x < 0 || y < 0 || width > bitmap.Width || height > bitmap.Height)
  133. {
  134. continue;
  135. }
  136. Rect roi1 = new Rect();
  137. Mat ImageROI1 = new Mat();
  138. try
  139. {
  140. roi1 = new Rect(x, y, width, height);
  141. ImageROI1 = new Mat(binarizationMat, roi1).Clone();
  142. for (int label_y = 0; label_y < height; label_y++)
  143. {
  144. for (int label_x = 0; label_x < width; label_x++)
  145. {
  146. if (labelMat.At<int>(y + label_y, x + label_x) != h)
  147. {
  148. ImageROI1.Set(label_y, label_x, new Vec4b(0, 0, 0, 0));
  149. }
  150. }
  151. }
  152. }
  153. catch (Exception)
  154. {
  155. ImageROI1 = oldMap;
  156. }
  157. OpenCvSharp.Point[][] contours = null;
  158. HierarchyIndex[] hierarchy = null;
  159. Mat mat = Mat.FromImageData(ImageROI1.ToBytes(), ImreadModes.Grayscale);
  160. Cv2.FindContours(mat, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  161. points.Add(contours);
  162. hierarchyIndices.Add(hierarchy);
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// 结果数据保存
  168. /// </summary>
  169. /// <param name="dataGridView1"></param>
  170. /// <returns>DataTable</returns>
  171. public DataTable ResultDataSaving(DataGridView dataGridView1)
  172. {
  173. DataTable dtDataGridView1 = new DataTable();
  174. DataColumn dc = null;
  175. for (int count = 0; count < dataGridView1.Columns.Count; count++)
  176. {
  177. dc = new DataColumn(dataGridView1.Columns[count].Name.ToString());
  178. dtDataGridView1.Columns.Add(dc);
  179. }
  180. for (int count = 0; count < dataGridView1.Rows.Count; count++)
  181. {
  182. DataRow dr = dtDataGridView1.NewRow();
  183. for (int countsub = 0; countsub < dataGridView1.Columns.Count; countsub++)
  184. {
  185. dr[countsub] = Convert.ToString(dataGridView1.Rows[count].Cells[countsub].Value);
  186. }
  187. dtDataGridView1.Rows.Add(dr);
  188. }
  189. return dtDataGridView1;
  190. }
  191. /// <summary>
  192. /// 分析结果数据保存
  193. /// </summary>
  194. /// <param name="dataGridView1"></param>
  195. /// <param name="imgName">选中图片名称</param>
  196. /// <param name="tag">选中图片tag</param>
  197. /// <returns>DataTable</returns>
  198. public DataTable AnalysisDataSaving(DataGridView dataGridView1, string imgName, string tag)
  199. {
  200. DataTable dtResult = new DataTable(tag);
  201. DataColumn dc2 = null;
  202. for (int count = 0; count <= dataGridView1.Columns.Count; count++)
  203. {
  204. if (count == 0)
  205. {
  206. dc2 = new DataColumn(PdnResources.GetString("Menu.picture.Text"));
  207. }
  208. else
  209. {
  210. dc2 = new DataColumn(dataGridView1.Columns[count - 1].Name.ToString());
  211. }
  212. dtResult.Columns.Add(dc2);
  213. }
  214. for (int count = 0; count < dataGridView1.Rows.Count; count++)
  215. {
  216. DataRow dr = dtResult.NewRow();
  217. for (int countsub = 0; countsub <= dataGridView1.Columns.Count; countsub++)
  218. {
  219. if (countsub == 0)
  220. {
  221. dr[countsub] = imgName;
  222. }
  223. else
  224. {
  225. dr[countsub] = Convert.ToString(dataGridView1.Rows[count].Cells[countsub - 1].Value);
  226. }
  227. }
  228. dtResult.Rows.Add(dr);
  229. }
  230. return dtResult;
  231. }
  232. public void DisplayData(DataGridView dataGridView,bool whether)
  233. {
  234. for (int i = 0; i < dataGridView.Rows.Count; i++)
  235. {
  236. dataGridView.Rows[i].Visible = whether;
  237. }
  238. }
  239. /// <summary>
  240. /// 判断当前系统是否存在office
  241. /// </summary>
  242. /// <returns></returns>
  243. public bool CheckOffice()
  244. {
  245. bool ifused = false;
  246. int officeVersion = 0;
  247. RegistryKey rk = Registry.LocalMachine;
  248. RegistryKey akey07 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\12.0\Common\InstallRoot\");//查询2007
  249. RegistryKey akey10 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\14.0\Common\InstallRoot\");//查询2010
  250. RegistryKey akey13 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\15.0\Common\InstallRoot\");//查询2013
  251. RegistryKey akey16 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\16.0\Common\InstallRoot\");//查询2016
  252. //检查本机是否安装Office2007
  253. if (akey07 != null)
  254. {
  255. string office07 = akey07.GetValue("Path").ToString();
  256. if (System.IO.File.Exists(office07 + "Excel.exe"))
  257. {
  258. ifused = true;
  259. officeVersion = 2007;
  260. }
  261. }
  262. //检查本机是否安装Office2010
  263. if (akey10 != null)
  264. {
  265. string office10 = akey10.GetValue("Path").ToString();
  266. if (System.IO.File.Exists(office10 + "Excel.exe"))
  267. {
  268. ifused = true;
  269. officeVersion = 2010;
  270. }
  271. }
  272. //检查本机是否安装Office2013
  273. if (akey13 != null)
  274. {
  275. string office13 = akey13.GetValue("Path").ToString();
  276. if (System.IO.File.Exists(office13 + "Excel.exe"))
  277. {
  278. ifused = true;
  279. officeVersion = 2013;
  280. }
  281. }
  282. //检查本机是否安装Office2016
  283. if (akey16 != null)
  284. {
  285. string office16 = akey16.GetValue("Path").ToString();
  286. if (System.IO.File.Exists(office16 + "Excel.exe"))
  287. {
  288. ifused = true;
  289. officeVersion = 2016;
  290. }
  291. }
  292. return ifused;
  293. }
  294. public bool isExcelInstalled()
  295. {
  296. try
  297. {
  298. Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
  299. return true;
  300. }
  301. catch (Exception)
  302. {
  303. return false;
  304. }
  305. //Type type = Type.GetTypeFromProgID("Excel.Application");
  306. //return type != null;
  307. }
  308. /// <summary>
  309. /// 获取石墨面积
  310. /// </summary>
  311. /// <param name="tempCopy">相</param>
  312. /// <param name="contours">轮廓</param>
  313. /// <returns></returns>
  314. public int Area(Mat tempCopy ,OpenCvSharp.Point contours)
  315. {
  316. Rect rectCopy;
  317. int length = Cv2.FloodFill(tempCopy, contours, new Scalar(0, 0, 0, 0), out rectCopy, null, null, FloodFillFlags.Link8);
  318. return length;
  319. }
  320. /// <summary>
  321. /// 需要显示不同颜色的相颜色赋值
  322. /// </summary>
  323. /// <param name="mat">需要处理的相</param>
  324. /// <param name="lstPoint">需要处理的轮廓集合</param>
  325. /// <param name="colour">颜色字典</param>
  326. /// <param name="key">对应颜色的key</param>
  327. public void DifferentColor(Mat mat,List<List<OpenCvSharp.Point>> lstPoint, Dictionary<string, Color> colour,string key)
  328. {
  329. Rect rectCopy;
  330. for (int i = 0; i < lstPoint.Count; i++)
  331. {
  332. int length1 = Cv2.FloodFill(mat, lstPoint[i][lstPoint[i].Count - 1], new Scalar(colour[key].B, colour[key].G, colour[key].R), out rectCopy, null, null, FloodFillFlags.Link8);
  333. }
  334. }
  335. /// <summary>
  336. /// 需要显示不同颜色的相颜色赋值
  337. /// </summary>
  338. /// <param name="mat">需要处理的相</param>
  339. /// <param name="lstPoint">需要处理的轮廓集合</param>
  340. /// <param name="panel2">颜色</param>
  341. public void DifferentColor(Mat mat, List<OpenCvSharp.Point[]> lstPoint, Color olor)
  342. {
  343. Rect rectCopy;
  344. for (int i = 0; i < lstPoint.Count; i++)
  345. {
  346. int length1 = Cv2.FloodFill(mat, lstPoint[i][0], new Scalar(olor.B, olor.G, olor.R), out rectCopy, null, null, FloodFillFlags.Link8);
  347. }
  348. }
  349. /// <summary>
  350. /// 需要显示不同颜色的相颜色赋值
  351. /// </summary>
  352. /// <param name="mat">需要处理的相</param>
  353. /// <param name="contours">石墨轮廓</param>
  354. /// <param name="olor">颜色</param>
  355. public void DifferentColor(Mat mat, OpenCvSharp.Point contours, Color olor)
  356. {
  357. Rect rectCopy;
  358. int length1 = Cv2.FloodFill(mat, contours, new Scalar(olor.B, olor.G, olor.R), out rectCopy, null, null, FloodFillFlags.Link8);
  359. }
  360. }
  361. }