public bool createGreyModel() { if (mImage == null) { if (!onTimer) { NotifyIconObserver(MatchingAssistant.ERR_NO_MODEL_DEFINED); } return(false); } try { ModelIDGrey = mImage.CreateTemplate(255, 4, "sort", "original"); } catch (HOperatorException e) { if (!onTimer) { exceptionText = e.Message; NotifyParamObserver(MatchingParam.H_ERR_MESSAGE); } return(false); } // tResult.mContour = ModelID.GetShapeModelContours(1); createNewModelID = false; return(true); }
/// <summary> /// Initializes flags, lists, and delegates to have a valid /// starting point to start the assistant. /// </summary> public MatchingAssistant(MatchingParam parSet) { parameterSet = parSet; NotifyIconObserver = new MatchingDelegate(dummy); NotifyParamObserver = new AutoParamDelegate(dummyS); ModelID = new HShapeModel(); homSc2D = new HHomMat2D(); TestImages = new Hashtable(10); tResult = new MatchingResult(); contrastLowB = 0; contrastUpB = 255; scaleStepLowB = 0.0; scaleStepUpB = (double)19.0 / 1000.0; angleStepLowB = 0.0; angleStepUpB = (double)(112.0 / 10.0) * Math.PI / 180.0; pyramLevLowB = 1; pyramLevUpB = 6; minContrastLowB = 0; minContrastUpB = 30; findAlways = false; createNewModelID = true; ModelID = new HShapeModel(); GrayTemplate = new HTemplate(); onExternalModelID = false; }
public GrayMatchTool(IToolInfo info, DisplayControl window) { this.Info = info; this.Window = window; this.ToolName = info.ToolName; try { Template = new HTemplate(); Template = ReadModelFromFile(info.ToolName); } catch (Exception e) { Template = null; WriteErrorLog("VisionTool", e.ToString()); } }
private HTemplate ReadModelFromFile(string fileName) { HTemplate model = new HTemplate();; try { if (File.Exists(@".//ModelMatchFile/" + fileName)) { model.ReadTemplate(@".//ModelMatchFile/" + fileName); } else { return(null); } } catch { WriteErrorLog("VisionTool", $"{ToolName}模板读取失败"); return(null); } return(model); }
/// <summary> /// Creates the shape-based model. If the region of interest /// <c>mROIModel</c> is missing or not well defined using the /// interactive ROIs, then an error message is returned. /// </summary> public bool createShapeModel(string filePath) { bool isSuccess = false; if (mReducedImage == null) { if (!onTimer) { NotifyIconObserver(MatchingAssistant.ERR_NO_MODEL_DEFINED); } return(false); } try { parameterSet.mMetric = "ignore_local_polarity"; //parameterSet.mOptimization = "auto"; //parameterSet.mAngleStep = "auto"; mReducedImage.WriteImage("bmp", 0, filePath + "template.bmp"); if (ModelID != null) { ModelID.Dispose(); } ModelID = mReducedImage.CreateShapeModel( parameterSet.mNumLevel, parameterSet.mStartingAngle, parameterSet.mAngleExtent, 0.0175 / 10, "auto",//parameterSet.mOptimization, parameterSet.mMetric, "auto_contrast", "auto" ); if (GrayTemplate != null) { GrayTemplate.Dispose(); } GrayTemplate = mReducedImage.CreateTemplate(255, parameterSet.mNumLevel, "none", "original"); isSuccess = true; } catch (HOperatorException e) { isSuccess = false; if (!onTimer) { exceptionText = e.Message; NotifyParamObserver(MatchingParam.H_ERR_MESSAGE); } return(false); } if (tResult.mContour != null) { tResult.mContour.Dispose(); } tResult.mContour = ModelID.GetShapeModelContours(1); createNewModelID = false; return(isSuccess); }
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); } HTemplate grayTempalte = new HTemplate(); double modelRow, modelCol; if (modelImage != null) { try { grayTempalte.CreateTemplateRot(modelImage, Convert.ToInt32(info.NumLevels), TransAngle.AngleToHu(info.AngleStart), TransAngle.AngleToHu(info.AngleExtent), TransAngle.AngleToHu(info.AngleStep), info._Optimization.ToString(), info.grayValues.ToString()); modelRegion.AreaCenter(out modelRow, out modelCol); info.ModelRegionRow = modelRow; info.ModelRegionCol = modelCol; info.ModelRegionAngle = 0.0; this.Template = grayTempalte; SaveModelFile(info.ToolName); } catch { WriteErrorLog("VisionTool", info.ToolName + "模板创建失败,原因是调用Halcon对象创建函数出错"); return(false); } } else { WriteErrorLog("VisionTool", info.ToolName + "模板创建失败,原因是模板图像对象为空"); return(false); } return(true); }