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(); } }
/// <summary> /// 绑定处理步骤和显示控件 /// </summary> /// <param name="strStep"></param> /// <param name="vc"></param> public void BindWindow(string strStep, VisionControl vc) { VisionBase vb = null; if (m_dicVision.TryGetValue(strStep, out vb)) { vb.BindWindow(vc); } }
/// <summary> /// 更新显示控件 /// </summary> /// <param name="ctl"></param> public override void UpdateVisionControl(VisionControl ctl) { ctl.LockDisplay(); try { if (imgSrc != null && imgSrc.IsInitialized() && imgSrc.Key != IntPtr.Zero) { HTuple num = 0; HOperatorSet.CountObj(imgSrc, out num); if (num > 0)//&& m_image.IsInitialized() && m_image.Key != IntPtr.Zero) { HOperatorSet.DispObj(imgSrc, ctl.GetHalconWindow()); } // HOperatorSet.DispObj(ModelContour, ctl.GetHalconWindow()); //if (MeasureRect != null && MeasureRect.IsInitialized()) //{ // HOperatorSet.SetColor(m_visionControl.GetHalconWindow(), "red"); // HOperatorSet.DispObj(MeasureRect, ctl.GetHalconWindow()); //} if (Lines != null && Lines.IsInitialized()) { HOperatorSet.SetColor(m_visionControl.GetHalconWindow(), "blue"); HOperatorSet.DispObj(Lines, ctl.GetHalconWindow()); } if (IntraDistance != null && IntraDistance.Length > 0) { if (IntraDistance.Type == HTupleType.STRING) { HDevelopExport.disp_message(m_visionControl.GetHalconWindow(), IntraDistance, "window", 20, -1, "green", "false"); } else { var value = SideCameraCalibData.PixelToMm(IntraDistance); HDevelopExport.disp_message(m_visionControl.GetHalconWindow(), $"{value:F2}", "window", 20, -1, "green", "false"); } } } } catch (HalconException HDevExpDefaultException1) { System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString()); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.ToString()); } finally { ctl.UnlockDisplay(); } }
public bool ProcessImage(string strStep, HObject image, VisionControl ctl) { VisionBase vb = null; if (m_dicVision.TryGetValue(strStep, out vb)) { vb.SetSrcImage(image); if (vb.Process(ctl)) { return(true); } } return(false); }
/// <summary> /// 处理当前图像并显示在指定控件上 /// </summary> /// <param name="vc"></param> /// <returns></returns> public override bool ProcessImage(VisionControl vc) { Measuer(); return(true); }
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); } }
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(); //图像预处理 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); } }
/// <summary> /// 显示控件刷新 /// </summary> /// <param name="ctl"></param> public abstract void UpdateVisionControl(VisionControl ctl);
/// <summary> /// 处理图像并显示在指定视觉控件上 /// </summary> /// <param name="ctl"></param> /// <returns></returns> public bool Process(VisionControl ctl) { ctl.RegisterUpdateInterface(this); return(ProcessImage(ctl)); }
/// <summary> /// 绑定一个视觉控件,自动流程时显示处理结果 /// </summary> /// <param name="vc"></param> public void BindWindow(VisionControl vc) { m_visionControl = vc; }
/// <summary> /// 处理当前缓存的图像并显示在指定视觉控件上 /// </summary> /// <param name="ctl"></param> /// <returns></returns> public abstract bool ProcessImage(VisionControl ctl);