public override bool ProcessImage(VisionControl ctl)
        {
            try
            {
                ctl.clearObj();
                ctl.DisplayResults();

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

                //if (AutoForm._autoForm.Param.IsSaveImageAll)
                //{
                //    //保存原图
                //    string path = $@"{ProductMgr.GetInstance().ProductPath}Images\Platform\{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("查找模板失败");
                }
                else
                {
                    //弧度差
                    HTuple relRow, relColumn, relRadian, rowTrans, colTrans;
                    //relRadian = PlatformCalibData.MarkRadian - angle;
                    relRadian = 0 - angle;

                    //旋转特征点
                    //HTuple homMat2D;
                    //HOperatorSet.HomMat2dIdentity(out homMat2D);
                    //HOperatorSet.HomMat2dRotate(homMat2D, relRadian, PlatformCalibData.CenterRow, PlatformCalibData.CenterColumn, out homMat2D);
                    //HOperatorSet.AffineTransPixel(homMat2D, row, column, out rowTrans, out colTrans);

                    //以平台Mark点为基准
                    //relRow = PlatformCalibData.MarkRow - rowTrans;
                    //relColumn = PlatformCalibData.MarkColumn - colTrans;

                    //没有旋转,直接计算X和Y方向的差值,下面的角度只用于显示
                    //relRow = PlatformCalibData.MarkRow - row;
                    //relColumn = PlatformCalibData.MarkColumn - column;

                    //弧度转角度
                    HTuple degree;
                    HOperatorSet.TupleDeg(relRadian, out degree);

                    //赋值给需要发送的数据
                    //SendData.X = PlatformCalibData.PixelToMm(relColumn);
                    //SendData.Y = PlatformCalibData.PixelToMm(relRow);
                    //SendData.Angle = degree;


                    //用矩阵获得Mark点的世界坐标和模板的世界坐标,求差值
                    HTuple colMark, rowMark;
                    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;



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

                    ctl.DisplayResults();

                    //显示数据到窗口
                    HTuple deg;
                    HOperatorSet.TupleDeg(angle, out deg);
                    Log.Show($"查找模板:row:{row.D:F2},column:{column.D:F2},degree:{deg.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},A:{SendData.Angle:F2}", "window", 150, -1, "green", "false");
                    Log.Show($"目标位置:X:{SendData.X:F2},Y:{SendData.Y:F2},A:{SendData.Angle:F2}");
                }

                //保存查找模板的窗口图片和原图
                if (AutoForm._autoForm.Param.IsSaveImageAll)
                {
                    SaveImage(ctl.GetHalconWindow(), result);
                }
                else
                {
                    if (AutoForm._autoForm.Param.IsSaveImageNG && !result)
                    {
                        SaveImage(ctl.GetHalconWindow(), result);
                    }
                }

                return(result);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        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);
            }
        }
Beispiel #3
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);
            }
        }