示例#1
0
        private void WindowControl_Load(object sender, EventArgs e)
        {
            string ImgType;

            Window       = WindowControl.HalconWindow;
            Framegrabber = new HFramegrabber("File", 1, 1, 0, 0, 0, 0, "default",
                                             -1, "default", -1, "default",
                                             "board/board.seq", "default", 1, -1);
            Img = Framegrabber.GrabImage();
            Img.GetImagePointer1(out ImgType, out ImgWidth, out ImgHeight);
            Window.SetPart(0, 0, ImgHeight - 1, ImgWidth - 1);
            Img.DispObj(Window);
            Window.SetDraw("margin");
            Window.SetLineWidth(3);
            Rectangle = new HRegion(188.0, 182, 298, 412);
            Rectangle.AreaCenter(out Row, out Column);
            Rect1Row         = Row - 102;
            Rect1Col         = Column + 5;
            Rect2Row         = Row + 107;
            Rect2Col         = Column + 5;
            RectPhi          = 0;
            RectLength1      = 170;
            RectLength2      = 5;
            this.MouseWheel += my_MouseWheel;
        }
示例#2
0
        public void CreateModel(HImage Img, HWindow window, HWindow window1, double row1, double col1, double row2, double col2, double rowMark, double colMark)
        {
            HRegion rectangle = new HRegion(row1, col1, row2, col2);

            rectangle.AreaCenter(out rowOrg, out colOrg);
            HImage ImgReduced = new HImage();

            //HRegion Rectangle1 = new HRegion();
            //HRegion Rectangle2 = new HRegion();

            //Window.SetColor("red");
            //Window.SetDraw("margin");
            //Window.SetLineWidth(3);
            //Rectangle.GenRectangle1(188.0, 182, 298, 412);
            ImgReduced = Img.ReduceDomain(rectangle);
            ImgReduced.InspectShapeModel(out ModelRegion, 1, 30);

            //Rectangle1.GenRectangle2(Rect1Row, Rect1Col, RectPhi, RectLength1, RectLength2);
            //Rectangle2.GenRectangle2(Rect2Row, Rect2Col, RectPhi, RectLength1, RectLength2);
            ShapeModel = new HShapeModel(ImgReduced, 4, 0, 0,
                                         0, "none", "use_polarity", 30, 10);
            ShapeModel.SetShapeModelOrigin(rowMark - rowOrg, colMark - colOrg);

            //window.SetColor("green");
            //window.SetDraw("margin");
            window.SetPart((int)row1, (int)col1, (int)row2, (int)col2);
            ModelRegion.DispObj(window);
            window1.SetPart((int)row1, (int)col1, (int)row2, (int)col2);
            ImgReduced.DispImage(window1);
            //Window.SetColor("blue");
            //Window.SetDraw("margin");
            //Rectangle1.DispObj(Window);
            //Rectangle2.DispObj(Window);
        }
示例#3
0
 private void AffineTransModelContour(HShapeModel model, HRegion modelRegion)
 {
     if (model != null && modelRegion != null)
     {
         try
         {
             //获取金字塔第一层轮廓
             HXLDCont modelContour = model.GetShapeModelContours(1);
             double   num3, num4;
             modelRegion.AreaCenter(out num3, out num4);
             HHomMat2D matd = new HHomMat2D();
             matd.VectorAngleToRigid(0.0, 0.0, 0.0, num3, num4, 0.0);
             HXLD affterAffineModelXLD = matd.AffineTransContourXld(modelContour);
             if (info.IsShowModelXLD)
             {
                 Window.SetColor("green");
                 affterAffineModelXLD.DispXld(Window.Window);
             }
         }
         catch (Exception e)
         {
             WriteErrorLog("VisionTool", e.ToString());
         }
     }
 }
示例#4
0
文件: MatchingModule.cs 项目: Gz1d/Gz
        public void CreateNccModel(string partName, System.Drawing.Bitmap srcImage, System.Drawing.Rectangle roi)
        {
            try
            {
                int          partIndex = 0;
                NccModelName newModel  = new NccModelName();
                if (m_hTemplate != null)
                {
                    m_hTemplate.Dispose();
                }

                if (m_hoRectangle != null)
                {
                    m_hoRectangle.Dispose();
                }
                m_hoRectangle.GenRectangle1(1.0 * roi.Top, roi.Left, roi.Bottom, roi.Right);
                m_hoRectangle.AreaCenter(out m_roiRow, out m_roiColumn);

                if (srcImage != null)
                {
                    HImage hImage = new HImage();//VisionProcess.Bitmap2HImage_8(srcImage);
                    HImage hImageReduced;

                    hImageReduced = hImage.ReduceDomain(m_hoRectangle);

                    if (CheckPartModelIdIsExist(partName, ref partIndex))
                    {
                        newModel.PartName = partName;
                        newModel.ModelID  = hImageReduced.CreateNccModel(m_iNumLevels, 0, 0, "auto", "use_polarity");
                        m_listPartModelID.RemoveAt(partIndex);
                    }
                    else
                    {
                        newModel.ModelID  = hImageReduced.CreateNccModel("auto", 0, 0, "auto", "use_polarity");
                        newModel.PartName = partName;
                    }
                    m_listPartModelID.Add(newModel);
                }
            }
            catch
            {
                //LogFile.AppendText("CreateNccModel函数Exception:" + ex.Message.ToString());
                //System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
示例#5
0
        public bool CreateShapeModelAct(HImage modelImage)
        {
            if (modelImage == null || !modelImage.IsInitialized())
            {
                return(false);
            }
            this.modelImage = modelImage;

            if (modelRegion == null || !modelRegion.IsInitialized())
            {
                return(false);
            }
            try
            {
                HImage ROIImage = new HImage();
                ROIImage.GenEmptyObj();
                ROIImage = modelImage.ReduceDomain(modelRegion);
                HImage temp = ROIImage.GaussFilter(7);
                ROIImage.Dispose();
                ROIImage    = temp;
                hShapeModel = ROIImage.CreateScaledShapeModel(
                    new HTuple(numLevels),
                    angleStart, angleExtent, new HTuple(angleStep),
                    scaleMin, scaleMax, new HTuple(scaleStep),
                    new HTuple(optimization).TupleConcat("no_pregeneration"),
                    metric,
                    ((new HTuple(contrastLow)).TupleConcat(contrastHigh)).TupleConcat(minLength),
                    new HTuple(minContrast));



                ROIImage.Dispose();
                double row, col;
                modelRegion.AreaCenter(out row, out col);
                refCoordinates   = new HTuple(row, col, 0);
                createNewModelID = false;
                //初始化modelContours
                if (modelXLDCont == null)
                {
                    modelXLDCont = new HXLDCont();
                }
                if (modelXLDCont != null && modelXLDCont.IsInitialized())
                {
                    modelXLDCont.Dispose();
                }
                modelXLDCont = hShapeModel.GetShapeModelContours(1);
            }
            catch
            {
                hShapeModel      = null;
                createNewModelID = true;
                return(false);
            }

            return(true);
        }
示例#6
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);
        }
        public void DisplayImage(HObject imageObj)
        {
            if (imageObj == null) return;

            HImage obj = imageObj as HImage;
            if (obj != null)
            {
                bool isImageValid = false;
                try
                {
                    int channelCount = obj.CountChannels();
                    isImageValid = channelCount > 0;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                if (isImageValid)
                {
                    int h;
                    int w;
                    string s;
                    obj.GetImagePointer1(out s, out w, out h);
                    imageUsed = obj;
                    if (imageHeight == h && imageWidth == w)
                    {
                        Repaint();
                        if (ImageChanged != null)
                            ImageChanged(this);
                        return;
                    }
                    HRegion domain = obj.GetDomain();
                    HTuple r;
                    HTuple c;
                    int area = domain.AreaCenter(out r, out c);


                    if (area == (w * h))
                    {
                        if ((h != imageHeight) || (w != imageWidth))
                        {
                            imageHeight = h;
                            imageWidth = w;
                            SetImagePart(0, 0, h, w);
                            ZoomImage(0, 0, 1);
                        }
                    }
                }
            }
            Repaint();
            if (ImageChanged != null)
                ImageChanged(this);
        }
        /// <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);
        }
        private void hWindowControlWPF1_HInitWindow(object sender, EventArgs e)
        {
            // Get HALCON window from control. Due to restrictions of WPF
            // interoperating with native Windows, this is not yet available
            // in the Loaded event of the control or the parent window. The
            // best time to extract the HALCON window is in this event.
            Window = hWindowControlWPF1.HalconWindow;

            // Attention: Technically we are called by the control during its
            // initialization. Under now account should an exception be allowed
            // to propagate upwards from here (under Windows 7, not even the
            // Visual Studio debugger will function properly in this case).

            try
            {
                // Initialize enabled states
                CreateBtn.IsEnabled = true;
                StartBtn.IsEnabled  = false;
                StopBtn.IsEnabled   = false;

                // Create a timer for execution loop;
                Timer          = new DispatcherTimer();
                Timer.Interval = new TimeSpan(10);
                Timer.Tick    += new EventHandler(Timer_Tick);

                // Prepare image processing
                string ImgType;
                Framegrabber = new HFramegrabber("File", 1, 1, 0, 0, 0, 0, "default",
                                                 -1, "default", -1, "default",
                                                 "board/board.seq", "default", 1, -1);
                Img = Framegrabber.GrabImage();
                Img.GetImagePointer1(out ImgType, out ImgWidth, out ImgHeight);
                Window.SetPart(0, 0, ImgHeight - 1, ImgWidth - 1);
                Img.DispObj(Window);
                Window.SetDraw("margin");
                Window.SetLineWidth(3);
                Rectangle = new HRegion(188.0, 182, 298, 412);
                Rectangle.AreaCenter(out Row, out Column);
                Rect1Row    = Row - 102;
                Rect1Col    = Column + 5;
                Rect2Row    = Row + 107;
                Rect2Col    = Column + 5;
                RectPhi     = 0;
                RectLength1 = 170;
                RectLength2 = 5;
            }
            catch (Exception ex)
            {
                // Catch all
                MessageBox.Show("Error in HInitWindow:" + ex.ToString());
            }
        }
示例#10
0
        /// <summary>
        /// Finds the white & black pattern area in the calibration document.
        /// </summary>
        /// <param name="image1">The image1.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static HImage FindPatternArea(HImage image1)
        {
            HImage reducedImage = new HImage();

            try
            {
                double row;
                double column;
                double phi;
                double length1;
                double length2;

                HRegion regions = image1.Threshold(Properties.Settings.Default.CalibDocumentThresholdMinimumLimit,
                                                   Properties.Settings.Default.CalibDocumentThresholdMaximumLimit);

                int areaOfWhiteRegion = regions.AreaCenter(out row, out column);
                // If the segmented white region does not have the specified area, return from the function
                if (areaOfWhiteRegion < Properties.Settings.Default.CalibDocumentWhiteRegionMinimumArea)
                {
                    return(null);
                }
                regions.SmallestRectangle2(out row, out column, out phi, out length1, out length2);

                HRegion rectangleRegion = new HRegion();
                rectangleRegion.GenRectangle2(row, column, phi, length1, length2);
                reducedImage = image1.ReduceDomain(rectangleRegion);
            }
            catch (Exception ex)
            {
                string errorMessage = "Exception occurred in FindPatternArea. ";

                errorMessage = errorMessage + " Error Message: " + ex.Message;

                MessageBox.Show(errorMessage, "Camera Setup Tool", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return(reducedImage);
        }
示例#11
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);
        }
        /// <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);
        }
示例#13
0
        public bool FindDataCode(HImage img, HRegion region, bool train = false)
        {
            if (!region.IsInitialized())
            {
                region = img.GetDomain();
            }

            if (!train)
            {
                Display(0, region);
            }


            HImage reduceImg = img.ReduceDomain(region);
            HImage searchImg = reduceImg.CropDomain();

            reduceImg.Dispose();

            int width, height;

            searchImg.GetImageSize(out width, out height);

            HImage zoomImg = searchImg.ZoomImageFactor(DateCodeZoomImg, DateCodeZoomImg, "constant");

            DataCodeContour.Dispose();
            searchImg.Dispose();

            HTuple ResultHandles, DecodedDataStrings;

            if (train)
            {
                DataCodeContour = CodeReaderHand.FindDataCode2d(zoomImg, "train", "all", out ResultHandles, out DecodedDataStrings);
            }
            else
            {
                DataCodeContour = CodeReaderHand.FindDataCode2d(zoomImg, "stop_after_result_num", 1, out ResultHandles, out DecodedDataStrings);
            }


            if (DecodedDataStrings.Length > 0)
            {
                DataCodeString = DecodedDataStrings[0].S;

                HHomMat2D mat = new HHomMat2D();
                HHomMat2D scalMat = mat.HomMat2dScale(1 / DateCodeZoomImg, 1 / DateCodeZoomImg, 0.0, 0.0);
                double    row, col;
                region.AreaCenter(out row, out col);
                HHomMat2D tranMat = scalMat.HomMat2dTranslate(row - height / 2.0, col - width / 2.0);

                HXLDCont tranDataCodeContour = tranMat.AffineTransContourXld(DataCodeContour);
                DataCodeContour.Dispose();
                DataCodeContour = tranDataCodeContour;

                if (!train)
                {
                    Display(2);
                    Display(3);
                }
            }
            else
            {
                if (UseBrighten)
                {
                    HImage brightenImg = zoomImg.ScaleImage(UseBrightenValue, 0.0);
                    DataCodeContour = CodeReaderHand.FindDataCode2d(brightenImg, "stop_after_result_num", 1, out ResultHandles, out DecodedDataStrings);
                    if (DecodedDataStrings.Length > 0)
                    {
                        DataCodeString = DecodedDataStrings[0].S;

                        HHomMat2D mat = new HHomMat2D();
                        HHomMat2D scalMat = mat.HomMat2dScale(1 / DateCodeZoomImg, 1 / DateCodeZoomImg, 0.0, 0.0);
                        double    row, col;
                        region.AreaCenter(out row, out col);
                        HHomMat2D tranMat = scalMat.HomMat2dTranslate(row - height / 2.0, col - width / 2.0);

                        HXLDCont tranDataCodeContour = tranMat.AffineTransContourXld(DataCodeContour);
                        DataCodeContour.Dispose();
                        DataCodeContour = tranDataCodeContour;
                        if (!train)
                        {
                            Display(2);
                            Display(3);
                        }

                        return(true);
                    }
                }



                DataCodeString = string.Empty;

                if (!train)
                {
                    Display(4);
                }
                return(false);
            }



            return(true);
        }
示例#14
0
        public bool CreateMatchTool()
        {
            HImage  modelImage  = null;
            HRegion modelRegion = null;

            if (info.ModelROIParam.GetType() == typeof(CircleParam))
            {
                CircleParam circle = info.ModelROIParam as CircleParam;
                if (circle != null)
                {
                    modelImage = GetModelImageByCircle(Image.CopyImage(), circle.CircleRow, circle.CircleColumn, circle.Radius, out modelRegion);
                }
            }
            else if (info.ModelROIParam.GetType() == typeof(Rectangle1Param))
            {
                Rectangle1Param rectangle1 = info.ModelROIParam as Rectangle1Param;
                if (rectangle1 != null)
                {
                    modelImage = GetModelImageByRectangle1(Image.CopyImage(), rectangle1.RectangleStartRow, rectangle1.RectangleStartColumn, rectangle1.RectangleEndRow, rectangle1.RectangleEndColumn, out modelRegion);
                }
            }
            else if (info.ModelROIParam.GetType() == typeof(Rectangle2Param))
            {
                Rectangle2Param rectangle2 = info.ModelROIParam as Rectangle2Param;
                if (rectangle2 != null)
                {
                    modelImage = GetModelImageByRectangle2(Image.CopyImage(), rectangle2.Rectangle2CenterRow, rectangle2.Retangle2CenterColumn, rectangle2.Retangle2Angle, rectangle2.Rectangle2Length1, rectangle2.Rectangle2Length2, out modelRegion);
                }
            }
            else if (info.ModelROIParam.GetType() == typeof(EllipseParam))
            {
                EllipseParam ellipse = info.ModelROIParam as EllipseParam;
                if (ellipse != null)
                {
                    modelImage = GetModelImageByEllipse(Image.CopyImage(), ellipse.EllipseCenterRow, ellipse.EllipseCenterColumn, ellipse.EllipseAngle, ellipse.EllipseRadius1, ellipse.EllipseRadius2, out modelRegion);
                }
            }
            else
            {
                WriteErrorLog("VisionTool", info.ToolName + "模板创建失败,原因是截取图像ROI参数类型不正确");
                return(false);
            }
            HNCCModel nccModel = new HNCCModel();
            double    modelRow, modelCol;

            if (modelImage != null)
            {
                try
                {
                    nccModel.CreateNccModel(modelImage, new HTuple(info.NumLevels), TransAngle.AngleToHu(info.AngleStart), TransAngle.AngleToHu(info.AngleExtent), new HTuple(TransAngle.AngleToHu(info.AngleStep)), info._Metric.ToString());
                    modelRegion.AreaCenter(out modelRow, out modelCol);
                    info.ModelRegionRow   = modelRow;
                    info.ModelRegionCol   = modelCol;
                    info.ModelRegionAngle = 0.0;
                    this.NCCModel         = nccModel;
                    SaveModelFile(info.ToolName);
                }
                catch
                {
                    WriteErrorLog("VisionTool", info.ToolName + "模板创建失败,原因是调用Halcon对象创建函数出错");
                    return(false);
                }
            }
            else
            {
                WriteErrorLog("VisionTool", info.ToolName + "模板创建失败,原因是模板图像对象为空");
                return(false);
            }
            return(true);
        }
示例#15
0
        public bool CreateMatchTool()
        {
            HImage  modelImage  = null;
            HRegion modelRegion = null;

            if (info.ModelROIParam.GetType() == typeof(CircleParam))
            {
                CircleParam circle = info.ModelROIParam as CircleParam;
                if (circle != null)
                {
                    modelImage = GetModelImageByCircle(Image.CopyImage(), circle.CircleRow, circle.CircleColumn, circle.Radius, out modelRegion);
                }
            }
            else if (info.ModelROIParam.GetType() == typeof(Rectangle1Param))
            {
                Rectangle1Param rectangle1 = info.ModelROIParam as Rectangle1Param;
                if (rectangle1 != null)
                {
                    modelImage = GetModelImageByRectangle1(Image.CopyImage(), rectangle1.RectangleStartRow, rectangle1.RectangleStartColumn, rectangle1.RectangleEndRow, rectangle1.RectangleEndColumn, out modelRegion);
                }
            }
            else if (info.ModelROIParam.GetType() == typeof(Rectangle2Param))
            {
                Rectangle2Param rectangle2 = info.ModelROIParam as Rectangle2Param;
                if (rectangle2 != null)
                {
                    modelImage = GetModelImageByRectangle2(Image.CopyImage(), rectangle2.Rectangle2CenterRow, rectangle2.Retangle2CenterColumn, rectangle2.Retangle2Angle, rectangle2.Rectangle2Length1, rectangle2.Rectangle2Length2, out modelRegion);
                }
            }
            else if (info.ModelROIParam.GetType() == typeof(EllipseParam))
            {
                EllipseParam ellipse = info.ModelROIParam as EllipseParam;
                if (ellipse != null)
                {
                    modelImage = GetModelImageByEllipse(Image.CopyImage(), ellipse.EllipseCenterRow, ellipse.EllipseCenterColumn, ellipse.EllipseAngle, ellipse.EllipseRadius1, ellipse.EllipseRadius2, out modelRegion);
                }
            }
            else
            {
                WriteErrorLog("VisionTool", info.ToolName + "模板创建失败");
                return(false);
            }
            HShapeModel shapeScaleModel = new HShapeModel();
            double      modelRow, modelCol;

            if (modelImage != null)
            {
                try
                {
                    shapeScaleModel.CreateScaledShapeModel(modelImage, new HTuple(info.NumLevels), TransAngle.AngleToHu(info.AngleStart), TransAngle.AngleToHu(info.AngleExtent), new HTuple(info.AngleStep), info.ScaleMin, info.ScaleMax, new HTuple(info.ScaleStep), new HTuple(info._Optimization.ToString()), info._Metric.ToString(), new HTuple(info.Contrast), new HTuple(info.MinContrast));
                    AffineTransModelContour(shapeScaleModel, modelRegion);
                    //record the model coordinate to the setting info.
                    modelRegion.AreaCenter(out modelRow, out modelCol);
                    info.ModelRegionRow   = modelRow;
                    info.ModelRegionCol   = modelCol;
                    info.ModelRegionAngle = 0.0;

                    this.ShapeModel = shapeScaleModel;
                    this.ModelXLD   = shapeScaleModel.GetShapeModelContours(1);
                    SaveModelFile(info.ToolName);
                }
                catch
                {
                    WriteErrorLog("VisionTool", info.ToolName + "模板创建失败");
                    return(false);
                }
            }
            else
            {
                WriteErrorLog("VisionTool", info.ToolName + "模板创建失败");
                return(false);
            }
            return(true);
        }
示例#16
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;
        }