Пример #1
0
 private void btn_loadImg_Click(object sender, EventArgs e)
 {
     if (mPicBox1.LoadImg())
     {
         roi.Attach(mPicBox1.Image);
         btn_action.Visible     = true;
         btn_learn.Visible      = true;
         btn_loadMoudle.Visible = true;
         btn_save.Visible       = true;
         btn_process.Visible    = true;
         ckbox_inRoi.Visible    = true;
         copyImg = new EImageBW8();
         copyImg.SetSize(mPicBox1.Image);
         EasyImage.Copy(mPicBox1.Image, copyImg);
     }
 }
Пример #2
0
        public void SaveEMatcher_Blank(
            PatternMatcherParameters param,
            string pattern1_FilePath,
            string pattern2_FilePath,
            string pattern1_ImageFilePath,
            string pattern2_ImageFilePath)
        {
            EImageBW8 blackImage = new EImageBW8(512, 512);

            EasyImage.Oper(EArithmeticLogicOperation.Copy, new EBW8(0), blackImage);   // make it black
            EROIBW8 blackEROI = new EROIBW8();

            blackEROI.OrgX   = 0;
            blackEROI.OrgY   = 0;
            blackEROI.Width  = 512;
            blackEROI.Height = 512;
            blackEROI.Attach(blackImage);
            TeachAndSaveEMatcher(
                param,
                blackEROI,
                blackEROI,
                pattern1_FilePath,
                pattern2_FilePath,
                pattern1_ImageFilePath,
                pattern2_ImageFilePath);
        }
Пример #3
0
        public bool LoadImage(string filePath)
        {
            bool reSuccess = false;

            try
            {
                InputImage.Load(filePath);
                trainROI.Attach(InputImage);
                reSuccess = true;
            }
            catch (EException exc)
            {
                StackFrame[] stackFrames = new StackTrace(true).GetFrames();
                clsLogFile.LogTryCatch(stackFrames, exc.Message, true, true);
            }
            catch (Exception ex)
            {
                StackFrame[] stackFrames = new StackTrace(true).GetFrames();
                clsLogFile.LogTryCatch(stackFrames, ex.Message, true, true);
            }
            return(reSuccess);
        }
Пример #4
0
        public override bool Run(bool isTaskRun = false)
        {
            int index = objConfig.SelectImgIndex - 1;

            if (index < 0 || index >= ParentTask.Imgs.Count)
            {
                InputImg = null;
                count    = 0;
                return(false);
            }
            InputImg = ParentTask.Imgs[index].Img;
            if (InputImg == null || InputImg.IsVoid)
            {
                count = 0;
                return(false);
            }
            if (objConfig.WhiteEncode && !ImageEncoder.GrayscaleSingleThresholdSegmenter.WhiteLayerEncoded)
            {
                ImageEncoder.GrayscaleSingleThresholdSegmenter.BlackLayerEncoded = false;
                ImageEncoder.GrayscaleSingleThresholdSegmenter.WhiteLayerEncoded = true;
            }
            else if (!objConfig.WhiteEncode && ImageEncoder.GrayscaleSingleThresholdSegmenter.WhiteLayerEncoded)
            {
                ImageEncoder.GrayscaleSingleThresholdSegmenter.BlackLayerEncoded = true;
                ImageEncoder.GrayscaleSingleThresholdSegmenter.WhiteLayerEncoded = false;
            }
            if (objConfig.UseRoi)
            {
                Roi.Comment = "";
                if (GetValueFromLinkStr(objConfig.LinkOrgX, out linkorgx))
                {
                    objConfig.Roi_OrgX = (int)linkorgx + objConfig.LinkOffsetOrgX;
                }
                if (GetValueFromLinkStr(objConfig.LinkOrgY, out linkorgy))
                {
                    objConfig.Roi_OrgY = (int)linkorgy + objConfig.LinkOffsetOrgY;
                }
                if (GetValueFromLinkStr(objConfig.LinkWidth, out linkwidth))
                {
                    objConfig.Roi_Width = (int)linkwidth + objConfig.LinkOffsetWidth;
                }
                if (GetValueFromLinkStr(objConfig.LinkHeight, out linkheight))
                {
                    objConfig.Roi_Height = (int)linkheight + objConfig.LinkOffsetHeight;
                }

                Roi.SetPlacement(objConfig.Roi_OrgX, objConfig.Roi_OrgY, objConfig.Roi_Width, objConfig.Roi_Height);
                if (Roi.Width > InputImg.Width)
                {
                    Roi.Width = InputImg.Width;
                }
                if (Roi.Height > InputImg.Height)
                {
                    Roi.Height = InputImg.Height;
                }
                if (Roi.OrgX < 0)
                {
                    Roi.OrgX = 0;
                }
                if (Roi.OrgY < 0)
                {
                    Roi.OrgY = 0;
                }
                if (Roi.OrgX + Roi.Width > InputImg.Width)
                {
                    Roi.OrgX = InputImg.Width - Roi.Width;
                }
                if (Roi.OrgY + Roi.Height > InputImg.Height)
                {
                    Roi.OrgY = InputImg.Height - Roi.Height;
                }

                Roi.Attach(InputImg);
                ImageEncoder.Encode(Roi, CodedImage);
            }
            else
            {
                Roi.Comment = "NoShow";
                ImageEncoder.Encode(InputImg, CodedImage);
            }

            ObjectSelection.Clear();
            ObjectSelection.AddObjects(CodedImage);
            if (objConfig.UseRoi)
            {
                foreach (var item in objConfig.FeatureFilters)
                {
                    switch (item.Feature)
                    {
                    case 3: ObjectSelection.RemoveUsingUnsignedIntegerFeature((EFeature)item.Feature, item.Min, item.Max, EDoubleThresholdMode.Outside);
                        break;

                    case 11:
                    case 12:
                    case 13:
                    case 14:
                    case 15:
                    case 16:
                    case 41:
                        ObjectSelection.RemoveUsingFloatFeature((EFeature)item.Feature, item.Min - Roi.OrgX, item.Max - Roi.OrgY, EDoubleThresholdMode.Outside);
                        break;

                    default:
                        break;
                    }
                }
            }
            else
            {
                foreach (var item in objConfig.FeatureFilters)
                {
                    switch (item.Feature)
                    {
                    case 3: ObjectSelection.RemoveUsingUnsignedIntegerFeature((EFeature)item.Feature, item.Min, item.Max, EDoubleThresholdMode.Outside);
                        break;

                    case 11:
                    case 12:
                    case 13:
                    case 14:
                    case 15:
                    case 16:
                    case 41:
                        ObjectSelection.RemoveUsingFloatFeature((EFeature)item.Feature, item.Min, item.Max, EDoubleThresholdMode.Outside);
                        break;

                    default:
                        break;
                    }
                }
            }
            //排序
            if (objConfig.CanSort)
            {
                int feature = 0;
                switch (objConfig.SortIndex)
                {
                case 0: feature = 3;
                    break;

                case 1: feature = 11;
                    break;

                case 2: feature = 12;
                    break;

                case 3: feature = 41;
                    break;

                case 4: feature = 13;
                    break;

                case 5: feature = 14;
                    break;

                case 6: feature = 15;
                    break;

                case 7: feature = 16;
                    break;

                default:
                    break;
                }
                if (objConfig.IsAscending)
                {
                    ObjectSelection.Sort((EFeature)feature, ESortDirection.Ascending);
                }
                else
                {
                    ObjectSelection.Sort((EFeature)feature, ESortDirection.Descending);
                }
            }
            //输出结果
            ResultData.Name = objConfig.ToolName;
            ResultData.ValueParams.Clear();
            count = ObjectSelection.ElementCount;
            ResultData.ValueParams.Add("斑点数量", new float[] { count });
            if (count > 0)
            {
                area     = new float[count];
                centerX  = new float[count];
                centerY  = new float[count];
                gravityX = new float[count];
                gravityY = new float[count];
                width    = new float[count];
                height   = new float[count];
                angle    = new float[count];
                for (int i = 0; i < count; i++)
                {
                    area[i]     = ObjectSelection.GetUnsignedIntegerFeature(i, EFeature.Area);
                    centerX[i]  = ObjectSelection.GetFloatFeature(i, EFeature.BoundingBoxCenterX);
                    centerY[i]  = ObjectSelection.GetFloatFeature(i, EFeature.BoundingBoxCenterY);
                    gravityX[i] = ObjectSelection.GetFloatFeature(i, EFeature.GravityCenterX);
                    gravityY[i] = ObjectSelection.GetFloatFeature(i, EFeature.GravityCenterY);
                    width[i]    = ObjectSelection.GetFloatFeature(i, EFeature.BoundingBoxWidth);
                    height[i]   = ObjectSelection.GetFloatFeature(i, EFeature.BoundingBoxHeight);
                    angle[i]    = ObjectSelection.GetFloatFeature(i, EFeature.EllipseAngle);
                    if (objConfig.UseRoi)
                    {
                        centerX[i]  += objConfig.Roi_OrgX;
                        gravityX[i] += objConfig.Roi_OrgX;

                        centerY[i]  += objConfig.Roi_OrgY;
                        gravityY[i] += objConfig.Roi_OrgY;
                    }
                }

                ResultData.ValueParams.Add("斑点面积", area);
                ResultData.ValueParams.Add("斑点中心X", centerX);
                ResultData.ValueParams.Add("斑点中心Y", centerY);
                ResultData.ValueParams.Add("斑点重心X", gravityX);
                ResultData.ValueParams.Add("斑点重心Y", gravityY);
                ResultData.ValueParams.Add("斑点宽度", width);
                ResultData.ValueParams.Add("斑点高度", height);
                ResultData.ValueParams.Add("斑点角度", angle);
            }
            if (isTaskRun)
            {
                ParentTask.ResultDatas.Add(ResultData);
            }

            //判定
            bool result = true;

            foreach (var item in objConfig.ResultsDetermines)
            {
                if (item.Enable)
                {
                    switch (item.Name)
                    {
                    case "数量":
                        if (count < item.Min || count > item.Max)
                        {
                            result = false;
                        }
                        break;

                    case "面积":
                        for (int i = 0; i < count; i++)
                        {
                            if (area[i] < item.Min || area[i] > item.Max)
                            {
                                result = false;
                            }
                        }
                        break;

                    case "重心X":
                        for (int i = 0; i < count; i++)
                        {
                            if (gravityX[i] < item.Min || gravityX[i] > item.Max)
                            {
                                result = false;
                            }
                        }
                        break;

                    case "重心Y":
                        for (int i = 0; i < count; i++)
                        {
                            if (gravityY[i] < item.Min || gravityY[i] > item.Max)
                            {
                                result = false;
                            }
                        }
                        break;

                    case "宽度":
                        for (int i = 0; i < count; i++)
                        {
                            if (width[i] < item.Min || width[i] > item.Max)
                            {
                                result = false;
                            }
                        }
                        break;

                    case "高度":
                        for (int i = 0; i < count; i++)
                        {
                            if (height[i] < item.Min || height[i] > item.Max)
                            {
                                result = false;
                            }
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            Roi.Title = Config.ToolName;
            Roi.Date  = result ? "" : "Red";
            return(result);
        }
        public static MapDataFromVision ReadImage(
            MapVisionParameters recipe,
            EImageBW8 eImage)
        {
            if (recipe == null)
            {
                throw new ArgumentNullException("recipe");
            }
            if (eImage == null)
            {
                throw new ArgumentNullException("eImage");
            }

            int   rowCount       = recipe.RowCount;
            int   columnCount    = recipe.ColumnCount;
            float startingPointX = recipe.StartingPointX;
            float startingPointY = recipe.StartingPointY;
            float mapWidth       = recipe.DieWidth;
            float mapHeight      = recipe.DieHeight;
            List <List <BDMMapFromVision> > MapFromVisionListofList = new List <List <BDMMapFromVision> >();

            for (int i = 0; i < rowCount; i++)
            {
                List <BDMMapFromVision> MapFromVisionList = new List <BDMMapFromVision>();
                for (int j = 0; j < columnCount; j++)
                {
                    EROIBW8 currentROI = new EROIBW8();
                    currentROI.Attach(eImage);
                    float ROIpositionX = startingPointX + j * mapWidth;
                    float ROIpositionY = startingPointY + i * mapHeight;
                    currentROI.OrgX   = (int)Math.Round(ROIpositionX);
                    currentROI.OrgY   = (int)Math.Round(ROIpositionY);
                    currentROI.Width  = (int)Math.Round(mapWidth);
                    currentROI.Height = (int)Math.Round(mapHeight);
                    VisionMapCategory visionMapCategory;

                    // if roi out of image border
                    if (currentROI.OrgX + currentROI.Width >= eImage.Width ||
                        currentROI.OrgY + currentROI.Height >= eImage.Height ||
                        currentROI.OrgX < 0 ||
                        currentROI.OrgY < 0)
                    {
                        visionMapCategory = VisionMapCategory.OutOfBound;
                    }

                    else
                    {
                        visionMapCategory = CalculateMapCategory(currentROI, recipe);
                    }

                    BDMMapFromVision currentMap = new BDMMapFromVision()
                    {
                        mapCategory = visionMapCategory,
                        PointX      = currentROI.OrgX,
                        PointY      = currentROI.OrgY,
                        Width       = mapWidth,
                        Height      = mapHeight,
                    };
                    MapFromVisionList.Add(currentMap);
                }
                MapFromVisionListofList.Add(MapFromVisionList);
            }

            return(new MapDataFromVision()
            {
                MapsFromVision = MapFromVisionListofList,
            });
        }