Exemple #1
0
        /*==============================================================================================*/

        private bool FindBlob(EImageBW8 inputImage, byte threshold = 128, bool findBlack = true, bool findWhite = false)
        {
            bool reSuccess = false;

            try
            {
                eImageEncoder.GrayscaleSingleThresholdSegmenter.BlackLayerEncoded = findBlack;
                eImageEncoder.GrayscaleSingleThresholdSegmenter.WhiteLayerEncoded = findWhite;
                eImageEncoder.SegmentationMethod = ESegmentationMethod.GrayscaleSingleThreshold;
                eImageEncoder.GrayscaleSingleThresholdSegmenter.Mode = EGrayscaleSingleThreshold.Absolute;
                eImageEncoder.GrayscaleSingleThresholdSegmenter.AbsoluteThreshold = threshold;
                eImageEncoder.Encode(inputImage, Base);
                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);
        }
        private void vehicleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            float PictureBoxSizeRatio, ImageSizeRatio;

            codedImage1ObjectSelection.FeretAngle = 0.00f;
            codedImage1Encoder.GrayscaleSingleThresholdSegmenter.WhiteLayerEncoded = true;
            codedImage1Encoder.GrayscaleSingleThresholdSegmenter.BlackLayerEncoded = false;
            codedImage1Encoder.SegmentationMethod = ESegmentationMethod.GrayscaleSingleThreshold;
            codedImage1Encoder.GrayscaleSingleThresholdSegmenter.Mode = EGrayscaleSingleThreshold.MinResidue;

            OriginalImg1.Load(files[0]);

            /*============================計算scaling ratio============================*/
            PictureBoxSizeRatio = (float)pbImg1.Width / pbImg1.Height;
            ImageSizeRatio      = (float)OriginalImg1.Width / OriginalImg1.Height;
            if (ImageSizeRatio > PictureBoxSizeRatio)
            {
                ScalingRatio = (float)pbImg1.Width / OriginalImg1.Width;
            }
            else
            {
                ScalingRatio = (float)pbImg1.Height / OriginalImg1.Height;
            }
            /*=========================================================================*/

            for (int i = 0; i < FileListBox.Items.Count; i++)
            {
                FileListBox.SelectedIndex = i;
                FileListBox.Refresh();
                OriginalImg1.Load(files[i]);
                OriginalImg1.Draw(pbImg1.CreateGraphics(), ScalingRatio);

                GrayImg1.SetSize(OriginalImg1);
                EasyImage.Oper(EArithmeticLogicOperation.Copy, new EBW8(0), GrayImg1);
                EasyImage.Convert(OriginalImg1, GrayImg1); //轉灰階

                EasyImage.Median(BackgroundGray, BackgroundGray);
                EasyImage.Median(GrayImg1, GrayImg1);
                EasyImage.Oper(EArithmeticLogicOperation.Subtract, GrayImg1, BackgroundGray, GrayImg1);

                EasyImage.Threshold(GrayImg1, GrayImg1, unchecked ((uint)EThresholdMode.MinResidue));

                EasyImage.ErodeBox(GrayImg1, GrayImg1, 1);  //侵蝕
                EasyImage.CloseBox(GrayImg1, GrayImg1, 10); //閉合

                codedImage1ObjectSelection.FeretAngle = 0.00f;
                codedImage1Encoder.Encode(GrayImg1, codedImage1);
                codedImage1ObjectSelection.Clear();
                codedImage1ObjectSelection.AddObjects(codedImage1);
                codedImage1ObjectSelection.AttachedImage = GrayImg1;
                codedImage1ObjectSelection.RemoveUsingUnsignedIntegerFeature(EFeature.RunCount, 1000, ESingleThresholdMode.Less); //移除RunCount小於1000的物件

                if (codedImage1ObjectSelection.ElementCount > 0)
                {
                    Console.WriteLine("(" + codedImage1ObjectSelection.GetElement(0).BoundingBoxCenterX + ", " + codedImage1ObjectSelection.GetElement(0).BoundingBoxCenterY + ")");
                }

                codedImage1.DrawFeature(pbImg1.CreateGraphics(), EDrawableFeature.BoundingBox, codedImage1ObjectSelection, ScalingRatio); // 把車的框框畫出來

                GrayImg1.Draw(pbImg2.CreateGraphics(), ScalingRatio);

                Console.WriteLine(files[i]);
            }
        }
Exemple #3
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);
        }