Example #1
0
        public VisionTask(VisionTaskInfo info)
        {
            #region MyRegion
            result        = new ToolResult();
            ToolsDic      = new Dictionary <string, ITool>();
            ToolList      = new List <ITool>();
            ToolResultDic = new Dictionary <string, ToolResult>();
            InputRegion   = new HRegion();
            InputRegion.GenEmptyRegion();
            InputImage = new HImage();
            InputImage.GenEmptyObj();
            #endregion

            this.Info = info;
            TaskName  = info.TaskName;
            //显示界面布局
            foreach (var item in Info.TaskToolsInfo)
            {
                object[] paras = new object[] { item };
                ITool    tool  = CreatingHelper <ITool> .CreateInstance(@".//VisionTools/" + item.GetToolNameSpace() + ".dll", item.GetToolNameSpace(), item.GetToolClassName(), paras);

                if (tool != null)
                {
                    ToolsDic.Add(tool.TaskName, tool);
                    ToolList.Add(tool);
                }
            }
        }
Example #2
0
        /// <summary>
        /// 从DataTable数据获取最终区域
        /// </summary>
        /// <param name="data">DataTable</param>
        /// <returns>最终区域</returns>
        public HRegion GenCompRegion(DataTable data)
        {
            var hRegion = new HRegion();

            hRegion.GenEmptyRegion();
            foreach (DataRow dr in data.Rows)
            {
                HRegion hr, htemp;
                hr = GetRegion(dr);
                if ((bool)dr["ToAdd"])
                {
                    htemp = hRegion | hr;
                }
                else
                {
                    htemp = hRegion / hr;
                }
                hr.Dispose();

                hRegion.Dispose();
                hRegion = htemp.CopyObj(1, -1);
                htemp.Dispose();
            }

            return(hRegion);
        }
Example #3
0
        public static HRegion GenLineRegion(double startRow, double startCol, double endRow, double endCol)
        {
            HRegion line = new HRegion();

            line.GenEmptyRegion();
            line.GenRegionLine(startRow, startCol, endRow, endCol);
            return(line);
        }
Example #4
0
        public static HRegion GenEllipseRegion(double row, double col, double angle, double radius1, double radius2)
        {
            HRegion ellipse = new HRegion();

            ellipse.GenEmptyRegion();
            ellipse.GenEllipse(row, col, angle, radius1, radius2);
            return(ellipse);
        }
Example #5
0
        public static HRegion GenRectangle2Region(double row, double col, double angle, double length1, double length2)
        {
            HRegion Rectangle2 = new HRegion();

            Rectangle2.GenEmptyRegion();
            Rectangle2.GenRectangle2(row, col, angle, length1, length2);
            return(Rectangle2);
        }
Example #6
0
        public static HRegion GenRectangle1Region(double row1, double col1, double row2, double col2)
        {
            HRegion Rectangle1 = new HRegion();

            Rectangle1.GenEmptyRegion();
            Rectangle1.GenRectangle1(row1, col1, row2, col2);
            return(Rectangle1);
        }
Example #7
0
        public static HRegion GenCircleRegion(double row, double col, double radius)
        {
            HRegion circleRegion = new HRegion();

            circleRegion.GenEmptyRegion();
            circleRegion.GenCircle(row, col, radius);
            return(circleRegion);
        }
Example #8
0
 public override bool Execute()
 {
     this.ROI.error = false;
     if (this.CurrentImage == null || this.ROI == null)
     {
         return(false);
     }
     try
     {
         HImage  reduceImage = this.CurrentImage.ReduceDomain(this.ROI.getRegion());
         HRegion region = reduceImage.Threshold((double)this.MinThreshold, this.MaxThreshold);
         HRegion connectRegion = region.Connection();
         HRegion selectRegionTmp, selectRegion;
         selectRegion = new HRegion();
         selectRegion.GenEmptyRegion();
         for (int i = 1; i < connectRegion.CountObj(); i++)
         {
             selectRegionTmp = connectRegion[i].SelectShape("area", "and", (double)this.MinArea, (double)this.MaxArea);
             if (selectRegionTmp != null)
             {
                 selectRegion = selectRegion.ConcatObj(selectRegionTmp);
             }
         }
         HRegion fillRegion;
         if (this.Filled)
         {
             fillRegion = selectRegion.FillUp();
         }
         else
         {
             fillRegion = selectRegion;
         }
         if (fillRegion.CountObj() > 0)
         {
             double areaMax   = fillRegion.Area.TupleMax();
             int    areaIndex = fillRegion.Area.TupleFind(areaMax);
             this.ResultRegion = fillRegion[areaIndex + 1];
         }
         else
         {
             this.ResultRegion = fillRegion;
         }
         if (this.ResultRegion.Area > 0)
         {
             return(true);
         }
         else
         {
             this.ROI.error = true;
             return(false);
         }
     }
     catch
     {
         this.ROI.error = true;
         return(false);
     }
 }
 public override void ClearTuYa()
 {
     if (reduceRegion != null && reduceRegion.IsInitialized())
     {
         reduceRegion.Dispose();
     }
     reduceRegion = new HRegion();
     reduceRegion.GenEmptyRegion();
 }
Example #10
0
 public VisionTask()
 {
     ToolsDic      = new Dictionary <string, ITool>();
     result        = new ToolResult();
     ToolList      = new List <ITool>();
     ToolResultDic = new Dictionary <string, ToolResult>();
     InputRegion   = new HRegion();
     InputRegion.GenEmptyRegion();
     InputImage = new HImage();
     InputImage.GenEmptyObj();
 }
Example #11
0
        /// <summary>
        /// Calculates the ModelROI region for all objects contained
        /// in ROIList, by adding and subtracting the positive and
        /// negative ROI objects.
        /// </summary>
        public bool defineModelROI()
        {
            HRegion tmpAdd, tmpDiff, tmp;
            double  row, col;

            if (stateROI == MODE_ROI_NONE)
            {
                return(true);
            }

            tmpAdd  = new HRegion();
            tmpDiff = new HRegion();
            tmpAdd.GenEmptyRegion();
            tmpDiff.GenEmptyRegion();

            for (int i = 0; i < ROIList.Count; i++)
            {
                switch (((ROI)ROIList[i]).getOperatorFlag())
                {
                case ROI.POSITIVE_FLAG:
                    tmp    = ((ROI)ROIList[i]).getRegion();
                    tmpAdd = tmp.Union2(tmpAdd);
                    break;

                case ROI.NEGATIVE_FLAG:
                    tmp     = ((ROI)ROIList[i]).getRegion();
                    tmpDiff = tmp.Union2(tmpDiff);
                    break;

                default:
                    break;
                }        //end of switch
            }            //end of for

            ModelROI = null;

            if (tmpAdd.AreaCenter(out row, out col) > 0)
            {
                tmp = tmpAdd.Difference(tmpDiff);
                if (tmp.AreaCenter(out row, out col) > 0)
                {
                    ModelROI = tmp;
                }
            }
            //in case the set of positiv and negative ROIs dissolve
            if (ModelROI == null || ROIList.Count == 0)
            {
                return(false);
            }
            //ModelRoiContour =   ModelROI.GenContourRegionXld("border_holes");
            //viewController.clearList();
            //viewController.addIconicVar(ModelRoiContour);
            return(true);
        }
        /// <summary>
        /// 获取+-运算后的roi轮廓
        /// </summary>
        public bool DefineModelROI()
        {
            HRegion tmpAdd, tmpDiff, tmp;
            double  row, col;

            if (stateROIOperation == ROIOperation.None)
            {
                return(true);
            }

            tmpAdd  = new HRegion();
            tmpDiff = new HRegion();
            tmpAdd.GenEmptyRegion();
            tmpDiff.GenEmptyRegion();

            for (int i = 0; i < ROIList.Count; i++)
            {
                switch (ROIList[i].OperatorFlag)
                {
                case ROIOperation.Positive:
                    tmp    = ROIList[i].GetRegion();
                    tmpAdd = tmp.Union2(tmpAdd);
                    break;

                case ROIOperation.Negative:
                    tmp     = ROIList[i].GetRegion();
                    tmpDiff = tmp.Union2(tmpDiff);
                    break;

                default:
                    break;
                } //end of switch
            }     //end of for

            modelROI = null;

            if (tmpAdd.AreaCenter(out row, out col) > 0)
            {
                tmp = tmpAdd.Difference(tmpDiff);
                if (tmp.AreaCenter(out row, out col) > 0)
                {
                    modelROI = tmp;
                }
            }

            //in case the set of positiv and negative ROIs dissolve
            if (modelROI == null || ROIList.Count == 0)
            {
                return(false);
            }

            return(true);
        }
        public void updateTuYaRegion(double row, double col)
        {
            if (reduceRegion == null)
            {
                reduceRegion = new HRegion();
                reduceRegion.GenEmptyRegion();
            }
            HRegion temp = new HRegion();

            temp.GenEmptyRegion();
            if (row < 1 && col < 1)
            {
                return;
            }
            temp.GenCircle(row, col, radius);
            reduceRegion = reduceRegion.Union2(temp);
        }
Example #14
0
        /// <summary>
        /// 在当前图像上按单元配置查找直线
        /// </summary>
        /// <param name="img"></param>
        /// <param name="hFixTool"></param>
        /// <returns></returns>
        public bool FindLine(HObject img)
        {
            m_rowBegin = new HTuple();
            m_colBegin = new HTuple();
            m_rowEnd   = new HTuple();
            m_colEnd   = new HTuple();

            m_hLine.Dispose();
            m_hCross.Dispose();
            m_hCrossDrop.Dispose();
            if (img == null)
            {
                return(false);
            }

            string strSelect = string.Empty;

            if (Hselect == "strongest")
            {
                strSelect = "all";
            }
            else
            {
                strSelect = Hselect;
            }

            HTuple imgWidth, imgHeight;

            HOperatorSet.GetImageSize(img, out imgWidth, out imgHeight);

            m_imageWidth  = imgWidth;
            m_imageHeight = imgHeight;

            double x1, y1, x2, y2;
            HTuple in_phi_t = in_phi + data_from_phi + data_phi * 3.1415926 / 180;
            double angle    = in_phi_t[0].D + 3.1415926 / 2;

            x1 = (in_col + data_col)[0].D + L2 * Math.Cos(angle);
            x2 = (in_col + data_col)[0].D - L2 * Math.Cos(angle);
            y1 = (in_row + data_row)[0].D - L2 * Math.Sin(angle);
            y2 = (in_row + data_row)[0].D + L2 * Math.Sin(angle);

            HHomMat2D hom = new HHomMat2D();

            hom.HomMat2dIdentity();
            hom = hom.HomMat2dTranslate((y2 - y1) / (Hnum - 1), (x2 - x1) / (Hnum - 1));

            double rect_row  = y1;
            double rect_col  = x1;
            double rect_phi  = in_phi_t[0].D;
            double rect_len1 = L1;
            double rect_len2 = (double)Hwid / 2.0;
            HTuple hms       = null;

            HOperatorSet.GenMeasureRectangle2(rect_row, rect_col, rect_phi, rect_len1,
                                              rect_len2, imgWidth, imgHeight, "nearest_neighbor", out hms);

            HTuple  Row         = new HTuple();
            HTuple  Col         = new HTuple();
            HTuple  Distance    = new HTuple();
            HTuple  outRow      = new HTuple();
            HTuple  outCol      = new HTuple();
            HTuple  outAmp      = new HTuple();
            HTuple  outDistance = new HTuple();
            HRegion regionCount = new HRegion();

            regionCount.GenEmptyRegion();
            HXLDCont contour = new HXLDCont();

            for (int i = 0; i < Hnum[0].I; ++i)
            {
                if (m_bShowRectang2)
                {
                    contour.Dispose();
                    contour.GenRectangle2ContourXld(rect_row, rect_col, rect_phi, rect_len1, rect_len2);
                    HRegion region;
                    region      = contour.GenRegionContourXld("margin");
                    regionCount = regionCount.Union2(region);
                }
                try
                {
                    HOperatorSet.MeasurePos(img, hms, Hsomth, Hamp, Htransition, strSelect,
                                            out outRow, out outCol, out outAmp, out outDistance);
                }
                catch { continue; }

                if (strSelect == "all" && outRow.Length > 1)
                {
                    HTuple hIndex = outAmp.TupleSortIndex();
                    int    nMax   = hIndex[hIndex.Length - 1];
                    Row.Append(outRow[nMax]);
                    Col.Append(outCol[nMax]);
                }
                else
                {
                    Row.Append(outRow);
                    Col.Append(outCol);
                }
                hom.AffineTransPixel(rect_row, rect_col, out rect_row, out rect_col);
                HOperatorSet.TranslateMeasure(hms, rect_row, rect_col);
            }
            HOperatorSet.CloseMeasure(hms);
            if (m_bShowRectang2)
            {
                m_hRectang2 = regionCount.GenContoursSkeletonXld(1, "filter");
            }

            if (Row.Length > 1)
            {
                HXLDCont counter = new HXLDCont();
                counter.GenContourPolygonXld(Row, Col);

                HTuple nr, nc, dist;
                counter.FitLineContourXld("tukey", -1, 0, 5, 2,
                                          out m_rowBegin, out m_colBegin, out m_rowEnd, out m_colEnd, out nr, out nc, out dist);

                HTuple rowDrop = new HTuple();
                HTuple colDrop = new HTuple();
                if (Row.Length - 3 > m_nNumDropPoints && m_nNumDropPoints > 0)
                {
                    HTuple distance = new HTuple();

                    for (int i = 0; i < Row.Length; ++i)
                    {
                        double dis = HMisc.DistancePl(Row[i], Col[i], m_rowBegin, m_colBegin, m_rowEnd, m_colEnd);
                        distance.Append(dis);
                    }
                    HTuple index = distance.TupleSortIndex();
                    index = index.TupleInverse();
                    for (int i = 0; i < m_nNumDropPoints; ++i)
                    {
                        int n = index[i];
                        rowDrop.Append(Row[n]);
                        colDrop.Append(Col[n]);
                    }
                    index = index.TupleFirstN(m_nNumDropPoints - 1);
                    Row   = Row.TupleRemove(index);
                    Col   = Col.TupleRemove(index);

                    counter.GenContourPolygonXld(Row, Col);
                    counter.FitLineContourXld("tukey", -1, 0, 5, 2,
                                              out m_rowBegin, out m_colBegin, out m_rowEnd, out m_colEnd, out nr, out nc, out dist);
                }

                if (m_bShowLine)
                {
                    HTuple rrow = new HTuple(m_rowBegin);
                    HTuple ccol = new HTuple(m_colBegin);
                    rrow.Append(m_rowEnd);
                    ccol.Append(m_colEnd);
                    m_hLine.GenContourPolygonXld(rrow, ccol);
                }

                if (m_bShowCross)
                {
                    m_hCross.GenEmptyObj();
                    m_hCross.GenCrossContourXld(Row, Col, 17, (new HTuple(45)).TupleRad());
                }

                if (m_bShowCrossDrop)
                {
                    m_hCrossDrop.GenEmptyObj();
                    m_hCrossDrop.GenCrossContourXld(rowDrop, colDrop, 17, (new HTuple(45)).TupleRad());
                }

                Result_Array[0] = m_rowBegin;
                Result_Array[1] = m_colBegin;
                Result_Array[2] = m_rowEnd;
                Result_Array[3] = m_colEnd;
                return(true);
            }
            return(false);
        }
Example #15
0
        /// <summary>
        /// Calculates the ModelROI region for all objects contained
        /// in ROIList, by adding and subtracting the positive and
        /// negative ROI objects.
        /// </summary>
        public bool defineModelROI()
        {
            HRegion tmpAdd, tmpDiff, tmp;
            double  row, col;
            double  rowDiff, colDiff;

            if (stateROI == MODE_ROI_NONE)
            {
                return(true);
            }

            tmp     = new HRegion();
            tmpAdd  = new HRegion();
            tmpDiff = new HRegion();
            tmpAdd.GenEmptyRegion();
            tmpDiff.GenEmptyRegion();

            for (int i = 0; i < ROIList.Count; i++)
            {
                switch (((ROI)ROIList[i]).getOperatorFlag())
                {
                case ROI.POSITIVE_FLAG:
                    tmp    = ((ROI)ROIList[i]).getRegion();
                    tmpAdd = tmp.Union2(tmpAdd);
                    break;

                case ROI.NEGATIVE_FLAG:
                    tmp     = ((ROI)ROIList[i]).getRegion();
                    tmpDiff = tmp.Union2(tmpDiff);
                    break;

                default:
                    break;
                } //end of switch
            }     //end of for

            ModelROI = null;

            if (tmpAdd.AreaCenter(out row, out col) > 0)
            {
                if (tmpDiff.AreaCenter(out rowDiff, out colDiff) > 0)
                {
                    tmp = tmpAdd.Difference(tmpDiff);
                }
                else
                {
                    tmp = tmpAdd;
                }
                if (tmp.AreaCenter(out row, out col) > 0)
                {
                    ModelROI = tmp;
                }
            }

            //in case the set of positiv and negative ROIs dissolve
            if (ModelROI == null || ROIList.Count == 0)
            {
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Calculates the ModelROI region for all objects contained 
        /// in ROIList, by adding and subtracting the positive and 
        /// negative ROI objects.
        /// </summary>
        public bool defineModelROI()
        {
            HRegion tmpAdd, tmpDiff, tmp;
            double row, col;

            if (stateROI == MODE_ROI_NONE)
                return true;

            tmpAdd = new HRegion();
            tmpDiff = new HRegion();
            tmpAdd.GenEmptyRegion();
            tmpDiff.GenEmptyRegion();

            for (int i = 0; i < ROIList.Count; i++)
            {
                switch (((ROI)ROIList[i]).getOperatorFlag())
                {
                    case ROI.POSITIVE_FLAG:
                        tmp = ((ROI)ROIList[i]).getRegion();
                        tmpAdd = tmp.Union2(tmpAdd);
                        break;
                    case ROI.NEGATIVE_FLAG:
                        tmp = ((ROI)ROIList[i]).getRegion();
                        tmpDiff = tmp.Union2(tmpDiff);
                        break;
                    default:
                        break;
                }//end of switch
            }//end of for

            ModelROI = null;

            if (tmpAdd.AreaCenter(out row, out col) > 0)
            {
                tmp = tmpAdd.Difference(tmpDiff);
                if (tmp.AreaCenter(out row, out col) > 0)
                    ModelROI = tmp;
            }

            //in case the set of positiv and negative ROIs dissolve
            if (ModelROI == null || ROIList.Count == 0)
                return false;

            return true;
        }
Example #17
0
        /// <summary>
        /// 该接口用于框架
        /// </summary>
        /// <returns></returns>
        public ToolResult GetResult()
        {
            HiPerfTimer timer = new HiPerfTimer();

            timer.Start();
            try
            {
                HRegion hRegion = new HRegion();
                hRegion.GenEmptyRegion();
                if (region != null)
                {
                    hRegion = region.Connection();
                    if (info.IsUseArea)
                    {
                        hRegion = hRegion.SelectShape("area", "and", info.MinArea, info.MaxArea);
                    }
                    if (info.IsUseWidth)
                    {
                        hRegion = hRegion.SelectShape("width", "and", info.MinWidth, info.MaxWidth);
                    }
                    if (info.IsUseHeight)
                    {
                        hRegion = hRegion.SelectShape("height", "and", info.MinHeight, info.MaxHeight);
                    }
                    if (info.IsUseRetanglarity)
                    {
                        hRegion = hRegion.SelectShape("rectangularity", "and", info.MinRetanglarity, info.MaxRetanglarity);
                    }
                    if (info.IsUseCircularity)
                    {
                        hRegion = hRegion.SelectShape("circularity", "and", info.MinCircularity, info.MaxCircularity);
                    }
                    if (region != null)
                    {
                        ToolResult result = new ToolResult();
                        result.ResultName = ToolName;
                        result.Region     = region;
                        int area = region.AreaCenter(out double row, out double col);
                        result.ImageX = col;
                        result.ImageY = row;
                        HTuple tuple = region.OrientationRegion();
                        if (tuple.Length > 0)
                        {
                            result.ImageAngle = tuple[0].D;
                        }
                        result.ElapsedTime   = timer.Duration;
                        result.GetResultTime = DateTime.Now;
                        return(result);
                    }
                    else
                    {
                        return(new ToolResult()
                        {
                            ResultName = ToolName, Errormessage = "没有找到合适的区域", GetResultTime = DateTime.Now
                        });
                    }
                }
                return(new ToolResult()
                {
                    ResultName = ToolName, Errormessage = "需要筛选的区域为空区域!", GetResultTime = DateTime.Now
                });
            }
            catch (Exception ex)
            {
                WriteErrorLog("VisionTool", $"{this.ToolName} has occured some mistakes with {ex.ToString()}");
                return(new ToolResult()
                {
                    ResultName = ToolName, Errormessage = ex.ToString(), GetResultTime = DateTime.Now
                });
            }
        }
        /// <summary>
        /// 获取+-运算后的roi轮廓
        /// </summary>
        public bool DefineModelROI()
        {
            HRegion tmpAdd, tmpDiff, tmp;
            double  row, col;

            if (stateROIOperation == ROIOperation.None)  //ROI搜索模式
            {
                return(true);
            }

            tmpAdd  = new HRegion();
            tmpDiff = new HRegion();
            tmpAdd.GenEmptyRegion();
            tmpDiff.GenEmptyRegion();

            for (int i = 0; i < ROIList.Count; i++)
            {
                switch (ROIList[i].OperatorFlag)
                {
                case ROIOperation.Positive:
                    tmp    = ROIList[i].GetRegion();
                    tmpAdd = tmp.Union2(tmpAdd);       //把所有求和模式的ROI Region联合在一起
                    break;

                case ROIOperation.Negative:
                    tmp     = ROIList[i].GetRegion();
                    tmpDiff = tmp.Union2(tmpDiff);      //把所有求差模式的ROI Region联合在一起。
                    break;

                case ROIOperation.Tuya:
                    tmp = ROIList[i].GetRegion();
                    if (tmp != null && tmp.IsInitialized())
                    {
                        tmpDiff = tmp.Union2(tmpDiff);      //把所有求差模式的ROI Region联合在一起。
                    }
                    break;

                default:
                    break;
                } //end of switch
            }     //end of for

            modelROI = null;

            if (tmpAdd.AreaCenter(out row, out col) > 0)
            {
                tmp = tmpAdd.Difference(tmpDiff);
                if (tmp.AreaCenter(out row, out col) > 0) //如果tmpAdd > tmpDiff;
                {
                    modelROI = tmp;                       //把差值赋给modelROI。
                }
            }

            //in case the set of positiv and negative ROIs dissolve
            if (modelROI == null || ROIList.Count == 0)
            {
                return(false);
            }

            return(true);
        }
Example #19
0
        public SurfaceResult SearchSurface(HImage image, SurfaceDefinition definition)
        {
            var swSearchSurface = new NotifyStopwatch("SearchSurface: " + definition.Name);

            if (definition.SaveAllCacheImageEnabled)
            {
//                definition.SaveCacheImageEnabled = true;
                foreach (var part in definition.ExcludeParts)
                {
                    part.SaveCacheImageEnabled = true;
                }
                foreach (var part in definition.IncludeParts)
                {
                    part.SaveCacheImageEnabled = true;
                }
            }

            var surfaceResult = new SurfaceResult()
            {
                Definition = definition.DeepClone(),
            };

            var unionIncludeRegion = new HRegion();
            var unionIncludeDomain = new HRegion();

            unionIncludeRegion.GenEmptyRegion();
            unionIncludeDomain.GenEmptyRegion();

            var unionExcludeRegion = new HRegion();
            var unionExcludeDomain = new HRegion();

            unionExcludeRegion.GenEmptyRegion();
            unionExcludeDomain.GenEmptyRegion();

            foreach (var excludeRegion in definition.ExcludeParts)
            {
                var     sw     = new NotifyStopwatch("excludeRegion.Process: " + excludeRegion.Name);
                HRegion region = excludeRegion.Extract(image);
                sw.Dispose();
                var domain = excludeRegion.Domain;

                unionExcludeRegion = unionExcludeRegion.Union2(region);
                unionExcludeDomain = unionExcludeDomain.Union2(domain);

                if (excludeRegion.SaveCacheImageEnabled)
                {
                    var fileName = "SurfaceDefinition_" + definition.Name + "_Exclude_" + excludeRegion.Name;
                    image.SaveCacheImagesForRegion(domain, region, fileName);
                }

                surfaceResult.ExcludeRegionResults.Add(new RegionResult()
                {
                    SurfaceGroupName = definition.GroupName,
                    SurfaceName      = definition.Name,
                    RegionName       = excludeRegion.Name,
                    Domain           = domain,
                    Region           = region,
                });

                //                    region.Dispose();
                //                    domain.Dispose();
            }

            foreach (var includeRegion in definition.IncludeParts)
            {
                var domain = includeRegion.Domain;
                unionIncludeDomain = unionIncludeDomain.Union2(domain);

                var remainDomain = domain.Difference(unionExcludeRegion);
                var reducedImage = image.ChangeDomain(remainDomain);

                HRegion region;
                using (new NotifyStopwatch("includeRegion.Process: " + includeRegion.Name))
                    region = includeRegion.Extract(reducedImage);
                var remainRegion = region.Difference(unionExcludeRegion);
                unionIncludeRegion = unionIncludeRegion.Union2(remainRegion);

                if (includeRegion.SaveCacheImageEnabled)
                {
                    var fileName = "SurfaceDefinition_" + definition.Name + "_Include_" + includeRegion.Name;
                    //                        _hDevelopExportHelper.HImage.SaveCacheImagesForRegion(domain, remainRegion, fileName);
                    image.SaveCacheImagesForRegion(domain, remainRegion, unionExcludeRegion,
                                                   fileName);
                }

                surfaceResult.IncludeRegionResults.Add(new RegionResult()
                {
                    SurfaceGroupName = definition.GroupName,
                    SurfaceName      = definition.Name,
                    RegionName       = includeRegion.Name,
                    Domain           = domain,
                    Region           = remainRegion,
                });
            }

            if (definition.SaveCacheImageEnabled && definition.IncludeParts.Any())
            {
                var fileName = "SurfaceDefinition_" + definition.Name + "_Include";
                image.SaveCacheImagesForRegion(unionIncludeDomain, unionIncludeRegion,
                                               unionExcludeRegion, fileName);
            }

            if (definition.SaveCacheImageEnabled && definition.ExcludeParts.Any())
            {
                var fileName = "SurfaceDefinition_" + definition.Name + "_Exclude";
                image.SaveCacheImagesForRegion(unionExcludeDomain, unionExcludeRegion,
                                               fileName);
            }

            surfaceResult.ExcludeRegion = unionExcludeRegion;
            surfaceResult.IncludeRegion = unionIncludeRegion;

            swSearchSurface.Dispose();
            return(surfaceResult);
        }
Example #20
0
        public bool FindCircle(HObject img)
        {
            HTuple  Row         = new HTuple();
            HTuple  Col         = new HTuple();
            HTuple  Distance    = new HTuple();
            HTuple  outRow      = new HTuple();
            HTuple  outCol      = new HTuple();
            HTuple  outAmp      = new HTuple();
            HTuple  outDistance = new HTuple();
            HRegion regionCount = new HRegion();

            regionCount.GenEmptyRegion();
            HXLDCont contour = new HXLDCont();

            if (img == null)
            {
                return(false);
            }
            HTuple imgWidth, imgHeight;
            HTuple hms = null;

            HOperatorSet.GetImageSize(img, out imgWidth, out imgHeight);

            double xCenter, yCenter, Center_radius, angle, angleSplit;

            angle = 0;
            //分割角度
            angleSplit    = 2 * 3.1415926 / (Hnum);
            xCenter       = in_col[0].D + data_col;
            yCenter       = in_row[0].D + data_row;
            Center_radius = in_radius[0].D;

            HHomMat2D hom = new HHomMat2D();

            hom.HomMat2dIdentity();
            hom = hom.HomMat2dRotate(angleSplit, yCenter, xCenter);
            double rect_row, rect_col, angleStart, L1, L2;

            //分割矩形的中心坐标
            rect_row = yCenter;
            rect_col = xCenter + Center_radius;
            L1       = HLengh[0].D / 2;
            L2       = Hwid[0].D / 2;
            if (Hdirection != "in2out")
            {
                //由外向内
                angleStart = angle + (new HTuple(180)).TupleRad();
            }
            else
            {
                angleStart = angle;
            }

            for (int i = 0; i < Hnum[0].I; ++i)
            {
                if (m_bShowRectang2)
                {
                    contour.Dispose();
                    contour.GenRectangle2ContourXld(rect_row, rect_col, angleStart + angleSplit * i, L1, L2);
                    HRegion region;
                    region      = contour.GenRegionContourXld("margin");
                    regionCount = regionCount.Union2(region);
                    region.Dispose();
                }
                try
                {//out_row, out_col, out_radius;
                    HOperatorSet.GenMeasureRectangle2(rect_row, rect_col, angleStart + angleSplit * i, L1,
                                                      L2, imgWidth, imgHeight, "nearest_neighbor", out hms);
                    HOperatorSet.MeasurePos(img, hms, Hsomth, Hamp, Htransition, Hselect,
                                            out outRow, out outCol, out outAmp, out outDistance);
                }
                catch { continue; }
                if (Hselect == "all" && outRow.Length > 1)
                {
                    HTuple hIndex = outAmp.TupleSortIndex();
                    int    nMax   = hIndex[hIndex.Length - 1];
                    Row.Append(outRow[nMax]);
                    Col.Append(outCol[nMax]);
                }
                else
                {
                    Row.Append(outRow);
                    Col.Append(outCol);
                }
                hom.AffineTransPixel(rect_row, rect_col, out rect_row, out rect_col);
            }
            HOperatorSet.CloseMeasure(hms);
            if (m_bShowRectang2)
            {
                m_hRectang2.Dispose();
                m_hRectang2 = regionCount.GenContoursSkeletonXld(1, "filter");
            }
            if (Row.Length > Hnum[0].D / 3)
            {
                HXLDCont counter = new HXLDCont();
                counter.GenContourPolygonXld(Row, Col);

                HTuple startphi, endphi;
                HTuple pointorder;
                counter.FitCircleContourXld("atukey", -1, 0, 0, 3, 2,
                                            out out_row, out out_col, out out_radius, out startphi, out endphi, out pointorder);

                HTuple rowDrop = new HTuple();
                HTuple colDrop = new HTuple();
                if (Row.Length - 5 > m_nNumDropPoints && m_nNumDropPoints > 0)
                {
                    HTuple distance = new HTuple();
                    for (int i = 0; i < Row.Length; ++i)
                    {
                        double dis = HMisc.DistancePp(Row[i], Col[i], xCenter, yCenter);
                        distance.Append(Math.Abs(dis - Center_radius));
                    }
                    HTuple index = distance.TupleSortIndex();
                    index = index.TupleInverse();
                    for (int i = 0; i < m_nNumDropPoints; ++i)
                    {
                        int n = index[i];
                        rowDrop.Append(Row[n]);
                        colDrop.Append(Col[n]);
                    }
                    index = index.TupleFirstN(m_nNumDropPoints - 1);
                    Row   = Row.TupleRemove(index);
                    Col   = Col.TupleRemove(index);

                    counter.GenContourPolygonXld(Row, Col);
                    counter.FitCircleContourXld("atukey", -1, 0, 0, 3, 2,
                                                out out_row, out out_col, out out_radius, out startphi, out endphi, out pointorder);
                }
                m_hCircle.Dispose();
                m_centerCross.Dispose();
                if (m_bShowCircle)
                {
                    HOperatorSet.GenCrossContourXld(out m_centerCross, out_row, out_col, (out_radius / 2) + 1, (new HTuple(0)).TupleRad());
                    HOperatorSet.GenCircleContourXld(out m_hCircle, out_row, out_col, out_radius,
                                                     0, 3.1415926 * 2, "positive", 1);
                }
                m_hCross.Dispose();
                if (m_bShowCross)
                {
                    HOperatorSet.GenCrossContourXld(out m_hCross, Row, Col, 17, (new HTuple(45)).TupleRad());
                }

                Result_Array[0] = out_row;
                Result_Array[1] = out_col;
                Result_Array[2] = out_radius;
                return(true);
            }
            return(false);
        }