Browse Source

添加合并颗粒功能

zty 2 years ago
parent
commit
a9b4e790ff

+ 1 - 2
OTSIncAMeasureApp/0-OTSModel/Measure/3-MeasureFlow/CSmplMeasure.cs

@@ -814,13 +814,12 @@ namespace OTSModelSharp
             List<COTSParticleClr> mergedParticles = new List<COTSParticleClr>();
             double pixelSize = theSample.CalculatePixelSize();
             int overlap = theSample.GetMsrParams().GetImageProcessParam().GetOverlapParam();
-            Dictionary<COTSParticleClr, COTSParticleClr> combinBorderParts = new Dictionary<COTSParticleClr, COTSParticleClr>();
 
             //Repeated particle treatment
             if (theSample.GetMsrParams().GetImageProcessParam().GetPartCombin())
             {
                 log.Info("begin repeated particle treatment...");
-                if (imgpro.RepeatedParticleTreatment(theSample.GetFieldsData(), ref combinBorderParts, theSample, m_pSampleRstFile.m_strdbPathName))
+                if (imgpro.RepeatedParticleTreatment(theSample.GetFieldsData(), theSample, m_pSampleRstFile.m_strdbPathName))
                 {
                     log.Info("success...");
                 }

+ 79 - 14
OTSIncAMeasureApp/ServiceCenter/CImageHandler.cs

@@ -389,7 +389,7 @@ namespace OTSModelSharp.ServiceInterface
             return points;
         }
 
-        public bool RepeatedParticleTreatment(List<COTSFieldData> allFields, ref Dictionary<COTSParticleClr, COTSParticleClr> combinBorderParts, COTSSample theSample, string stdPath)
+        public bool RepeatedParticleTreatment(List<COTSFieldData> allFields, COTSSample theSample, string stdPath)
         {
             int maxPartCount = theSample.GetMsrParams().GetXRayParam().GetXrayLimit();
             int overlap = theSample.GetMsrParams().GetImageProcessParam().GetOverlapParam();
@@ -400,6 +400,9 @@ namespace OTSModelSharp.ServiceInterface
             int offsetX = scanFieldSizeX - (int)(overlap * pixelSize);
             int offsetY = scanFieldSizeY - (int)(overlap * pixelSize);
             List<COTSParticleClr> deletePartList = new List<COTSParticleClr>();
+            List<COTSParticleClr> updatePartList= new List<COTSParticleClr>();
+            Dictionary<COTSParticleClr, COTSParticleClr> combinBorderParts = new Dictionary<COTSParticleClr, COTSParticleClr>();
+            List<string> combinBorderParts_style = new List<string>();
 
             foreach (var item in allFields)
             {
@@ -418,7 +421,7 @@ namespace OTSModelSharp.ServiceInterface
                     if (leftField[0].GetIsMeasureComplete() == true)//存在测量帧图
                     {
                         //寻找重叠颗粒
-                        deletePartList.AddRange(FieldFun(leftField[0], item, "left_right", resolutionSize, overlap, ref combinBorderParts));
+                        deletePartList.AddRange(FieldFun(leftField[0], item, "left_right", resolutionSize, overlap, ref combinBorderParts, ref combinBorderParts_style));
                     }
                 }
                 if (rightField.Count() == 1)//包含右帧图
@@ -426,7 +429,7 @@ namespace OTSModelSharp.ServiceInterface
                     if (rightField[0].GetIsMeasureComplete() == true)//存在测量帧图
                     {
                         //寻找重叠颗粒
-                        deletePartList.AddRange(FieldFun(item, rightField[0], "left_right", resolutionSize, overlap, ref combinBorderParts));
+                        deletePartList.AddRange(FieldFun(item, rightField[0], "left_right", resolutionSize, overlap, ref combinBorderParts, ref combinBorderParts_style));
                     }
                 }
                 if (upField.Count() == 1)//包含上帧图
@@ -434,7 +437,7 @@ namespace OTSModelSharp.ServiceInterface
                     if (upField[0].GetIsMeasureComplete() == true)//存在测量帧图
                     {
                         //寻找重叠颗粒
-                        deletePartList.AddRange(FieldFun(upField[0], item, "up_down", resolutionSize, overlap, ref combinBorderParts));
+                        deletePartList.AddRange(FieldFun(upField[0], item, "up_down", resolutionSize, overlap, ref combinBorderParts, ref combinBorderParts_style));
                     }
                 }
                 if (downField.Count() == 1)//包含下帧图
@@ -442,7 +445,7 @@ namespace OTSModelSharp.ServiceInterface
                     if (downField[0].GetIsMeasureComplete() == true)//存在测量帧图
                     {
                         //寻找重叠颗粒
-                        deletePartList.AddRange(FieldFun(item, downField[0], "up_down", resolutionSize, overlap, ref combinBorderParts));
+                        deletePartList.AddRange(FieldFun(item, downField[0], "up_down", resolutionSize, overlap, ref combinBorderParts, ref combinBorderParts_style));
                     }
                 }
             }
@@ -454,17 +457,28 @@ namespace OTSModelSharp.ServiceInterface
 
             deletePartList = deletePartList.Distinct().ToList();//去重
 
-            if (!sQLiteHelper.DeletePartForTransaction(deletePartList))
+            if (!sQLiteHelper.DeletePartForTransaction(deletePartList))//删除重复颗粒
             {
                 return false;
             }
 
+            if (!sQLiteHelper.CombinPartForTransaction(allFields, combinBorderParts, updatePartList, combinBorderParts_style, resolutionSize))//合并大颗粒
+            {
+                return false;
+            }
+
+            if (!sQLiteHelper.UpdatePartForTransaction(updatePartList))//修改Segment
+            {
+                return false;
+            }
+
+
             sQLiteHelper.CommitTransaction();
 
             return true;
         }
 
-        private List<COTSParticleClr> FieldFun(COTSFieldData left_upField, COTSFieldData right_downField, string style, System.Drawing.Size resolutionSize, int overlap, ref Dictionary<COTSParticleClr, COTSParticleClr> combinBorderParts)
+        private List<COTSParticleClr> FieldFun(COTSFieldData left_upField, COTSFieldData right_downField, string style, System.Drawing.Size resolutionSize, int overlap, ref Dictionary<COTSParticleClr, COTSParticleClr> combinBorderParts, ref List<string> combinBorderParts_style)
         {
             List<COTSParticleClr> particleClrs = new List<COTSParticleClr>();
             combinBorderParts = new Dictionary<COTSParticleClr, COTSParticleClr>();
@@ -512,6 +526,7 @@ namespace OTSModelSharp.ServiceInterface
                             if (i > rightRectangle.Y && i < rightRectangle.Y + rightRectangle.Height)
                             {
                                 combinBorderParts.Add(leftBorder, rightBorder);
+                                combinBorderParts_style.Add("left");
                                 isTrue = true;
                                 break;
                             }
@@ -530,7 +545,10 @@ namespace OTSModelSharp.ServiceInterface
                         Rectangle leftRectangle = (Rectangle)leftParticles.GetParticleRect();
                         if (leftRectangle.Left > resolutionSize.Width - overlap)//未跨界
                         {
-                            particleClrs.Add(leftParticles);
+                            if (!combinBorderParts.ContainsKey(leftParticles))
+                            {
+                                particleClrs.Add(leftParticles);
+                            }
                         }
                     }
                 }
@@ -541,7 +559,10 @@ namespace OTSModelSharp.ServiceInterface
                         Rectangle downRectangle = (Rectangle)downParticles.GetParticleRect();
                         if (downRectangle.Right < overlap / 2)//未跨界
                         {
-                            particleClrs.Add(downParticles);
+                            if (!combinBorderParts.ContainsValue(downParticles))
+                            {
+                                particleClrs.Add(downParticles);
+                            }
                         }
                     }
                 }
@@ -551,6 +572,8 @@ namespace OTSModelSharp.ServiceInterface
                 //寻找上帧图的下侧区域颗粒
                 double left_upField_sum = 0;
                 double right_downField_sum = 0;
+                List<COTSParticleClr> upBorderParts = new List<COTSParticleClr>();
+                List<COTSParticleClr> downBorderParts = new List<COTSParticleClr>();
                 foreach (var upParticles in left_upField.GetListAnalysisParticles())
                 {
                     Rectangle upRectangle = (Rectangle)upParticles.GetParticleRect();
@@ -558,6 +581,10 @@ namespace OTSModelSharp.ServiceInterface
                     {
                         left_upField_sum += upParticles.GetActualArea();
                     }
+                    if (upRectangle.Bottom == resolutionSize.Height || upRectangle.Bottom == resolutionSize.Height - 1)//边界
+                    {
+                        upBorderParts.Add(upParticles);
+                    }
                 }
                 foreach (var downParticles in right_downField.GetListAnalysisParticles())
                 {
@@ -566,6 +593,38 @@ namespace OTSModelSharp.ServiceInterface
                     {
                         right_downField_sum += downParticles.GetActualArea();
                     }
+                    if (downRectangle.Top == 0)//边界
+                    {
+                        downBorderParts.Add(downParticles);
+                    }
+                    else if (downRectangle.Top == 1)//边界
+                    {
+                        downBorderParts.Add(downParticles);
+                    }
+                }
+
+                foreach (var upBorder in upBorderParts)
+                {
+                    Rectangle upRectangle = (Rectangle)upBorder.GetParticleRect();
+                    foreach (var downBorder in downBorderParts)
+                    {
+                        Rectangle downRectangle = (Rectangle)downBorder.GetParticleRect();
+                        bool isTrue = false;
+                        for (int i = upRectangle.X; i < upRectangle.X + upRectangle.Width; i++)
+                        {
+                            if (i > downRectangle.X && i < downRectangle.X + downRectangle.Width)
+                            {
+                                combinBorderParts.Add(upBorder, downBorder);
+                                combinBorderParts_style.Add("up");
+                                isTrue = true;
+                                break;
+                            }
+                        }
+                        if (isTrue)
+                        {
+                            break;
+                        }
+                    }
                 }
 
                 if (left_upField_sum < right_downField_sum)
@@ -575,7 +634,10 @@ namespace OTSModelSharp.ServiceInterface
                         Rectangle upRectangle = (Rectangle)upParticles.GetParticleRect();
                         if (upRectangle.Top > resolutionSize.Height - overlap / 2)//未跨界
                         {
-                            particleClrs.Add(upParticles);
+                            if (!combinBorderParts.ContainsKey(upParticles))
+                            {
+                                particleClrs.Add(upParticles);
+                            }
                         }
                     }
                 }
@@ -586,7 +648,10 @@ namespace OTSModelSharp.ServiceInterface
                         Rectangle downRectangle = (Rectangle)downParticles.GetParticleRect();
                         if (downRectangle.Bottom < overlap / 2)//未跨界
                         {
-                            particleClrs.Add(downParticles);
+                            if (!combinBorderParts.ContainsValue(downParticles))
+                            {
+                                particleClrs.Add(downParticles);
+                            }
                         }
                     }
                 }
@@ -740,9 +805,9 @@ namespace OTSModelSharp.ServiceInterface
             MStitch mStitch = new MStitch();
             mStitch.Pwidth = newImg1.Width;
             mStitch.Pheight = newImg1.Height;
-            mStitch.W_min = min_w - 100;
-            mStitch.W_max = min_w + 100;
-            mStitch.H_min = newImg1.Height;
+            mStitch.W_min = min_w - 50;
+            mStitch.W_max = min_w - 50;
+            mStitch.H_min = newImg1.Height - 20;
             mStitch.minval = 255;
             mStitch.im = newImg1;
 

+ 192 - 2
OTSIncAMeasureApp/ServiceCenter/DTLBase/SQliteHelper.cs

@@ -211,6 +211,190 @@ namespace OTSModelSharp.DTLBase
             return true;
         }
 
+        /// <summary>
+        /// 合并大颗粒
+        /// </summary>
+        /// <param name="combinBorderParts"></param>
+        /// <returns></returns>
+        public bool CombinPartForTransaction(List<COTSFieldData> allFields, Dictionary<COTSParticleClr, COTSParticleClr> combinBorderParts, List<COTSParticleClr> updatePartList, List<string> combinBorderParts_style, System.Drawing.Size resolutionSize)
+        {
+            for (int i = 0; i < combinBorderParts.Count; i++)
+            {
+                COTSParticleClr particleClr_key = new COTSParticleClr();
+                COTSParticleClr particleClr_value = new COTSParticleClr();
+                for (int j = 0; j < allFields.Count; j++)
+                {
+                    for (int k = 0; k < allFields[j].GetListAnalysisParticles().Count; k++)
+                    {
+                        if (allFields[j].GetListAnalysisParticles()[k] == combinBorderParts.ElementAt(i).Key)
+                        {
+                            particleClr_key = allFields[j].GetListAnalysisParticles()[k];
+                            break;
+                        }
+                    }
+                }
+                for (int j = 0; j < allFields.Count; j++)
+                {
+                    for (int k = 0; k < allFields[j].GetListAnalysisParticles().Count; k++)
+                    {
+                        if (allFields[j].GetListAnalysisParticles()[k] == combinBorderParts.ElementAt(i).Value)
+                        {
+                            particleClr_value = allFields[j].GetListAnalysisParticles()[k];
+                            break;
+                        }
+                    }
+                }
+                COTSFeatureClr featureClr_key = particleClr_key.GetFeature();
+                COTSFeatureClr featureClr_value = particleClr_value.GetFeature();
+
+                List<COTSSegmentClr> segmentClr_key = featureClr_key.GetSegmentsList();
+                List<COTSSegmentClr> segmentClr_value = featureClr_value.GetSegmentsList();
+
+                List<int> height_left_up = new List<int>();
+                List<int> height_right_down = new List<int>();
+                if (combinBorderParts_style[i] == "left")
+                {
+                    foreach (var item in segmentClr_key)
+                    {
+                        if (item.GetStart() + item.GetLength() == resolutionSize.Width)
+                        {
+                            height_left_up.Add(item.GetHeight());
+                        }
+                    }
+                    foreach (var item in segmentClr_value)
+                    {
+                        if (item.GetStart() == 0)
+                        {
+                            height_right_down.Add(item.GetHeight());
+                        }
+                    }
+
+                    int center_left = (int)height_left_up.Average();
+                    int center_right = (int)height_right_down.Average();
+
+                    if (height_left_up.Max() < height_right_down.Max())
+                    {
+                        int des = center_right - center_left;
+                        foreach (var item in segmentClr_key)
+                        {
+                            int newHeight = item.GetHeight() + des > resolutionSize.Height - 1 ? resolutionSize.Height - 1 : item.GetHeight() + des;
+                            item.SetHeight(newHeight);
+                        }
+                    }
+                    else
+                    {
+                        int des = center_left - center_right;
+                        foreach (var item in segmentClr_value)
+                        {
+                            int newHeight = item.GetHeight() + des > resolutionSize.Height - 1 ? resolutionSize.Height - 1 : item.GetHeight() + des;
+                            item.SetHeight(newHeight);
+                        }
+                    }
+                }
+                else
+                {
+                    int flag = 0;
+                    foreach (var item in segmentClr_key)
+                    {
+                        if (item.GetHeight() == resolutionSize.Height - 1)
+                        {
+                            height_left_up.Add(item.GetStart());
+                        }
+                    }
+                    foreach (var item in segmentClr_value)
+                    {
+                        if (item.GetHeight() == 0)
+                        {
+                            height_right_down.Add(item.GetStart());
+                        }
+                    }
+                    if (height_right_down.Count == 0)
+                    {
+                        flag = 1;
+                        foreach (var item in segmentClr_value)
+                        {
+                            if (item.GetHeight() == 1)
+                            {
+                                height_right_down.Add(item.GetStart());
+                            }
+                        }
+                    }
+
+                    int center_left = (int)height_left_up.Average();
+                    int center_right = (int)height_right_down.Average();
+                    if (height_left_up.Max() < height_right_down.Max())
+                    {
+                        int des = height_right_down.Max() - height_left_up.Max();
+                        foreach (var item in segmentClr_key)
+                        {
+                            int newStart = item.GetStart() + des;
+                            item.SetStart(newStart);
+                            if (newStart + item.GetLength() > resolutionSize.Width)
+                            {
+                                item.SetLength(resolutionSize.Width - newStart);
+                            }
+                        }
+                    }
+                    else
+                    {
+                        int des = height_left_up.Max() - height_right_down.Max();
+                        foreach (var item in segmentClr_value)
+                        {
+                            int newStart = item.GetStart() + des;
+                            item.SetStart(newStart);
+                            if (newStart + item.GetLength() > resolutionSize.Width)
+                            {
+                                item.SetLength(resolutionSize.Width - newStart);
+                            }
+                            if (flag == 1)
+                            {
+                                item.SetHeight(item.GetHeight() - 1);
+                            }
+                        }
+                    }
+                }
+
+                featureClr_key.SetSegmentsList(segmentClr_key, true);
+                featureClr_value.SetSegmentsList(segmentClr_value, true);
+
+                particleClr_key.SetFeature(featureClr_key);
+                particleClr_value.SetFeature(featureClr_value);
+
+                updatePartList.Add(particleClr_key);
+                updatePartList.Add(particleClr_value);
+
+                //COTSParticleClr particleClr_new = combinBorderParts.ElementAt(i).Key;
+                //particleClr_new.SetAbsolutPos(combinBorderParts.ElementAt(i).Key.GetAbsolutPos());
+                //string partsStr = combinBorderParts.ElementAt(i).Key.GetFieldId() + ":" + combinBorderParts.ElementAt(i).Key.GetAnalysisId();
+                //partsStr += "," + combinBorderParts.ElementAt(i).Value.GetFieldId() + ":" + combinBorderParts.ElementAt(i).Value.GetAnalysisId();
+                //double allPartArea = combinBorderParts.ElementAt(i).Key.GetActualArea() + combinBorderParts.ElementAt(i).Value.GetActualArea();
+                //int SegmentNum = combinBorderParts.ElementAt(i).Key.GetFeature().GetSegmentsList().Count + combinBorderParts.ElementAt(i).Value.GetFeature().GetSegmentsList().Count;
+                //Point point_new = (Point)particleClr_new.GetXRayPos();
+                //Point point_field = (Point)particleClr_new.GetAbsolutPos();
+                //Rectangle rectangle_key = (Rectangle)combinBorderParts.ElementAt(i).Key.GetParticleRect();
+                //Rectangle rectangle_value = (Rectangle)combinBorderParts.ElementAt(i).Value.GetParticleRect();
+                ////rectangle_key.Width += rectangle_value.Width;
+                ////rectangle_key.Height += rectangle_value.Height - Math.Abs(height_left_up.Max() - height_right_down.Max());
+                //particleClr_new.SetFieldId(-1);
+                //particleClr_new.SetAnalysisId(i + 1);
+                //particleClr_new.SetTagId(i + 1);
+                //particleClr_new.SetTypeName(combinBorderParts.ElementAt(i).Key.GetTypeName());
+                //particleClr_new.SetTypeColor(combinBorderParts.ElementAt(i).Key.GetTypeColor());
+                //particleClr_new.SetArea(allPartArea);
+                //particleClr_new.SetParticleRect(rectangle_key);
+                //particleClr_new.SetSubParticles(partsStr);
+
+                //cmdForTrans.CommandText = "INSERT INTO MergedParticleInfo(FieldId, XrayId, AveGray, RectLeft, RectTop, RectWidth, RectHeight, Area, PosX, PosY, TypeId, ElementNum, SegmentNum, FieldPosX, FieldPosY, ParticleId, DMAX, DMIN, DPERP, PERIMETER, ORIENTATION, DINSCR, DMEAN, DELONG, DFERET, TypeName, TypeColor, SubParticles) VALUES("
+                //    + particleClr_new.GetFieldId() + ", " + particleClr_new.GetAnalysisId() + ", " + particleClr_new.GetAveGray() + "," + rectangle_key.Left + ", " + rectangle_key.Top + ", " + rectangle_key.Width + ", " + rectangle_key.Height + ", " + particleClr_new.GetActualArea() + ", " + point_new.X + ", " + point_new.Y + "," + particleClr_new.GetType() + "," + 0 + "," + SegmentNum + "," + point_field.X + "," + point_field.Y + "," + particleClr_new.GetTagId() + "," + particleClr_new.GetDMAX() + "," + particleClr_new.GetDMIN() + ", " + particleClr_new.GetDMPERP() + ", " + particleClr_new.GetDPRIMETER() + ", " + particleClr_new.GetORIENTATION() + ", " + particleClr_new.GetDINSCR() + ", " + particleClr_new.GetDMEAN() + ", " + particleClr_new.GetDELONG() + "," + particleClr_new.GetFeretDiameter() + ",'" + particleClr_new.GetTypeName() + "','" + particleClr_new.GetTypeColor() + "','" + particleClr_new.GetSubParticles() + "')";
+                //if (cmdForTrans.ExecuteNonQuery() == 0)
+                //{
+                //    return false;
+                //}
+            }
+
+            return true;
+        }
+
         /// <summary>
         /// 删除颗粒信息
         /// </summary>
@@ -249,9 +433,15 @@ namespace OTSModelSharp.DTLBase
                 {
                     return false;
                 }
-                foreach (var SegmentsList in PartList.GetFeature().GetSegmentsList())
+                cmdForTrans.CommandText = "DELETE FROM Segment WHERE XrayId = " + PartList.GetAnalysisId() + " and FieldId = " + PartList.GetFieldId() + "";
+                if (cmdForTrans.ExecuteNonQuery() == 0)
+                {
+                    return false;
+                }
+                for (int i = 0; i < PartList.GetFeature().GetSegmentsList().Count; i++)
                 {
-                    cmdForTrans.CommandText = "UPDATE Segment SET Start = " + SegmentsList.GetStart() + ", Height = '" + SegmentsList.GetHeight() + "' WHERE XrayId = " + PartList.GetAnalysisId() + " and FieldId = " + PartList.GetFieldId() + "";
+                    cmdForTrans.CommandText = "INSERT INTO Segment (XRayId,FieldId,SegmentId,SegmentNum,Start,Height,Length,ParticleId) VALUES ("+
+                        PartList.GetAnalysisId() + "," + PartList.GetFieldId() + "," + i + "," + PartList.GetFeature().GetSegmentsList().Count + "," + PartList.GetFeature().GetSegmentsList()[i].GetStart() + "," + PartList.GetFeature().GetSegmentsList()[i].GetHeight() + "," + PartList.GetFeature().GetSegmentsList()[i].GetLength() + "," + PartList.GetAnalysisId() + ")";
                     if (cmdForTrans.ExecuteNonQuery() == 0)
                     {
                         return false;

+ 2 - 1
OTSIncAReportApp/1-UI/Control_Grids/ParticlesGridDevidePage.cs

@@ -824,7 +824,8 @@ namespace OTSIncAReportGrids
                                 {
                                     string[] str = subt.Split(',');
                                     bmap.Tag = new List<string>() { str[0].Split(':')[0], str[0].Split(':')[1], particles.Rows[i]["TypeId"].ToString() };
-                                    dgV_ParticlesDevidePage.Rows[add_rowindex].Height = 150;
+                                    //dgV_ParticlesDevidePage.Rows[add_rowindex].Height = 150;
+                                    dgV_ParticlesDevidePage.Rows[add_rowindex].Height = bmap.Height + 20; ;
                                     dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = bmap;
                                     dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Style.BackColor = Color.Azure;
                                     dgV_ParticlesDevidePage.Rows[add_rowindex].DefaultCellStyle.ForeColor = Color.Chocolate;