Example #1
0
        /// <summary>
        /// Test the neighborhood.
        /// </summary>
        private void NeighborhoodTest()
        {
            HImage  image            = new HImage("byte", 512, 512);
            HRegion regionLine       = new HRegion();
            HRegion regionComplement = new HRegion();
            HRegion regionConnected  = new HRegion();
            HTuple  hv_DefWindow     = new HTuple();
            HWindow HalconWindow1    = HalconWindowControl1.HalconWindow;
            HWindow HalconWindow2    = HalconWindowControl2.HalconWindow;

            try
            {
                HalconWindow1.SetColored(6);
                HalconWindow2.SetColored(6);
                int numObjects;

                image.GenImageGrayRamp(0, 0, 128, 256, 256, 512, 512);

                // Display code
                image.DispImage(HalconWindow1);
                image.DispImage(HalconWindow2);

                regionLine.GenRegionLine(100, -1, 150, 512);
                regionComplement = regionLine.Complement();

                HOperatorSet.SetSystem("neighborhood", 4);
                regionConnected = regionComplement.Connection();
                image.DispImage(HalconWindow1);
                regionConnected.DispRegion(HalconWindow1);

                numObjects  = regionConnected.CountObj();
                label1.Text = "Number of objects 4-connected:" + numObjects.ToString();

                regionConnected.Dispose();
                HOperatorSet.SetSystem("neighborhood", 8);
                regionConnected = regionComplement.Connection();
                image.DispImage(HalconWindow2);
                regionConnected.DispRegion(HalconWindow2);

                numObjects  = regionConnected.CountObj();
                label2.Text = "Number of objects 8-connected:" + numObjects.ToString();

                HOperatorSet.CloseWindow(HDevWindowStack.Pop());
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                image.Dispose();
                regionLine.Dispose();
                regionConnected.Dispose();
                regionComplement.Dispose();
            }
        }
Example #2
0
        /// <summary>
        /// Action for this instance.
        /// </summary>
        private void Action()
        {
            // Local iconic variables
            HObject ho_ImageGrayRamp, ho_RegionLines, ho_RegionComplement;
            HObject ho_ConnectedRegions;

            // Local control variables
            HTuple hv_Number = null;

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_ImageGrayRamp);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_RegionComplement);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            ho_ImageGrayRamp.Dispose();
            HOperatorSet.GenImageGrayRamp(out ho_ImageGrayRamp, 0, 0, 128, 256, 256, 512, 512);
            HImage ImageGrayRamp = new HImage(ho_ImageGrayRamp);

            // Cast using as operator

            // Traditional cast
            ho_RegionLines.Dispose();
            HOperatorSet.GenRegionLine(out ho_RegionLines, 100, -1, 150, 512);
            HRegion RegionLines = new HRegion(ho_RegionLines);

            ho_RegionComplement.Dispose();
            HOperatorSet.Complement(ho_RegionLines, out ho_RegionComplement);
            HRegion RegionComplement = new HRegion(ho_RegionComplement);

            HOperatorSet.SetSystem("neighborhood", 4);
            ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_RegionComplement, out ho_ConnectedRegions);
            HRegion ConnectedRegions = new HRegion(ho_ConnectedRegions);

            ImageGrayRamp.DispImage(HalconWindow1);
            ConnectedRegions.DispRegion(HalconWindow1);
            RegionLines.DispRegion(HalconWindow1);

            // Should be two objects
            HOperatorSet.CountObj(ho_ConnectedRegions, out hv_Number);
            label1.Text = "Number of objects 4-connected:" + hv_Number.ToString();

            HOperatorSet.SetSystem("neighborhood", 8);
            ConnectedRegions.Dispose();
            ConnectedRegions = RegionComplement.Connection();

            ImageGrayRamp.DispImage(HalconWindow2);
            ConnectedRegions.DispRegion(HalconWindow2);
            RegionLines.DispRegion(HalconWindow2);

            // Should be a single object
            hv_Number   = ConnectedRegions.CountObj();
            label2.Text = "Number of objects 8-connected:" + hv_Number.ToString();

            ho_ImageGrayRamp.Dispose();
            ho_RegionLines.Dispose();
            ho_RegionComplement.Dispose();
            ho_ConnectedRegions.Dispose();
        }
Example #3
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);
     }
 }
Example #4
0
        private void button2_Click(object sender, EventArgs e)
        {
            sortedRegions = mlpOCR.GetSortedRegions(mlpOCR.image);
            txt           = new System.Windows.Forms.TextBox[sortedRegions.CountObj()];
            HView         = new HWindowControl[sortedRegions.CountObj()];
            //
            int wndSize = 100;
            int c       = sortedRegions.CountObj();

            for (int i = 0; i < c; i++)
            {
                HRegion selectedRegion = sortedRegions.SelectObj(i + 1);

                HView[i] = new HWindowControl();
                HWndCtrl hWndControl = new HWndCtrl(HView[i]);

                HView[i].Size = new System.Drawing.Size(wndSize, wndSize);
                panelCharClass.Controls.Add(HView[i]);
                HView[i].Location = new System.Drawing.Point(15, 30 + (wndSize + 5) * i);

                int     row1, row2;
                int     col1, col2;
                int     imgH, imgW;
                HObject imgO;

                selectedRegion.SmallestRectangle1(out row1, out col1, out row2, out col2);
                mlpOCR.image.GetImageSize(out imgH, out imgW);
                HImage img = selectedRegion.RegionToBin(255, 0, imgH, imgW);
                HOperatorSet.CropPart(img, out imgO, row1, col1, row2, col2);
                //HOperatorSet.GetImageSize(imgO,out imgH, out imgW);


                hWndControl.addIconicVar(imgO);
                hWndControl.repaint();
                //
                txt[i]           = new System.Windows.Forms.TextBox();
                txt[i].Size      = new System.Drawing.Size(wndSize, wndSize);
                txt[i].MaxLength = 1;
                panelCharClass.Controls.Add(txt[i]);
                txt[i].Location = new System.Drawing.Point(15 + wndSize + 5, 30 + (wndSize + 5) * i);
            }
            button1.Enabled = true;
        }
Example #5
0
 private void button1_Click(object sender, EventArgs e)
 {
     for (int i = 0; i < sortedRegions.CountObj(); i++)
     {
         HRegion selectedRegion = sortedRegions.SelectObj(i + 1);
         if (txt[i].Text != "")
         {
             mlpOCR.CreateTrainingFile(selectedRegion, mlpOCR.image, txt[i].Text);
         }
     }
     button3.Enabled = true;
 }
        public static IList <HRegion> ToList(this HRegion region)
        {
            IList <HRegion> list = new List <HRegion>();

            var count = region.CountObj();

            for (int i = 0; i < count; i++)
            {
                list.Add(region[i + 1]);
            }

            return(list);
        }
Example #7
0
        private bool AnalisiLivello(HImage Img, DataType.AlgoritmoControlloLivelloParam param, double centerColumn, bool regolazioni, out bool errorLevelMin, out bool errorLevelMax, out bool errorEmpty, ref ArrayList iconicList)
        {
            bool ret = false;


            errorLevelMin = false;
            errorLevelMax = false;
            errorEmpty    = false;


            int Width, Height;

            Img.GetImageSize(out Width, out Height);

            const string Interpolation = "nearest_neighbor";
            string       Transition    = param.LiquidoChiaro ? "negative" : "positive";
            const string Select        = "first";
            const double delta         = 30.0;

            // Disegna la regione per il calcolo del Livello
            HRegion Rectangle = new HRegion();

            Rectangle.GenRectangle2(param.RectLivello.Row, centerColumn, param.RectLivello.Angle, param.RectLivello.Length1, param.RectLivello.Length2);

            HMeasure MeasureHandle = new HMeasure(param.RectLivello.Row, centerColumn, param.RectLivello.Angle, param.RectLivello.Length1, param.RectLivello.Length2, Width, Height, Interpolation);

            HTuple RowEdge, ColumnEdge, Amplitude, Distance;

            Img.MeasurePos(MeasureHandle, param.SigmaLivellox10 / 10.0, param.ThresholdLivellox10 / 10.0, Transition, Select, out RowEdge, out ColumnEdge, out Amplitude, out Distance);

            MeasureHandle.Dispose();

            if (RowEdge.Length > 0)
            {
                double altezzaSchiuma = RowEdge.D;

                if (altezzaSchiuma >= param.RowMaxLivello && altezzaSchiuma <= param.RowMinLivello)
                {
                    if (param.LiquidoChiaro)
                    {
                        ret = true;
                    }
                    else
                    {
                        HRegion rectangleThreshold = new HRegion();
                        //double row = altezzaSchiuma + (paramLevel.rowMin - altezzaSchiuma) / 2;
                        double row    = altezzaSchiuma + (param.RectLivello.Row + param.RectLivello.Length1 - altezzaSchiuma) / 2;
                        double column = centerColumn;
                        //double length1 = (paramLevel.rowMin - altezzaSchiuma) / 2;
                        double length1 = (param.RectLivello.Row + param.RectLivello.Length1 - altezzaSchiuma) / 2;
                        double length2 = param.RectLivello.Length2;

                        rectangleThreshold.GenRectangle2(row, column, param.RectLivello.Angle, length1, length2);

                        HImage  imgReduced      = Img.ReduceDomain(rectangleThreshold);
                        HRegion regionThreshold = imgReduced.Threshold((double)param.ThresholdMin, (double)param.ThresholdMax);
                        imgReduced.Dispose();

                        double r, c;
                        int    area = rectangleThreshold.AreaCenter(out r, out c);
                        rectangleThreshold.Dispose();

                        HRegion regionSelect = regionThreshold.SelectShape("area", "and", area * 0.7, double.MaxValue);
                        regionThreshold.Dispose();

                        ret = regionSelect.CountObj() == 1;
                        iconicList.Add(new Utilities.ObjectToDisplay(regionSelect, "blue", 3)
                        {
                            DrawMode = "fill"
                        });
                    }
                }
                else if (altezzaSchiuma < param.RowMaxLivello)
                {
                    errorLevelMax = true;
                }
                else if (altezzaSchiuma > param.RowMinLivello)
                {
                    errorLevelMin = true;
                }

                iconicList.Add(new Utilities.ObjectToDisplay("Line", new HTuple(altezzaSchiuma, centerColumn - delta, altezzaSchiuma, centerColumn + delta), ret ? "green" : "red", 3));
            }
            else
            {
                if (param.UseThreshold)
                {
                    if (param.LiquidoChiaro)
                    {
                        HRegion rectangleThreshold = new HRegion();
                        rectangleThreshold.GenRectangle2(param.QuotaControlloVuoto, centerColumn, 0.0, param.LarghezzaControlloVuoto, 20);

                        HImage imgReduced = Img.ReduceDomain(rectangleThreshold);
                        rectangleThreshold.Dispose();

                        HRegion regionThreshold = imgReduced.Threshold((double)param.ThresholdMin, (double)param.ThresholdMax);
                        imgReduced.Dispose();

                        HRegion connectedRegions = regionThreshold.Connection();
                        regionThreshold.Dispose();

                        HRegion filledCandidates = connectedRegions.FillUp();
                        connectedRegions.Dispose();

                        HRegion regionMax = filledCandidates.SelectShapeStd("max_area", 0);

                        double row, col;
                        int    area = regionMax.AreaCenter(out row, out col);
                        regionMax.Dispose();

                        if (area > param.SogliaAreaControlloVuoto)
                        {
                            iconicList.Add(new Utilities.ObjectToDisplay(filledCandidates, "green", 1)
                            {
                                DrawMode = "fill"
                            });
                            ret = true;
                        }
                        else
                        {
                            iconicList.Add(new Utilities.ObjectToDisplay(filledCandidates, "red", 1)
                            {
                                DrawMode = "fill"
                            });
                            ret = false;
                        }

                        iconicList.Add(new Utilities.ObjectToDisplay(area.ToString(), "blue", (int)(row - 50), (int)(col - param.LarghezzaControlloVuoto)));
                    }
                    else
                    {
                        HRegion rectangleThreshold = new HRegion();
                        rectangleThreshold.GenRectangle2(param.RowMaxLivello + (param.RowMinLivello - param.RowMaxLivello) / 2
                                                         , centerColumn
                                                         , param.RectLivello.Angle
                                                         , (param.RowMinLivello - param.RowMaxLivello) / 2
                                                         , param.RectLivello.Length2);

                        HImage  imgReduced      = Img.ReduceDomain(rectangleThreshold);
                        HRegion regionThreshold = imgReduced.Threshold((double)param.ThresholdMin, (double)param.ThresholdMax);
                        imgReduced.Dispose();

                        double r, c;
                        int    area = rectangleThreshold.AreaCenter(out r, out c);

                        HRegion regionSelect = regionThreshold.SelectShape("area", "and", area * 0.7, double.MaxValue);

                        ret = regionSelect.CountObj() == 1;
                        regionSelect.Dispose();

                        iconicList.Add(new Utilities.ObjectToDisplay(rectangleThreshold, "red", 1));
                        iconicList.Add(new Utilities.ObjectToDisplay(regionThreshold, "red", 1)
                        {
                            DrawMode = "fill"
                        });
                    }

                    if (ret == false)
                    {
                        errorEmpty = true;
                    }
                }
            }

            iconicList.Add(new Utilities.ObjectToDisplay(Rectangle, "red", 2));

            iconicList.Add(new Utilities.ObjectToDisplay("Line", new HTuple(param.RowMaxLivello, centerColumn - delta, param.RowMaxLivello, centerColumn + delta), "cyan", 3));
            iconicList.Add(new Utilities.ObjectToDisplay("Max", "cyan", (int)(param.RowMaxLivello - delta), (int)(centerColumn + 2 * delta)));

            iconicList.Add(new Utilities.ObjectToDisplay("Line", new HTuple(param.RowMinLivello, centerColumn - delta, param.RowMinLivello, centerColumn + delta), "cyan", 3));
            iconicList.Add(new Utilities.ObjectToDisplay("Min", "cyan", (int)(param.RowMinLivello), (int)(centerColumn + 2 * delta)));

            return(ret);
        }