public override void UpdateVisionControl(VisionControl ctl)
        {
            ctl.LockDisplay();
            try
            {
                if (imgSrc != null && imgSrc.IsInitialized() && imgSrc.Key != IntPtr.Zero)
                {
                    if (imgSrc != null)
                    {
                        HOperatorSet.DispObj(imgSrc, ctl.GetHalconWindow());
                    }

                    while (ctl.getStack().Count > 0)
                    {
                        HOperatorSet.DispObj(ctl.getStack().Pop(), ctl.GetHalconWindow());
                    }

                    foreach (var mea in MeasureMgr.GetInstance().MeasureList)
                    {
                        HOperatorSet.SetColor(ctl.GetHalconWindow(), "blue");
                        HOperatorSet.DispObj(mea.LineEdge, ctl.GetHalconWindow());
                        HOperatorSet.SetColor(ctl.GetHalconWindow(), "green");
                        HOperatorSet.DispObj(mea.ContourOk, ctl.GetHalconWindow());
                        HOperatorSet.SetColor(ctl.GetHalconWindow(), "red");
                        HOperatorSet.DispObj(mea.ContourAreaNG, ctl.GetHalconWindow());
                        HOperatorSet.SetColor(ctl.GetHalconWindow(), "magenta");
                        HOperatorSet.DispObj(mea.ContourPosNG, ctl.GetHalconWindow());
                    }

                    HOperatorSet.SetColor(ctl.GetHalconWindow(), "blue");

                    //HTuple hv_Width, hv_Height;
                    //HOperatorSet.GetImageSize(m_image, out hv_Width, out hv_Height);
                    //HObject ht_CrossLineV, ht_CrossLineH;
                    //HOperatorSet.SetColor(ctl.GetHalconWindow(), "red");
                    //HOperatorSet.GenRegionLine(out ht_CrossLineV, 0, hv_Width / 2, hv_Height, hv_Width / 2);
                    //HOperatorSet.GenRegionLine(out ht_CrossLineH, hv_Height / 2, 0, hv_Height / 2, hv_Width);
                    //HOperatorSet.DispObj(ht_CrossLineV, ctl.GetHalconWindow());
                    //HOperatorSet.DispObj(ht_CrossLineH, ctl.GetHalconWindow());
                }
            }
            catch (HalconException HDevExpDefaultException1)
            {
                System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString());
            }
            catch (AccessViolationException e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
            }
            finally
            {
                ctl.UnlockDisplay();
            }
        }
            public void MeasurePos(HObject image, HObject edgeContour)
            {
                //记录测量数量
                int meaCount = 0;

                try
                {
                    HTuple row    = CenterRow;
                    HTuple column = CenterColumn;
                    HTuple radian = Radian;

                    HTuple homMat2D, rowTrans, columnTrans;

                    /*
                     * HOperatorSet.HomMat2dIdentity(out homMat2D);
                     * HOperatorSet.HomMat2dTranslate(homMat2D, 0, PinDistance * PinCount / 2, out homMat2D);
                     * HOperatorSet.HomMat2dRotate(homMat2D, radian, row, column, out homMat2D);
                     * HOperatorSet.AffineTransPixel(homMat2D, row, column, out rowTrans, out columnTrans);
                     *
                     * HObject rect;
                     * HOperatorSet.GenRectangle2(out rect, rowTrans, columnTrans, radian, PinDistance * PinCount / 2 + Width, 100);
                     * HOperatorSet.ReduceDomain(image, rect, out image);
                     *
                     * HOperatorSet.AnisotropicDiffusion(image, out image, "weickert", 5, 1, 10);
                     * HOperatorSet.Emphasize(image, out image, 15, 15, 1);
                     *
                     * HOperatorSet.ConcatObj(LineEdge, image, out LineEdge);
                     */

                    //HTuple edgeRow1, edgeColumn1, edgeRow2, edgeColumn2;
                    //FindEdge(image, row, column, radian, out edgeRow1, out edgeColumn1, out edgeRow2, out edgeColumn2);
                    HOperatorSet.ConcatObj(LineEdge, edgeContour, out LineEdge);

                    //测量所有碳碳点
                    for (int i = 0; i < PinCount; i++)
                    {
                        //变换检测位置
                        //HTuple homMat2D, rowTrans, columnTrans;
                        HOperatorSet.HomMat2dIdentity(out homMat2D);
                        HOperatorSet.HomMat2dTranslate(homMat2D, 0, i * PinDistance, out homMat2D);
                        HOperatorSet.HomMat2dRotate(homMat2D, radian, row, column, out homMat2D);
                        HOperatorSet.AffineTransPixel(homMat2D, row, column, out rowTrans, out columnTrans);

                        //检测碳点
                        HObject contour;
                        HTuple  diameterMax, diameterMin, disLeft, disRight, edgeRow, edgeColumn;
                        MeasureCircle(image, rowTrans, columnTrans, radian, out diameterMax, out diameterMin, out disLeft, out disRight,
                                      out edgeRow, out edgeColumn, out contour);

                        //测量碳点到玻璃边缘的距离
                        HTuple disTop, distanceMax;
                        //HOperatorSet.DistancePl(edgeRow, edgeColumn, edgeRow1, edgeColumn1, edgeRow2, edgeColumn2, out disTop);
                        HOperatorSet.DistancePc(edgeContour, edgeRow, edgeColumn, out disTop, out distanceMax);


                        //像素转毫米
                        double maxDiameter = Math.Round((PlatformCalibData.PixelToMm(diameterMax) + MeasureMgr.GetInstance().OffsetDia), 2);
                        double minDiameter = Math.Round(PlatformCalibData.PixelToMm(diameterMin), 2);
                        double leftPos     = Math.Round(PlatformCalibData.PixelToMm(disLeft), 2);
                        double reghtPos    = Math.Round(PlatformCalibData.PixelToMm(disRight), 2);
                        double topPos      = Math.Round(PlatformCalibData.PixelToMm(disTop), 2);

                        //优先判断面积NG,后判断位置NG
                        double LimiteMax = PlatformCalibData.PixelToMm(PinDistance);
                        if (maxDiameter > LimiteDiameterMax || maxDiameter < LimiteDiameterMin)
                        {
                            HOperatorSet.ConcatObj(ContourAreaNG, contour, out ContourAreaNG);
                            CountAreaNG++;
                        }
                        else if (leftPos < LimiteLeft || leftPos > LimiteMax ||
                                 reghtPos < LimiteRight || reghtPos > LimiteMax ||
                                 topPos < LimiteTopMin || topPos > LimiteTopMax)
                        {
                            HOperatorSet.ConcatObj(ContourPosNG, contour, out ContourPosNG);
                            CountPosNG++;
                        }
                        else
                        {
                            HOperatorSet.ConcatObj(ContourOk, contour, out ContourOk);
                            CountOK++;
                        }

                        //添加到参数列表
                        DiameterMax.Append(maxDiameter);
                        DiameterMin.Append(minDiameter);
                        DisLeft.Append(leftPos);
                        DisRight.Append(reghtPos);
                        DisTop.Append(topPos);

                        //计数
                        meaCount++;
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {
                    //未测量完成计入面积NG
                    if (PinCount != meaCount)
                    {
                        CountAreaNG += PinCount - meaCount;
                    }
                }
            }
        public override bool ProcessImage(VisionControl ctl)
        {
            try
            {
                //清理数据
                foreach (var mea in MeasureMgr.GetInstance().MeasureList)
                {
                    mea.ClearResult();
                }

                ctl.clearObj();
                ctl.DisplayResults();

                //保存图像
                //string imageName = $"{DateTime.Now:HHmmss}.tiff";

                //if (AutoForm._autoForm.Param.IsSaveImageAll)
                //{
                //    //保存原图
                //    string path = $@"{ProductMgr.GetInstance().ProductPath}Images\Robot\{DateTime.Now:yyMMdd}\Source\";
                //    if (!System.IO.Directory.Exists(path))
                //    {
                //        System.IO.Directory.CreateDirectory(path);
                //    }
                //    string fileName = $"{path}{imageName}";
                //    HDevelopExport.WriteImage(imgSrc, fileName);
                //}

                //图像预处理
                HObject image;
                if (ProductMgr.GetInstance().Param.PlatformRegion != null)
                {
                    HOperatorSet.ReduceDomain(imgSrc, ProductMgr.GetInstance().Param.PlatformRegion, out image);

                    //if (ProductMgr.GetInstance().Param.IsPerprocess)
                    //{
                    //    image = HDevelopExport.Preprocess(image, ProductMgr.GetInstance().Param.Emphasize, false);
                    //}
                }
                else
                {
                    image = imgSrc;
                }

                //查找模板
                HTuple row, column, angle, scale, score;
                bool   result = HDevelopExport.FindScaleShapeModel(image, out row, out column, out angle, out scale, out score);

                //显示轮廓
                HDevelopExport.dev_display_shape_matching_results(ctl.GetHalconWindow(),
                                                                  ProductMgr.GetInstance().Param.ModelID, "blue", row, column, angle, scale, scale, 0);

                if (result && ProductMgr.GetInstance().Param.IsSecondPos)
                {
                    HTuple transRow, transColumn, transRadian;
                    result = HDevelopExport.FindPinCenter(imgSrc, row, column, angle, out transRow, out transColumn, out transRadian);

                    if (result)
                    {
                        row    = transRow;
                        column = transColumn;
                        angle  = transRadian;
                    }

                    //ctl.DisplayResults();
                }

                if (!result)
                {
                    Log.Show("查找模板失败");
                    return(false);
                }

                //*************相对位置**************
                //HTuple relRow, relColumn;

                //relRow = PlatformCalibData.MarkRow - row;
                //relColumn = PlatformCalibData.MarkColumn - column;

                //SendData.X = PlatformCalibData.PixelToMm(relColumn);
                //SendData.Y = PlatformCalibData.PixelToMm(relRow);

                //用矩阵获得Mark点的世界坐标和模板的世界坐标,求差值
                HTuple colMark, rowMark, rowTrans, colTrans;
                HOperatorSet.AffineTransPixel(PlatformCalibData.HomMat2D, PlatformCalibData.MarkColumn, PlatformCalibData.MarkRow, out colMark, out rowMark);
                HOperatorSet.AffineTransPixel(PlatformCalibData.HomMat2D, column, row, out colTrans, out rowTrans);
                SendData.X = colMark - colTrans;
                SendData.Y = rowMark - rowTrans;

                Log.Show($"目标位置:X:{SendData.X:F2},Y:{SendData.Y:F2}");

                //***********************测量****************************
                MeasureMgr.GetInstance().MeasureAll(image, row, column, angle);

                //显示数据
                int hasCount = 0, meaCount = 0;
                for (int i = 0; i < MeasureMgr.GetInstance().MeasureList.Count; i++)
                {
                    var mea = MeasureMgr.GetInstance().MeasureList[i];

                    hasCount += mea.PinCount;
                    meaCount += mea.CountOK + mea.CountAreaNG + mea.CountPosNG;

                    //发送的数据
                    SendData.CountAreaNG += mea.CountAreaNG;
                    SendData.CountPosNG  += mea.CountPosNG;

                    if (mea.DiameterMax.Length > 0)
                    {
                        //显示和保存数据
                        Data.Show(i, mea.DiameterMax.ToDArr(), mea.DisLeft.ToDArr(), mea.DisRight.ToDArr(), mea.DisTop.ToDArr());
                    }
                }

                ctl.AddToStack(ProductMgr.GetInstance().Param.ModelContours);
                ctl.AddToStack(ProductMgr.GetInstance().Param.ModelOriginContours);
                ctl.AddToStack(ProductMgr.GetInstance().Param.MarkContours);

                ctl.DisplayResults();

                //显示数据到窗口
                HTuple degree;
                HOperatorSet.TupleDeg(angle, out degree);
                Log.Show($"查找模板:row:{row.D:F2},column:{column.D:F2},degree:{degree.D:F2},分数:{score.D:F2}");

                HDevelopExport.disp_message(ctl.GetHalconWindow(), $"位置:{row.D:F2},{column.D:F2},{degree.D:F2}", "window", 10, -1, "green", "false");
                HDevelopExport.disp_message(ctl.GetHalconWindow(), $"分数:{score.D:F2}", "window", 30, -1, "green", "false");

                HDevelopExport.disp_message(ctl.GetHalconWindow(), $"X:{SendData.X:F2},Y:{SendData.Y:F2}", "window", 150, -1, "green", "false");
                HDevelopExport.disp_message(ctl.GetHalconWindow(), $"{hasCount}-{meaCount}", "window", 170, -1, "green", "false");

                if (SendData.CountAreaNG > 0)
                {
                    HDevelopExport.disp_message(ctl.GetHalconWindow(), $"面积NG:{SendData.CountAreaNG}", "window", 190, -1, "red", "false");
                }

                if (SendData.CountPosNG > 0)
                {
                    HDevelopExport.disp_message(ctl.GetHalconWindow(), $"位置NG:{SendData.CountPosNG}", "window", 210, -1, "magenta", "false");
                }

                //显示日志
                Log.Show($"面积NG:{SendData.CountAreaNG},位置NG:{SendData.CountPosNG}");

                //保存窗口图像和原图
                result = result && SendData.CountAreaNG == 0 && SendData.CountPosNG == 0;
                if (AutoForm._autoForm.Param.IsSaveImageAll)
                {
                    SaveImage(ctl.GetHalconWindow(), result);
                }
                else
                {
                    if (AutoForm._autoForm.Param.IsSaveImageNG && !result)
                    {
                        SaveImage(ctl.GetHalconWindow(), result);
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
コード例 #4
0
        public override bool ProcessImage(VisionControl ctl)
        {
            try
            {
                //清理数据
                foreach (var mea in MeasureMgr.GetInstance().MeasureList)
                {
                    mea.ClearResult();
                }

                ctl.clearObj();
                ctl.DisplayResults();

                //图像预处理
                HObject image;
                if (ProductMgr.GetInstance().Param.PlatformRegion != null)
                {
                    HOperatorSet.ReduceDomain(imgSrc, ProductMgr.GetInstance().Param.PlatformRegion, out image);
                }
                else
                {
                    image = imgSrc;
                }

                //查找模板
                HTuple row, column, angle, scale, score;
                bool   result = HDevelopExport.FindScaleShapeModel(image, out row, out column, out angle, out scale, out score);

                //显示轮廓
                HDevelopExport.dev_display_shape_matching_results(ctl.GetHalconWindow(),
                                                                  ProductMgr.GetInstance().Param.ModelID, "blue", row, column, angle, scale, scale, 0);

                if (!result)
                {
                    Log.Show("查找模板失败");
                    return(false);
                }


                //***********************测量****************************
                image = MeasureMgr.GetInstance().ImagePre(image);
                MeasureMgr.GetInstance().MeasureAll(image, row, column, angle);
                //ctl.AddToStack(image);

                //显示数据
                bool resRoi = true;//每个测量区域都做判断
                int  hasCount = 0, meaCount = 0;
                for (int i = 0; i < MeasureMgr.GetInstance().MeasureList.Count; i++)
                {
                    var mea = MeasureMgr.GetInstance().MeasureList[i];

                    if (mea.PinCount != mea.CountPinMea)
                    {
                        resRoi = false;
                    }

                    hasCount += mea.PinCount;
                    //meaCount += mea.CountOK + mea.CountAreaNG + mea.CountPosNG;
                    meaCount += mea.CountPinMea;

                    //发送的数据
                    //SendData.CountAreaNG += mea.CountAreaNG;
                    //SendData.CountPosNG += mea.CountPosNG;

                    //if (mea.DiameterMax.Length > 0)
                    //{
                    //    //显示和保存数据
                    //    Data.Show(i, mea.DiameterMax.ToDArr(), mea.DisLeft.ToDArr(), mea.DisRight.ToDArr(), mea.DisTop.ToDArr());
                    //}
                }

                string color = "green";
                if (resRoi && hasCount == meaCount)
                {
                    color = "green";
                    SendData.PinCountOK = true;
                }
                else
                {
                    color = "red";
                    SendData.PinCountOK = false;
                }

                ctl.AddToStack(ProductMgr.GetInstance().Param.ModelContours);
                ctl.AddToStack(ProductMgr.GetInstance().Param.ModelOriginContours);
                //ctl.AddToStack(ProductMgr.GetInstance().Param.MarkContours);

                ctl.DisplayResults();

                for (int i = 0; i < MeasureMgr.GetInstance().MeasureList.Count; i++)
                {
                    var mea = MeasureMgr.GetInstance().MeasureList[i];

                    if (mea.PinCount == mea.CountPinMea)
                    {
                        HDevelopExport.disp_message(ctl.GetHalconWindow(), $"ROI{i}数量:{mea.PinCount},测量数量:{mea.CountPinMea}", "window", 10 + 20 * i, -1, "green", "false");
                    }
                    else
                    {
                        HDevelopExport.disp_message(ctl.GetHalconWindow(), $"ROI{i}数量:{mea.PinCount},测量数量:{mea.CountPinMea}", "window", 10 + 20 * i, -1, "red", "false");
                    }
                }

                HDevelopExport.disp_message(ctl.GetHalconWindow(), $"总数量:{hasCount},测量数量:{meaCount}", "window", 10 + 20 * MeasureMgr.GetInstance().MeasureList.Count, -1, color, "false");

                //保存窗口图像和原图
                result = result && SendData.CountAreaNG == 0 && SendData.CountPosNG == 0;
                if (AutoForm._autoForm.Param.IsSaveImageAll)
                {
                    SaveImage(ctl.GetHalconWindow(), result);
                }
                else
                {
                    if (AutoForm._autoForm.Param.IsSaveImageNG && !result)
                    {
                        SaveImage(ctl.GetHalconWindow(), result);
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }