Exemple #1
0
        HTuple GetGrayHisto(HTuple rectangle1)
        {
            if (HObjList == null || HObjList.Count < 1)
            {
                return(null);
            }
            HImage hv_image = HObjList[0].HObj as HImage;

            if (hv_image != null)
            {
                try
                {
                    HTuple hv_AbsoluteHisto, hv_RelativeHisto;

                    HTuple channel = hv_image.CountChannels();
                    HImage imgTmp  = null;
                    if (channel == 3)
                    {
                        imgTmp = hv_image.Rgb1ToGray();
                    }
                    else
                    {
                        imgTmp = hv_image.Clone();
                    }
                    HRegion region = new HRegion();
                    region.GenRectangle1(rectangle1[0].D, rectangle1[1], rectangle1[2], rectangle1[3]);
                    hv_AbsoluteHisto = imgTmp.GrayHisto(region, out hv_RelativeHisto);
                    return(hv_AbsoluteHisto);
                }
                catch (Exception)
                {
                }
            }
            return(null);
        }
        /// <summary>Gets the HALCON region described by the ROI</summary>
        public override HRegion getRegion()
        {
            HRegion region = new HRegion();

            region.GenRectangle1(row1, col1, row2, col2);
            return(region);
        }
Exemple #3
0
        private void showButton_Click(object sender, EventArgs e)
        {
            clearAllWindow();
            int     Width, Height;
            HRegion _1 = new HRegion();
            HRegion _2 = new HRegion();
            HImage  i1 = bottomView.GrabImageAsync(-1);
            HImage  i2 = bottomView.GrabImageAsync(-1);

            i1 = i1.RotateImage(180.0, "constant");
            i2 = i2.RotateImage(180.0, "constant");
            i1.GetImagePointer1(out _, out Width, out Height);
            updateSetting();
            _1.GenRectangle1(licXi, licYi, licXf, licYf);
            _2.GenRectangle1(conXi, conYi, conXf, conYf);

            mainWindowList[0].SetPart(0, 0, Height, Width);
            mainWindowList[1].SetPart(0, 0, Height, Width);

            mainWindowList[0].DispObj(i1);
            mainWindowList[1].DispObj(i2);
            mainWindowList[0].SetDraw("margin");
            mainWindowList[1].SetDraw("margin");
            mainWindowList[0].SetColor("red");
            mainWindowList[1].SetColor("red");
            mainWindowList[0].DispObj(_1);
            mainWindowList[1].DispObj(_2);
        }
Exemple #4
0
        public HImage SegmentImage()
        {
            HRegion region, connectedRegions, selectedRegions, SortedRegions;
            HRegion regionDilated, tmpRegion;
            HImage  imgReduced;
            HTuple  row1, row2;
            HTuple  col1, col2;

            region = image.Threshold(minGray, maxGray);

            regionDilated    = region.Connection().DilationRectangle1(dilationH, dilationW);
            selectedRegions  = regionDilated.SelectShape(new HTuple("area", "row"), "and", new HTuple(2000, 400), new HTuple(60000, 2200));
            connectedRegions = selectedRegions.Union1().Connection();
            connectedRegions.SmallestRectangle1(out row1, out col1, out row2, out col2);
            tmpRegion = new HRegion();
            tmpRegion.GenRectangle1(row1, col1, row2, col2);
            imgReduced = image.ReduceDomain(tmpRegion.Union1());

            region          = imgReduced.Threshold(12.0, 255.0);
            selectedRegions = region.Connection().SelectShape("area", "and", 1000, 6000);
            imgReduced.GetImageSize(out imgH, out imgW);
            segImage = selectedRegions.ClosingRectangle1(8, 3).Union1().RegionToBin(255, 0, imgH, imgW);

            //SortedRegions = selectedRegions.SortRegion("character", "true", "row");
            return(segImage);
        }// HImage SegmentImage()
Exemple #5
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);
        }
Exemple #6
0
        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());
            }
        }
Exemple #7
0
 public DrawRectangle1(double row1, double column1, double row2, double column2)
 {
     Row1    = row1;
     Column1 = column1;
     Row2    = row2;
     Column2 = column2;
     ROIType = ROIType.Rectangle1;
     Region  = new HRegion();
     Region.GenRectangle1(Row1, Column1, Row2, Column2);
 }
        public HRegion Process(HRegion region)
        {
            HTuple row1, row2, col1, col2;

            region.SmallestRectangle1(out row1, out col1, out row2, out col2);
            var smallestRect1 = new HRegion();

            smallestRect1.GenRectangle1(row1, col1, row2, col2);
            return(smallestRect1);
        }
Exemple #9
0
        public static HTuple getGrayHisto(HObject image, HTuple rectangle1 = null)
        {
            if (image != null)
            {
                try
                {
                    HTuple hv_AbsoluteHisto, hv_RelativeHisto;

                    HOperatorSet.CountChannels(image, out HTuple channel);

                    HObject imgTmp = null;
                    if (channel == 3)
                    {
                        HOperatorSet.Rgb1ToGray(image, out imgTmp);
                    }
                    else
                    {
                        imgTmp = image;
                    }
                    HRegion region = new HRegion();
                    if (rectangle1 == null)
                    {
                        HTuple col, row;
                        HOperatorSet.GetImageSize(imgTmp, out col, out row);
                        region.GenRectangle1(0, 0, row - 1, col - 1);
                    }
                    else
                    {
                        region.GenRectangle1(rectangle1[0].D, rectangle1[1], rectangle1[2], rectangle1[3]);
                    }
                    HOperatorSet.GrayHisto(region, imgTmp, out hv_AbsoluteHisto, out hv_RelativeHisto);
                    return(hv_AbsoluteHisto);
                }
                catch (Exception exc)
                {
                    Wells.FrmType.frm_Log.Log("获取灰度直方图出错:" + exc.Message, 2);
                }
            }
            return(null);
        }
        /// <summary>
        ///  在图像中绘制矩形区域
        /// </summary>
        /// <param name="rowBegin"></param>
        /// <param name="colBegin"></param>
        /// <param name="rowEnd"></param>
        /// <param name="colEnd"></param>
        public void DrawRectangle1(string color, out double rowBegin, out double colBegin, out double rowEnd, out double colEnd)
        {
            try
            {
                Double _rowBegin, _colBegin, _rowEnd, _colEnd;

                ShieldMouseEvent();

                hWindowControl1.Focus();

                #region 无用代码
                //IntPtr handle = (IntPtr)GetFocus();
                //if (handle == null)
                //{
                //    this.FindForm().KeyPreview = true;
                //}
                //else
                //{
                //    Control c = Control.FromHandle(handle);//这就是
                //}
                #endregion

                hWindowControl1.HalconWindow.SetColor(color);
                hWindowControl1.HalconWindow.DrawRectangle1(out _rowBegin, out _colBegin, out _rowEnd, out _colEnd);
                // hv_window.SetColor(color);
                //  hv_window.DrawRectangle1(out _rowBegin, out _colBegin, out _rowEnd, out _colEnd);


                HRegion rectangle = new HRegion();
                rectangle.GenRectangle1(_rowBegin, _colBegin, _rowEnd, _colEnd);
                rectangle.GenContourRegionXld("border").DispObj(hWindowControl1.HalconWindow);

                //  rectangle.DispObj(hWindowControl1.HalconWindow);
                rectangle.Dispose();

                ReloadMouseEvent();

                rowBegin = _rowBegin;
                colBegin = _colBegin;
                rowEnd   = _rowEnd;
                colEnd   = _colEnd;
            }
            catch (System.Exception ex)
            {
                rowBegin            = 0.0;
                colBegin            = 0.0;
                rowEnd              = 0.0;
                colEnd              = 0.0;
                status_message.Text = ex.Message;
            }
        }
Exemple #11
0
        //ROI区域设置
        public void button_Roi_Click(object sender, EventArgs e)
        {
            HOperatorSet.DispObj(getImage, hwindow);

            HTuple ROI_row1, ROI_column1, ROI_row2, ROI_column2 = new HTuple();

            hWindowControl1.Focus();
            HOperatorSet.DrawRectangle1(hWindowControl1.HalconWindow, out ROI_row1, out ROI_column1,
                                        out ROI_row2, out ROI_column2);
            HRegion ROI = new HRegion();

            ROI.GenRectangle1(ROI_row1, ROI_column1, ROI_row2, ROI_column2);

            hWindowControl1.HalconWindow.SetColor("red");
            HOperatorSet.SetDraw(hWindowControl1.HalconWindow, "margin");
            hWindowControl1.HalconWindow.DispObj(ROI);

            //更新xml文件ROI区域坐标。
            double roi_row1_d, roi_column1_d, roi_row2_d, roi_column2_d;
            int    roi_row1, roi_column1, roi_row2, roi_column2;

            roi_row1_d    = ROI_row1;
            roi_column1_d = ROI_column1;
            roi_row2_d    = ROI_row2;
            roi_column2_d = ROI_column2;

            roi_row1    = (int)roi_row1_d;
            roi_column1 = (int)roi_column1_d;
            roi_row2    = (int)roi_row2_d;
            roi_column2 = (int)roi_column2_d;

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load("InputLocateParam.txt");
            XmlNode     rootNode           = xmlDoc.FirstChild;
            XmlNodeList InputParamNodeList = rootNode.ChildNodes;

            foreach (XmlNode InputParamNode in InputParamNodeList)
            {
                if (InputParamNode.Name == "TopPt")
                {
                    InputParamNode.InnerText = roi_row1.ToString();
                    //InputParamNode.InnerText = ROI_row1.ToString();
                }
                else if (InputParamNode.Name == "LeftPt")
                {
                    InputParamNode.InnerText = roi_column1.ToString();
                    //InputParamNode.InnerText = ROI_column1.ToString();
                }
                else if (InputParamNode.Name == "BottomPt")
                {
                    InputParamNode.InnerText = roi_row2.ToString();
                    //InputParamNode.InnerText = ROI_row2.ToString();
                }
                else if (InputParamNode.Name == "RightPt")
                {
                    InputParamNode.InnerText = roi_column2.ToString();
                    //InputParamNode.InnerText = ROI_column2.ToString();
                }
            }
            xmlDoc.Save("InputLocateParam.txt");
        }
 /// <summary>Gets the HALCON region described by the ROI</summary>
 public override HRegion getRegion()
 {
     HRegion region = new HRegion();
     region.GenRectangle1(row1, col1, row2, col2);
     return region;
 }
Exemple #13
0
        private void CaptureButton_Click(object sender, EventArgs e)
        {
            clearAllWindow();
            HTuple  _, height, width, lic, con;
            HRegion licRegion, conRegion;
            HImage  conImage, licImage;

            try
            {
                {
                    licImage = bottomView.GrabImageAsync(-1);
                    licImage = licImage.RotateImage(-90.0, "constant");
                    HRegion ROI = new HRegion();
                    ROI.GenRectangle1(licXi, licYi, licXf, licYf);
                    ocr_NHP_Call.SetInputIconicParamObject("Image", licImage);
                    ocr_NHP_Call.SetInputIconicParamObject("ROI", ROI);
                    ocr_NHP_Call.SetInputCtrlParamTuple("typeChk", "license");
                    ocr_NHP_Call.SetInputCtrlParamTuple("threshold", licThres);
                    ocr_NHP_Call.Execute();
                    licRegion = ocr_NHP_Call.GetOutputIconicParamRegion("Characters");
                    lic       = ocr_NHP_Call.GetOutputCtrlParamTuple("result");
                    charArray2String_Call.SetInputCtrlParamTuple("inputArray", lic);
                    charArray2String_Call.Execute();
                    lic = charArray2String_Call.GetOutputCtrlParamTuple("result");
                }
                {
                    conImage = bottomView.GrabImageAsync(-1);
                    conImage = conImage.RotateImage(-90.0, "constant");
                    HRegion ROI = new HRegion();
                    ROI.GenRectangle1(conXi, conYi, conXf, conYf);
                    ocr_NHP_Call.SetInputIconicParamObject("Image", conImage);
                    ocr_NHP_Call.SetInputIconicParamObject("ROI", ROI);
                    ocr_NHP_Call.SetInputCtrlParamTuple("typeChk", "container");
                    ocr_NHP_Call.SetInputCtrlParamTuple("threshold", conThres);
                    ocr_NHP_Call.Execute();
                    conRegion = ocr_NHP_Call.GetOutputIconicParamRegion("Characters");
                    con       = ocr_NHP_Call.GetOutputCtrlParamTuple("result");
                    charArray2String_Call.SetInputCtrlParamTuple("inputArray", con);
                    charArray2String_Call.Execute();
                    con = charArray2String_Call.GetOutputCtrlParamTuple("result");
                }


                conImage.GetImagePointer1(out _, out width, out height);
                mainWindowList[0].SetPart(0, 0, height.I, width.I);
                mainWindowList[1].SetPart(0, 0, height.I, width.I);


                sideWindowList[0].SetPart(0, 0, height.I, width.I);
                sideWindowList[1].SetPart(0, 0, height.I, width.I);

                mainWindowList[0].DispImage(licImage);
                mainWindowList[1].DispImage(conImage);

                /*lic = programCall.GetCtrlVarTuple("license");
                 * con = programCall.GetCtrlVarTuple("container");
                 */
                license   = licenseLabel;
                container = containerLabel;

                /*
                 * licRegion = programCall.GetIconicVarRegion("Characters");
                 * conRegion = programCall.GetIconicVarRegion("Characters2");
                 */
                sideWindowList[0].SetColored(12);
                sideWindowList[1].SetColored(12);
                sideWindowList[0].DispObj(licRegion);
                sideWindowList[1].DispObj(conRegion);

                license.Text   = lic.S;
                container.Text = con.S;
                t = DateTime.Now;
                String date   = t.ToString("dd-MM-yyyy");
                String timeIn = t.ToString("HH:mm:ss");
                //Console.WriteLine(time);
                //Console.WriteLine(time.Substring(0, 10));
                //Console.WriteLine(time.Substring(0, 7));
                setData(con.S, lic.S, date, timeIn, "");
                uploadToCloud(licImage, conImage, lic.S, con.S);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        public void FindShapeModel(HImage Img, HWindow window, double row1, double col1, double row2, double col2, double rowMark, double colMark)
        {
            double    S1, S2;
            HTuple    RowCheck, ColumnCheck, AngleCheck, Score;
            HHomMat2D Matrix = new HHomMat2D();
            HRegion   ModelRegionTrans;
            HTuple    row1Check, col1Check;
            HTuple    row2Check, col2Check;
            HRegion   Rectangle1 = new HRegion();

            //HRegion           Rectangle2 = new HRegion();
            //HMeasure          Measure1, Measure2;
            //HTuple            RowEdgeFirst1, ColumnEdgeFirst1;
            //HTuple            AmplitudeFirst1, RowEdgeSecond1;
            //HTuple            ColumnEdgeSecond1, AmplitudeSecond1;
            //HTuple            IntraDistance1, InterDistance1;
            //HTuple            RowEdgeFirst2, ColumnEdgeFirst2;
            //HTuple            AmplitudeFirst2, RowEdgeSecond2;
            //HTuple            ColumnEdgeSecond2, AmplitudeSecond2;
            //HTuple            IntraDistance2, InterDistance2;
            //HTuple            MinDistance;
            //int               NumLeads;

            //HSystem.SetSystem("flush_graphic", "false");
            //Img.GrabImage(Framegrabber);
            //Img.DispObj(Window);

            // Find the IC in the current image.
            S1 = HSystem.CountSeconds();
            ShapeModel.FindShapeModel(Img, 0,
                                      new HTuple(360).TupleRad().D,
                                      0.7, 1, 0.5, "least_squares",
                                      4, 0.9, out RowCheck, out ColumnCheck,
                                      out AngleCheck, out Score);
            S2 = HSystem.CountSeconds();
            if (RowCheck.Length == 1)
            {
                //MatchingScoreLabel.Text = "Score: " +
                //    String.Format("{0:F5}", Score.D);
                // Rotate the model for visualization purposes.
                //创建严格的仿射变换VectorAngleToRigid
                Matrix.VectorAngleToRigid(new HTuple((row1 + row2) / 2), new HTuple((col1 + col2) / 2), new HTuple(0.0),
                                          RowCheck - rowMark + rowOrg, ColumnCheck - colMark + colOrg, AngleCheck);
                //根据Matrix变换ModelRegion
                ModelRegionTrans = ModelRegion.AffineTransRegion(Matrix, "false");
                window.SetColor("red");
                window.SetDraw("margin");
                window.SetLineWidth(2);
                ModelRegionTrans.DispObj(window);
                // Compute the parameters of the measurement rectangles.
                Matrix.AffineTransPixel(new HTuple(row1), new HTuple(col1),
                                        out row1Check, out col1Check);
                Matrix.AffineTransPixel(new HTuple(row2), new HTuple(col2),
                                        out row2Check, out col2Check);
                Rectangle1.GenRectangle1(row1Check, col1Check, row2Check, col2Check);
                window.SetColor("green");
                window.SetDraw("margin");
                window.SetLineWidth(1);
                Rectangle1.DispObj(window);
                window.SetColor("gold");
                window.DispLine(RowCheck - 15, ColumnCheck, RowCheck + 15, ColumnCheck);
                window.DispLine(RowCheck, ColumnCheck - 15, RowCheck, ColumnCheck + 45);


                // For visualization purposes, generate the two rectangles as
                // regions and display them.
                //Rectangle1.GenRectangle2(Rect1RowCheck.D, Rect1ColCheck.D,
                //    RectPhi + AngleCheck.D,
                //    RectLength1, RectLength2);
                //Rectangle2.GenRectangle2(Rect2RowCheck.D, Rect2ColCheck.D,
                //    RectPhi + AngleCheck.D,
                //    RectLength1, RectLength2);
                //window.SetColor("blue");
                //window.SetDraw("margin");
                //Rectangle1.DispObj(window);
                //Rectangle2.DispObj(window);
            }
            //MatchingTimeLabel.Text = "Time: " +
            //    String.Format("{0,4:F1}", (S2 - S1)*1000) + "ms";
            //MatchingScoreLabel.Text = "Score: ";
            //{
            //    MatchingScoreLabel.Text = "Score: " +
            //        String.Format("{0:F5}", Score.D);
            //    // Rotate the model for visualization purposes.
            //    Matrix.VectorAngleToRigid(new HTuple(Row), new HTuple(Column), new HTuple(0.0),
            //        RowCheck, ColumnCheck, AngleCheck);

            //    ModelRegionTrans = ModelRegion.AffineTransRegion(Matrix, "false");
            //window.SetColor("green");
            //window.SetDraw("fill");
            ////ModelRegionTrans.DispObj(window);
        }
Exemple #15
0
        public CircleSearchingResult SearchCircle(HImage image, CircleSearchingDefinition definition)
        {
            var swSearchCircle = new NotifyStopwatch("SearchCircle: " + definition.Name);

            var circleSearchingResult = new CircleSearchingResult
            {
                Definition = definition.DeepClone(),
                Name       = definition.Name,
//                Index = index
            };

            if (definition.ImageFilter_Disabled)
            {
                definition.ImageFilter = null;
            }

            //                if (circleDefinition.RegionExtractor_Disabled)
            //                    circleDefinition.RegionExtractor = null;

            var topLeftX     = definition.CenterX - definition.OuterRadius;
            var topLeftY     = definition.CenterY - definition.OuterRadius;
            var bottomRightX = definition.CenterX + definition.OuterRadius;
            var bottomRightY = definition.CenterY + definition.OuterRadius;

            var reg = new HRegion();

            reg.GenRectangle1(topLeftY, topLeftX, bottomRightY, bottomRightX);
            var reducedImage = image.ReduceDomain(reg);

            reg.Dispose();

            if (definition.Domain_SaveCacheImageEnabled)
            {
                reducedImage.WriteImageOfTiffLzwOfCropDomain(
                    _cacheImageDir + "\\SearchCircles_" + definition.Name + "_1_Domain.tif");
            }

            /*                HRegion domain;
             *              if (circleDefinition.RegionExtractor != null)
             *              {
             *                  throw new NotImplementedException();
             *                  var oldDomain = reducedImage.GetDomain();
             *                  domain = circleDefinition.RegionExtractor.Process(reducedImage);
             *                  oldDomain.Dispose();
             *
             *                  if (circleDefinition.ImageFilter_SaveCacheImageEnabled)
             *                      reducedImage
             *                          .ReduceDomain(domain)
             *                          .CropDomain()
             *                          .ToBitmapSource()
             *                          .SaveToTiff(_cacheImageDir + "\\SearchCircles_" + circleDefinition.Name + "_2_ROI.tif");
             *              }
             *              else
             *              {
             *                  domain = reducedImage.GetDomain();
             *              }*/

            HRegion domain  = reducedImage.GetDomain();
            int     offsetX = domain.GetColumn1();
            int     offsetY = domain.GetRow1();

            var roiImage = reducedImage.CropDomain();

            HImage filterImage = null;

            if (definition.ImageFilter != null)
            {
                var swImageFilter = new NotifyStopwatch("CircleInspector.ImageFilter: " + definition.Name);
                filterImage = definition.ImageFilter.Process(roiImage);
                swImageFilter.Dispose();

                if (definition.ImageFilter_SaveCacheImageEnabled)
                {
                    filterImage.WriteImageOfTiffLzwOfCropDomain(
                        _cacheImageDir + "\\SearchCircles_" + definition.Name + "_3_ImageFilter.tif");

                    //
                    var paintedImage = filterImage.PaintGrayOffset(image, offsetY, offsetX);
                    paintedImage.WriteImageOfJpeg(_cacheImageDir + "\\SearchCircles_" + definition.Name +
                                                  "_3_ImageFilter_PaintGrayOffset.jpg");
                    paintedImage.Dispose();
                }
            }
            else
            {
                filterImage = roiImage;
            }

            var offsetCenterX = definition.CenterX - offsetX;
            var offsetCenterY = definition.CenterY - offsetY;

            var swFindCircle = new NotifyStopwatch("CircleInspector.FindCircle: " + definition.Name);
            var circle       = definition.CircleExtractor.FindCircle(filterImage,
                                                                     offsetCenterX, offsetCenterY, definition.InnerRadius, definition.OuterRadius);

            swFindCircle.Dispose();

            if (circle.IsEmpty)
            {
                circleSearchingResult.HasError   = true;
                circleSearchingResult.IsNotFound = true;
                //                    circleSearchingResult.Circle = new Circle(circleDefinition.CenterX, circleDefinition.CenterY);
            }
            else
            {
                var newCircle = new Circle(circle.CenterX + offsetX, circle.CenterY + offsetY, circle.Radius);
                circleSearchingResult.Circle = newCircle;
            }

            swSearchCircle.Dispose();
            return(circleSearchingResult);
        }