using Microsoft.Win32; using OpenCvSharp; using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Windows.Forms; namespace PaintDotNet.DedicatedAnalysis.DuctileIron.Common { internal partial class AppCommon { List points;//各个视场石墨轮廓集合 List> lstAttribute;//各个视场属性集合,索引0-横坐标,1-纵坐标,2-视场宽度,3-视场高度,4-视场面积 List attribute;//单视场属性集合 List hierarchyIndices; /// /// 获取视场相关信息 /// /// 主控件 /// 相 /// 图片集合 /// 画布 /// 石墨轮廓集合 /// 各个视场属性集合,索引0-横坐标,1-纵坐标,2-视场宽度,3-视场高度,4-视场面积 /// 视场个数 public void MultiFieldView(AppWorkspace appWorkspace,Mat binarizationMat, ListView listView1, DocumentWorkspaceWindow documentWorkspace,out List points,out List> lstAttribute,out int sumFieldl) { points = new List(); lstAttribute = new List>(); hierarchyIndices = new List(); //寻找并标记区域个数 Mat labelMat = new Mat(); Mat stats = new Mat(); Mat centroids = new Mat(); Bitmap bitmap = appWorkspace.DocumentWorkspaces[listView1.FocusedItem.Index].GetCutSizeWithColorWhiteOrTransparentColor(false); Mat oldMap = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap); int nonenum = Cv2.ConnectedComponentsWithStats(BinaryClass.BGRA2GRAY(oldMap)/*.CvtColor(ColorConversionCodes.BGR2GRAY)*/, labelMat, stats, centroids, PixelConnectivity.Connectivity8); //视场个数 sumFieldl = centroids.Height - 1; if (documentWorkspace.GraphicsList.IsExsitView()) { for (int h = 1; h < centroids.Height; h++) { attribute = new List(); int x = stats.At(h, 0); int y = stats.At(h, 1); int width = stats.At(h, 2); int height = stats.At(h, 3); int areaField1 = stats.At(h, 4); attribute.Add(x); attribute.Add(y); attribute.Add(width); attribute.Add(height); attribute.Add(areaField1); lstAttribute.Add(attribute); //double areaField = areaField1 * unitLength * unitLength; if (x < 0 || y < 0 || width > bitmap.Width || height > bitmap.Height) { continue; } Rect roi1 = new Rect(); Mat ImageROI1 = new Mat(); try { roi1 = new Rect(x, y, width, height); ImageROI1 = new Mat(binarizationMat, roi1).Clone(); for (int label_y = 0; label_y < height; label_y++) { for (int label_x = 0; label_x < width; label_x++) { if (labelMat.At(y + label_y, x + label_x) != h) { ImageROI1.Set(label_y, label_x, new Vec4b(0, 0, 0, 0)); } } } } catch (Exception) { ImageROI1 = oldMap; } OpenCvSharp.Point[][] contours = null; HierarchyIndex[] hierarchy = null; Mat mat = Mat.FromImageData(ImageROI1.ToBytes(), ImreadModes.Grayscale); Cv2.FindContours(mat, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxNone); points.Add(contours); hierarchyIndices.Add(hierarchy); } } } /// /// 获取视场相关信息 /// /// 主控件 /// 相 /// 图片集合 /// 画布 /// 石墨轮廓集合 /// 各个视场属性集合,索引0-横坐标,1-纵坐标,2-视场宽度,3-视场高度,4-视场面积 /// 视场个数 public void MultiFieldView(AppWorkspace appWorkspace, Mat binarizationMat, ListView listView1, DocumentWorkspaceWindow documentWorkspace, out List points, out List> lstAttribute, out int sumFieldl, out List hierarchyIndices) { points = new List(); lstAttribute = new List>(); hierarchyIndices = new List(); //寻找并标记区域个数 Mat labelMat = new Mat(); Mat stats = new Mat(); Mat centroids = new Mat(); Bitmap bitmap = appWorkspace.DocumentWorkspaces[listView1.FocusedItem.Index].GetCutSizeWithColorWhiteOrTransparentColor(false); Mat oldMap = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap); int nonenum = Cv2.ConnectedComponentsWithStats(BinaryClass.BGRA2GRAY(oldMap)/*.CvtColor(ColorConversionCodes.BGR2GRAY)*/, labelMat, stats, centroids, PixelConnectivity.Connectivity8); //视场个数 sumFieldl = centroids.Height - 1; if (documentWorkspace.GraphicsList.IsExsitView()) { for (int h = 1; h < centroids.Height; h++) { attribute = new List(); int x = stats.At(h, 0); int y = stats.At(h, 1); int width = stats.At(h, 2); int height = stats.At(h, 3); int areaField1 = stats.At(h, 4); attribute.Add(x); attribute.Add(y); attribute.Add(width); attribute.Add(height); attribute.Add(areaField1); lstAttribute.Add(attribute); //double areaField = areaField1 * unitLength * unitLength; if (x < 0 || y < 0 || width > bitmap.Width || height > bitmap.Height) { continue; } Rect roi1 = new Rect(); Mat ImageROI1 = new Mat(); try { roi1 = new Rect(x, y, width, height); ImageROI1 = new Mat(binarizationMat, roi1).Clone(); for (int label_y = 0; label_y < height; label_y++) { for (int label_x = 0; label_x < width; label_x++) { if (labelMat.At(y + label_y, x + label_x) != h) { ImageROI1.Set(label_y, label_x, new Vec4b(0, 0, 0, 0)); } } } } catch (Exception) { ImageROI1 = oldMap; } OpenCvSharp.Point[][] contours = null; HierarchyIndex[] hierarchy = null; Mat mat = Mat.FromImageData(ImageROI1.ToBytes(), ImreadModes.Grayscale); Cv2.FindContours(mat, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxNone); points.Add(contours); hierarchyIndices.Add(hierarchy); } } } /// /// 结果数据保存 /// /// /// DataTable public DataTable ResultDataSaving(DataGridView dataGridView1) { DataTable dtDataGridView1 = new DataTable(); DataColumn dc = null; for (int count = 0; count < dataGridView1.Columns.Count; count++) { dc = new DataColumn(dataGridView1.Columns[count].Name.ToString()); dtDataGridView1.Columns.Add(dc); } for (int count = 0; count < dataGridView1.Rows.Count; count++) { DataRow dr = dtDataGridView1.NewRow(); for (int countsub = 0; countsub < dataGridView1.Columns.Count; countsub++) { dr[countsub] = Convert.ToString(dataGridView1.Rows[count].Cells[countsub].Value); } dtDataGridView1.Rows.Add(dr); } return dtDataGridView1; } /// /// 分析结果数据保存 /// /// /// 选中图片名称 /// 选中图片tag /// DataTable public DataTable AnalysisDataSaving(DataGridView dataGridView1, string imgName, string tag) { DataTable dtResult = new DataTable(tag); DataColumn dc2 = null; for (int count = 0; count <= dataGridView1.Columns.Count; count++) { if (count == 0) { dc2 = new DataColumn(PdnResources.GetString("Menu.picture.Text")); } else { dc2 = new DataColumn(dataGridView1.Columns[count - 1].Name.ToString()); } dtResult.Columns.Add(dc2); } for (int count = 0; count < dataGridView1.Rows.Count; count++) { DataRow dr = dtResult.NewRow(); for (int countsub = 0; countsub <= dataGridView1.Columns.Count; countsub++) { if (countsub == 0) { dr[countsub] = imgName; } else { dr[countsub] = Convert.ToString(dataGridView1.Rows[count].Cells[countsub - 1].Value); } } dtResult.Rows.Add(dr); } return dtResult; } public void DisplayData(DataGridView dataGridView,bool whether) { for (int i = 0; i < dataGridView.Rows.Count; i++) { dataGridView.Rows[i].Visible = whether; } } /// /// 判断当前系统是否存在office /// /// public bool CheckOffice() { bool ifused = false; int officeVersion = 0; RegistryKey rk = Registry.LocalMachine; RegistryKey akey07 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\12.0\Common\InstallRoot\");//查询2007 RegistryKey akey10 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\14.0\Common\InstallRoot\");//查询2010 RegistryKey akey13 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\15.0\Common\InstallRoot\");//查询2013 RegistryKey akey16 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\16.0\Common\InstallRoot\");//查询2016 //检查本机是否安装Office2007 if (akey07 != null) { string office07 = akey07.GetValue("Path").ToString(); if (System.IO.File.Exists(office07 + "Excel.exe")) { ifused = true; officeVersion = 2007; } } //检查本机是否安装Office2010 if (akey10 != null) { string office10 = akey10.GetValue("Path").ToString(); if (System.IO.File.Exists(office10 + "Excel.exe")) { ifused = true; officeVersion = 2010; } } //检查本机是否安装Office2013 if (akey13 != null) { string office13 = akey13.GetValue("Path").ToString(); if (System.IO.File.Exists(office13 + "Excel.exe")) { ifused = true; officeVersion = 2013; } } //检查本机是否安装Office2016 if (akey16 != null) { string office16 = akey16.GetValue("Path").ToString(); if (System.IO.File.Exists(office16 + "Excel.exe")) { ifused = true; officeVersion = 2016; } } return ifused; } public bool isExcelInstalled() { try { Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); return true; } catch (Exception) { return false; } //Type type = Type.GetTypeFromProgID("Excel.Application"); //return type != null; } /// /// 获取石墨面积 /// /// 相 /// 轮廓 /// public int Area(Mat tempCopy ,OpenCvSharp.Point contours) { Rect rectCopy; int length = Cv2.FloodFill(tempCopy, contours, new Scalar(0, 0, 0, 0), out rectCopy, null, null, FloodFillFlags.Link8); return length; } /// /// 需要显示不同颜色的相颜色赋值 /// /// 需要处理的相 /// 需要处理的轮廓集合 /// 颜色字典 /// 对应颜色的key public void DifferentColor(Mat mat,List> lstPoint, Dictionary colour,string key) { Rect rectCopy; for (int i = 0; i < lstPoint.Count; i++) { 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); } } /// /// 需要显示不同颜色的相颜色赋值 /// /// 需要处理的相 /// 需要处理的轮廓集合 /// 颜色 public void DifferentColor(Mat mat, List lstPoint, Color olor) { Rect rectCopy; for (int i = 0; i < lstPoint.Count; i++) { int length1 = Cv2.FloodFill(mat, lstPoint[i][0], new Scalar(olor.B, olor.G, olor.R), out rectCopy, null, null, FloodFillFlags.Link8); } } /// /// 需要显示不同颜色的相颜色赋值 /// /// 需要处理的相 /// 石墨轮廓 /// 颜色 public void DifferentColor(Mat mat, OpenCvSharp.Point contours, Color olor) { Rect rectCopy; int length1 = Cv2.FloodFill(mat, contours, new Scalar(olor.B, olor.G, olor.R), out rectCopy, null, null, FloodFillFlags.Link8); } } }