public void Close() { if (hShapeModel != null && hShapeModel.IsInitialized()) { hShapeModel.Dispose(); } hShapeModel = null; if (modelRegion != null && modelRegion.IsInitialized()) { modelRegion.Dispose(); } modelRegion = null; if (modelXLDCont != null && modelXLDCont.IsInitialized()) { modelXLDCont.Dispose(); } modelXLDCont = null; if (modelXLDContAffine != null && modelXLDContAffine.IsInitialized()) { modelXLDContAffine.Dispose(); } modelXLDContAffine = null; shapeModelROIList = null; }
public void SetModelImage() { if (ModelImg != null) { ModelImg.Dispose(); } ModelImg = new HImage(InputImg); if (SearchRegion != null) { SearchRegion.Dispose(); SearchRegion = null; } if (ModelRegion != null) { ModelRegion.Dispose(); ModelRegion = null; } if (shapeModel != null) { shapeModel.Dispose(); shapeModel = null; } }
public override HMessage ActorTask(HMessage obj) { HTuple isempty = msg_queue.GetMessageQueueParam("is_empty"); if (isempty.I == 0) { msg_queue.SetMessageQueueParam("flush_queue", 1); } if (obj != null) { obj.Dispose(); } HTuple row, column, angle, score; HImage img; img = imggrabber.GrabImageAsync(-1); HRegion roi = new HRegion(ActiveObject); HMessage msg = new HMessage(); msg.SetMessageObj(img, "image"); Monitor.Enter(lockobj); try { if (sbm != null) { sbm.Dispose(); } sbm = new HShapeModel(img.ReduceDomain(roi), "auto", -Math.PI, 2 * Math.PI, "auto", "auto", "use_polarity", "auto", "auto"); sbm.FindShapeModel(img, 0.0, 0.0, 0.5, 1, 0.5, "least_squares", 0, 0.9, out row, out column, out angle, out score); if (row.Length > 0) { HXLDCont contours = sbm.GetShapeModelContours(1); HHomMat2D homMat2D = new HHomMat2D(); homMat2D.VectorAngleToRigid(0.0, 0.0, 0.0, row.D, column.D, angle.D); HXLDCont projcont = homMat2D.AffineTransContourXld(contours); msg.SetMessageObj(projcont, "contour"); } else { sbm.Dispose(); sbm = null; } } finally { Monitor.Exit(lockobj); } return(msg); }
/// <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); }
// Training of the shape model and write the serialized item to a file // stream private void CreateBtn_Click(object sender, System.EventArgs e) { HTuple Names; HRegion Coin; HShapeModel Model; TrainBtn.Enabled = false; Window.SetColor("red"); Window.SetDraw("margin"); Window.SetLineWidth(1); Names = "german"; Names.Append("italian"); Names.Append("greek"); Names.Append("spanish"); //Write a serialized item of type tuple to a file Stream s = File.OpenWrite("serialized_shape_model"); Names.Serialize(s); // Note: The binary layout of serialized HALCON objects is determined by // the native HALCON libary. You could also package this blob together with // other .NET objects into a formatted stream, e.g. by using // // BinaryFormatter f = new BinaryFormatter(); // f.Serialize(s, Names); // // When using only HALCON objects without extra formatting, the streamed // data is interchangable with HDevelop or HALCON/C++. //Train shape models for (int i = 0; i < 4; i++) { MatchingLabel.Text = "Train coin " + (i + 1) + "/4 (" + Names[i] + ") and serialize resulting shape model to a" + " file..."; MatchingLabel.Refresh(); Img = new HImage("coins/20cent_" + Names[i]); Window.DispObj(Img); HRegion Region = Img.Threshold(70.0, 255.0); HRegion ConnectedRegions = Region.Connection(); HRegion SelectedRegions = ConnectedRegions.SelectShapeStd("max_area", 0); HRegion RegionTrans = SelectedRegions.ShapeTrans("convex"); Coin = new HRegion(RegionTrans.Row, RegionTrans.Column, 120); int Contrast = 20; HTuple HysteresisContrast; HysteresisContrast = (Contrast / 2.0); HysteresisContrast.Append(Contrast + 6.0); HysteresisContrast.Append(10.0); HImage ImgReduced = Img.ReduceDomain(Coin); //Called during the test phase to see if contrast is selected correctly Model = new HShapeModel(ImgReduced, new HTuple(0), 0.0, new HTuple(360.0).TupleRad(), new HTuple(0), new HTuple("no_pregeneration"), "ignore_local_polarity", HysteresisContrast, new HTuple(5)); //Write a serialized item of type shape based model to a file Model.Serialize(s); // Destroy shape based model and objects Coin.Dispose(); Model.Dispose(); Img.Dispose(); ImgReduced.Dispose(); Region.Dispose(); SelectedRegions.Dispose(); RegionTrans.Dispose(); ImgReduced.Dispose(); HSystem.WaitSeconds(0.4); } s.Close(); MatchingLabel.Text = "Ready to find coins"; StopBtn.Enabled = false; StartBtn.Enabled = true; }