浏览代码

1.修复不能取消颗粒分离的bug
2.修复颗粒分离后颗粒像素的问题

zty 3 年之前
父节点
当前提交
3dd9d38cf7

+ 1 - 1
OTSIncAReportApp/1-UI/Control_Graph/Controls/Control_DrawDistrbutionImageAndBSE.cs

@@ -1191,7 +1191,7 @@ namespace OTSIncAReportGraph.Controls
                 else if(m_ParticleSegmentation.IsParticleSegmentation)
                 else if(m_ParticleSegmentation.IsParticleSegmentation)
                 {
                 {
                     m_ParticleSegmentation.EndPoint = e.Location;
                     m_ParticleSegmentation.EndPoint = e.Location;
-                    DialogResult res = MessageBox.Show(table["str11"].ToString());
+                    DialogResult res = MessageBox.Show(table["str11"].ToString(), "", MessageBoxButtons.OKCancel);
                     if (res == DialogResult.OK)
                     if (res == DialogResult.OK)
                     {
                     {
                         m_ReportFun.m_ParticleSegmentation = m_ParticleSegmentation;
                         m_ReportFun.m_ParticleSegmentation = m_ParticleSegmentation;

+ 112 - 99
OTSIncAReportApp/1-UI/Control_Graph/OTSIncAReportGraphFuncation/OTSImageDisHelp.cs

@@ -697,10 +697,18 @@ namespace OTSIncAReportGraph.OTSIncAReportGraphFuncation
                 return false;
                 return false;
             }
             }
         }
         }
+
+        /// <summary>
+        /// 获取分离颗粒方法
+        /// </summary>
+        /// <param name="startPoint"></param>
+        /// <param name="endPoint"></param>
+        /// <returns></returns>
         public bool GetSplitPartFun(Point startPoint, Point endPoint)
         public bool GetSplitPartFun(Point startPoint, Point endPoint)
         {
         {
             try
             try
             {
             {
+                CImageHandler m_ImagePro = new CImageHandler();
                 Particle particle1 = (Particle)CloneObject(m_ParticleSegmentation.ParticleData);
                 Particle particle1 = (Particle)CloneObject(m_ParticleSegmentation.ParticleData);
                 Particle particle2 = (Particle)CloneObject(m_ParticleSegmentation.ParticleData);
                 Particle particle2 = (Particle)CloneObject(m_ParticleSegmentation.ParticleData);
                 Dictionary<string, object> sampleMembers = ((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["Sample"])["Members"]);
                 Dictionary<string, object> sampleMembers = ((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["Sample"])["Members"]);
@@ -710,111 +718,75 @@ namespace OTSIncAReportGraph.OTSIncAReportGraphFuncation
                 int width = int.Parse(ImageResolution.Split('_')[1]);
                 int width = int.Parse(ImageResolution.Split('_')[1]);
                 int height = int.Parse(ImageResolution.Split('_')[2]);
                 int height = int.Parse(ImageResolution.Split('_')[2]);
                 double dPixelSize = d_scanFieldSize_width / width;
                 double dPixelSize = d_scanFieldSize_width / width;
-                Mat mat = new Mat(height, width, MatType.CV_8UC1, Scalar.Black);//黑色底图
-                Mat matBse = new Mat(resultFile.List_OTSField[particle1.FieldId].FieldImage, ImreadModes.Grayscale);//原图
-                foreach (Segment segment in m_ParticleSegmentation.ParticleData.SegmentList)
-                {
-                    Cv2.Line(mat, new OpenCvSharp.Point(segment.Start, segment.Height), new OpenCvSharp.Point(segment.Start + segment.Length, segment.Height), Scalar.White, 1, LineTypes.AntiAlias);
-                }
-                Cv2.Line(mat, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y, Scalar.Black, 2, LineTypes.AntiAlias);
-                Mat labelMat = new Mat();
-                Mat stats = new Mat();//点的信息
-                Mat centroids = new Mat();//质心的信息
-                int nonenum = Cv2.ConnectedComponentsWithStats(mat, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
-                //暂时只分割成两个颗粒
-                if (nonenum != 3)
-                {
-                    log.Trace("(GetSplitPartFun) More than two particle");
-                    return false;
-                }
-                //寻找坐标点
-                List<Point> points1 = new List<Point>();
-                List<Point> points2 = new List<Point>();
-                List<int> aveGray1 = new List<int>();
-                List<int> aveGray2 = new List<int>();
-                for (int k = 0; k < labelMat.Height; k++)
+
+                using (Mat mat = new Mat(height, width, MatType.CV_8UC1, Scalar.Black))//黑色底图
+                using (Mat labelMat = new Mat())
+                using (Mat stats = new Mat())
+                using (Mat centroids = new Mat())
+                using (Mat matBse = new Mat(resultFile.List_OTSField[particle1.FieldId].FieldImage, ImreadModes.Grayscale))
                 {
                 {
-                    for (int j = 0; j < labelMat.Width; j++)
+                    foreach (Segment segment in m_ParticleSegmentation.ParticleData.SegmentList)
                     {
                     {
-                        int no = labelMat.Get<int>(k, j);
-                        if (no == 1)
-                        {
-                            points1.Add(new Point(j, k));
-                            aveGray1.Add(matBse.Get<byte>(k, j));
-                        }
-                        else if (no == 2)
+                        Cv2.Line(mat, new OpenCvSharp.Point(segment.Start, segment.Height), new OpenCvSharp.Point(segment.Start + segment.Length, segment.Height), Scalar.White, 1, LineTypes.AntiAlias);
+                    }
+                    //寻找坐标点
+                    List<Point> points1 = new List<Point>();
+                    List<Point> points2 = new List<Point>();
+                    List<int> aveGray1 = new List<int>();
+                    List<int> aveGray2 = new List<int>();
+                    for (int k = 0; k < mat.Height; k++)
+                    {
+                        for (int j = 0; j < mat.Width; j++)
                         {
                         {
-                            points2.Add(new Point(j, k));
-                            aveGray2.Add(matBse.Get<byte>(k, j));
+                            if (mat.Get<int>(k, j) != 0)
+                            {
+                                int side = (startPoint.X - j) * (endPoint.Y - k) - (startPoint.Y - k) * (endPoint.X - j);
+                                if (side >= 0)//区分像素位置
+                                {
+                                    points1.Add(new Point(j, k));
+                                    aveGray1.Add(matBse.Get<byte>(k, j));
+                                }
+                                else
+                                {
+                                    points2.Add(new Point(j, k));
+                                    aveGray2.Add(matBse.Get<byte>(k, j));
+                                }
+                            }
                         }
                         }
                     }
                     }
-                }
-                mat.Dispose();
-                matBse.Dispose();
-                //处理Segment
-                List<COTSSegmentClr> SegmentClrList1 = new List<COTSSegmentClr>();
-                List<COTSSegmentClr> SegmentClrList2 = new List<COTSSegmentClr>();
-                List<Segment> SegmentList1 = new List<Segment>();
-                List<Segment> SegmentList2 = new List<Segment>();
-                GetSegment(points1, SegmentClrList1, ref SegmentList1);
-                GetSegment(points2, SegmentClrList2, ref SegmentList2);
+                    //处理Segment
+                    List<COTSSegmentClr> SegmentClrList1 = new List<COTSSegmentClr>();
+                    List<COTSSegmentClr> SegmentClrList2 = new List<COTSSegmentClr>();
+                    List<Segment> SegmentList1 = new List<Segment>();
+                    List<Segment> SegmentList2 = new List<Segment>();
+                    GetSegment(points1, SegmentClrList1, ref SegmentList1);
+                    GetSegment(points2, SegmentClrList2, ref SegmentList2);
+
+                    //颗粒一
+                    Cv2.Threshold(mat, mat, 0, 0, ThresholdTypes.Binary);
+                    foreach (Segment segment in SegmentList1)
+                    {
+                        Cv2.Line(mat, new OpenCvSharp.Point(segment.Start, segment.Height), new OpenCvSharp.Point(segment.Start + segment.Length, segment.Height), Scalar.White, 1, LineTypes.AntiAlias);
+                    }
+                    Cv2.ConnectedComponentsWithStats(mat, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
 
 
-                CImageHandler m_ImagePro = new CImageHandler();
-                COTSParticleClr part1 = new COTSParticleClr();
-                part1.SetArea(stats.At<int>(1, 4) * dPixelSize * dPixelSize);
-                part1.SetParticleRect(new Rectangle(stats.At<int>(1, 0), stats.At<int>(1, 1), stats.At<int>(1, 2), stats.At<int>(1, 3)));
-                part1.GetFeature().SetSegmentsList(SegmentClrList1, false);
-                m_ImagePro.CalParticleImageProp(part1, dPixelSize);
-                COTSParticleClr part2 = new COTSParticleClr();
-                part2.SetArea(stats.At<int>(2, 4) * dPixelSize * dPixelSize);
-                part2.SetParticleRect(new Rectangle(stats.At<int>(2, 0), stats.At<int>(2, 1), stats.At<int>(2, 2), stats.At<int>(2, 3)));
-                part2.GetFeature().SetSegmentsList(SegmentClrList2, false);
-                m_ImagePro.CalParticleImageProp(part2, dPixelSize);
-
-                particle1.SegmentList = SegmentList1;
-                particle1.SegmentNum = SegmentList1.Count;
-                particle1.AveGray = (int)aveGray1.Average();
-                particle1.RectLeft = stats.At<int>(1, 0);
-                particle1.RectTop = stats.At<int>(1, 1);
-                particle1.RectWidth = stats.At<int>(1, 2);
-                particle1.RectHeight = stats.At<int>(1, 3);
-                particle1.Area = stats.At<int>(1, 4) * dPixelSize * dPixelSize;
-                particle1.PosX = (int)centroids.At<double>(1, 0);
-                particle1.PosY = (int)centroids.At<double>(1, 1);
-                particle1.DFERET = part1.GetFeretDiameter();
-                particle1.DMAX = part1.GetDMAX();
-                particle1.DMIN = part1.GetDMIN();
-                particle1.DPERP = part1.GetDMPERP();
-                particle1.PERIMETER = part1.GetDPRIMETER();
-                particle1.ORIENTATION = part1.GetORIENTATION();
-                particle1.DINSCR = part1.GetDINSCR();
-                particle1.DMEAN = part1.GetDMEAN();
-                particle1.DELONG = part1.GetDELONG();
-
-                particle2.SegmentList = SegmentList2;
-                particle2.SegmentNum = SegmentList2.Count;
-                particle2.AveGray = (int)aveGray2.Average();
-                particle2.RectLeft = stats.At<int>(2, 0);
-                particle2.RectTop = stats.At<int>(2, 1);
-                particle2.RectWidth = stats.At<int>(2, 2);
-                particle2.RectHeight = stats.At<int>(2, 3);
-                particle2.Area = stats.At<int>(2, 4) * dPixelSize * dPixelSize;
-                particle2.PosX = (int)centroids.At<double>(2, 0);
-                particle2.PosY = (int)centroids.At<double>(2, 1);
-                particle2.DFERET = part2.GetFeretDiameter();
-                particle2.DMAX = part2.GetDMAX();
-                particle2.DMIN = part2.GetDMIN();
-                particle2.DPERP = part2.GetDMPERP();
-                particle2.PERIMETER = part2.GetDPRIMETER();
-                particle2.ORIENTATION = part2.GetORIENTATION();
-                particle2.DINSCR = part2.GetDINSCR();
-                particle2.DMEAN = part2.GetDMEAN();
-                particle2.DELONG = part2.GetDELONG();
-
-                if (!SaveToDb(particle1, particle2))
-                {
-                    log.Trace("(GetSplitPartFun) SaveToDb Faild");
-                    return false;
+                    CopyToPart(particle1, SegmentList1, SegmentClrList1, aveGray1, stats, centroids, dPixelSize);
+
+                    //颗粒二
+                    Cv2.Threshold(mat, mat, 0, 0, ThresholdTypes.Binary);
+                    foreach (Segment segment in SegmentList2)
+                    {
+                        Cv2.Line(mat, new OpenCvSharp.Point(segment.Start, segment.Height), new OpenCvSharp.Point(segment.Start + segment.Length, segment.Height), Scalar.White, 1, LineTypes.AntiAlias);
+                    }
+                    Cv2.ConnectedComponentsWithStats(mat, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
+
+                    CopyToPart(particle2, SegmentList2, SegmentClrList2, aveGray2, stats, centroids, dPixelSize);
+
+                    if (!SaveToDb(particle1, particle2))
+                    {
+                        log.Trace("(GetSplitPartFun) SaveToDb Faild");
+                        return false;
+                    }
                 }
                 }
 
 
                 return true;
                 return true;
@@ -825,6 +797,47 @@ namespace OTSIncAReportGraph.OTSIncAReportGraphFuncation
                 return false;
                 return false;
             }
             }
         }
         }
+
+        /// <summary>
+        /// 拷贝颗粒数据
+        /// </summary>
+        /// <param name="particle"></param>
+        /// <param name="segments"></param>
+        /// <param name="SegmentClrList"></param>
+        /// <param name="aveGray"></param>
+        /// <param name="stats"></param>
+        /// <param name="centroids"></param>
+        /// <param name="dPixelSize"></param>
+        private void CopyToPart(Particle particle, List<Segment> segments, List<COTSSegmentClr> SegmentClrList, List<int> aveGray, Mat stats, Mat centroids, double dPixelSize)
+        {
+            CImageHandler m_ImagePro = new CImageHandler();
+            COTSParticleClr part = new COTSParticleClr();
+            part.SetArea(stats.At<int>(1, 4) * dPixelSize * dPixelSize);
+            part.SetParticleRect(new Rectangle(stats.At<int>(1, 0), stats.At<int>(1, 1), stats.At<int>(1, 2), stats.At<int>(1, 3)));
+            part.GetFeature().SetSegmentsList(SegmentClrList, false);
+            m_ImagePro.CalParticleImageProp(part, dPixelSize);
+
+            particle.SegmentList = segments;
+            particle.SegmentNum = segments.Count;
+            particle.AveGray = (int)aveGray.Average();
+            particle.RectLeft = stats.At<int>(1, 0);
+            particle.RectTop = stats.At<int>(1, 1);
+            particle.RectWidth = stats.At<int>(1, 2);
+            particle.RectHeight = stats.At<int>(1, 3);
+            particle.Area = stats.At<int>(1, 4) * dPixelSize * dPixelSize;
+            particle.PosX = (int)centroids.At<double>(1, 0);
+            particle.PosY = (int)centroids.At<double>(1, 1);
+            particle.DFERET = part.GetFeretDiameter();
+            particle.DMAX = part.GetDMAX();
+            particle.DMIN = part.GetDMIN();
+            particle.DPERP = part.GetDMPERP();
+            particle.PERIMETER = part.GetDPRIMETER();
+            particle.ORIENTATION = part.GetORIENTATION();
+            particle.DINSCR = part.GetDINSCR();
+            particle.DMEAN = part.GetDMEAN();
+            particle.DELONG = part.GetDELONG();
+        }
+
         /// <summary>
         /// <summary>
         /// 保存数据库
         /// 保存数据库
         /// </summary>
         /// </summary>