| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- 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<OpenCvSharp.Point[][]> points;//各个视场石墨轮廓集合
- List<List<int>> lstAttribute;//各个视场属性集合,索引0-横坐标,1-纵坐标,2-视场宽度,3-视场高度,4-视场面积
- List<int> attribute;//单视场属性集合
- List<HierarchyIndex[]> hierarchyIndices;
- /// <summary>
- /// 获取视场相关信息
- /// </summary>
- /// <param name="appWorkspace">主控件</param>
- /// <param name="binarizationMat">相</param>
- /// <param name="listView1">图片集合</param>
- /// <param name="documentWorkspace">画布</param>
- /// <param name="points">石墨轮廓集合</param>
- /// <param name="lstAttribute">各个视场属性集合,索引0-横坐标,1-纵坐标,2-视场宽度,3-视场高度,4-视场面积</param>
- /// <param name="sumFieldl">视场个数</param>
- public void MultiFieldView(AppWorkspace appWorkspace,Mat binarizationMat, ListView listView1, DocumentWorkspaceWindow documentWorkspace,out List<OpenCvSharp.Point[][]> points,out List<List<int>> lstAttribute,out int sumFieldl)
- {
- points = new List<OpenCvSharp.Point[][]>();
- lstAttribute = new List<List<int>>();
- hierarchyIndices = new List<HierarchyIndex[]>();
- //寻找并标记区域个数
- 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>();
- int x = stats.At<int>(h, 0);
- int y = stats.At<int>(h, 1);
- int width = stats.At<int>(h, 2);
- int height = stats.At<int>(h, 3);
- int areaField1 = stats.At<int>(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<int>(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);
- }
- }
- }
- /// <summary>
- /// 获取视场相关信息
- /// </summary>
- /// <param name="appWorkspace">主控件</param>
- /// <param name="binarizationMat">相</param>
- /// <param name="listView1">图片集合</param>
- /// <param name="documentWorkspace">画布</param>
- /// <param name="points">石墨轮廓集合</param>
- /// <param name="lstAttribute">各个视场属性集合,索引0-横坐标,1-纵坐标,2-视场宽度,3-视场高度,4-视场面积</param>
- /// <param name="sumFieldl">视场个数</param>
- 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)
- {
- points = new List<OpenCvSharp.Point[][]>();
- lstAttribute = new List<List<int>>();
- hierarchyIndices = new List<HierarchyIndex[]>();
- //寻找并标记区域个数
- 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>();
- int x = stats.At<int>(h, 0);
- int y = stats.At<int>(h, 1);
- int width = stats.At<int>(h, 2);
- int height = stats.At<int>(h, 3);
- int areaField1 = stats.At<int>(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<int>(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);
- }
- }
- }
- /// <summary>
- /// 结果数据保存
- /// </summary>
- /// <param name="dataGridView1"></param>
- /// <returns>DataTable</returns>
- 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;
- }
- /// <summary>
- /// 分析结果数据保存
- /// </summary>
- /// <param name="dataGridView1"></param>
- /// <param name="imgName">选中图片名称</param>
- /// <param name="tag">选中图片tag</param>
- /// <returns>DataTable</returns>
- 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;
- }
- }
- /// <summary>
- /// 判断当前系统是否存在office
- /// </summary>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 获取石墨面积
- /// </summary>
- /// <param name="tempCopy">相</param>
- /// <param name="contours">轮廓</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 需要显示不同颜色的相颜色赋值
- /// </summary>
- /// <param name="mat">需要处理的相</param>
- /// <param name="lstPoint">需要处理的轮廓集合</param>
- /// <param name="colour">颜色字典</param>
- /// <param name="key">对应颜色的key</param>
- public void DifferentColor(Mat mat,List<List<OpenCvSharp.Point>> lstPoint, Dictionary<string, Color> 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);
- }
- }
- /// <summary>
- /// 需要显示不同颜色的相颜色赋值
- /// </summary>
- /// <param name="mat">需要处理的相</param>
- /// <param name="lstPoint">需要处理的轮廓集合</param>
- /// <param name="panel2">颜色</param>
- public void DifferentColor(Mat mat, List<OpenCvSharp.Point[]> 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);
- }
- }
- /// <summary>
- /// 需要显示不同颜色的相颜色赋值
- /// </summary>
- /// <param name="mat">需要处理的相</param>
- /// <param name="contours">石墨轮廓</param>
- /// <param name="olor">颜色</param>
- 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);
- }
- }
- }
|