private void ExecuteBtn_Click(object sender, System.EventArgs e) { this.Cursor = Cursors.WaitCursor; HFramegrabber Framegrabber = new HFramegrabber(); // read images and process them try { Framegrabber.OpenFramegrabber("File", 1, 1, 0, 0, 0, 0, "default", -1, "default", -1, "default", "fin.seq", "default", -1, -1); HImage Image = new HImage(); HRegion FinRegion; HTuple FinArea; for (int i = 0; i <= 2; i++) { Image.GrabImage(Framegrabber); Image.DispObj(Window); // execute procedure ProcCall.SetInputIconicParamObject("Image", Image); ProcCall.Execute(); // get output parameters from procedure call FinRegion = ProcCall.GetOutputIconicParamRegion("FinRegion"); FinArea = ProcCall.GetOutputCtrlParamTuple("FinArea"); // display results Image.DispObj(Window); Window.SetColor("red"); Window.DispObj(FinRegion); Window.SetColor("white"); Window.SetTposition(150, 20); Window.WriteString("FinArea: " + FinArea.D); HSystem.WaitSeconds(2); FinRegion.Dispose(); Image.Dispose(); } } catch (HOperatorException Ex) { MessageBox.Show(Ex.Message, "HALCON Exception"); } catch (HDevEngineException Ex) { MessageBox.Show(Ex.Message, "HDevEngine Exception"); } Framegrabber.Dispose(); this.Cursor = Cursors.Default; }
private void StartBtn_Click(object sender, System.EventArgs e) { Framegrabber = new HFramegrabber(); // read images and process them try { //Framegrabber.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb", -1, "false", "default", "[0] USB2.0 HD UVC WebCam", 0, -1); Framegrabber.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "003053231ec3_basler_aca250014gm", 0, -1); Framegrabber.SetFramegrabberParam("AcquisitionMode", "SingleFrame"); HImage Image = new HImage(); HRegion CirclesRegion; int shot; shot = Int32.Parse(shotComboBox.SelectedItem + ""); // execute procedure ProcCall.SetInputCtrlParamTuple("shot", shot); ProcCall.Execute(); // get output parameters from procedure call CirclesRegion = ProcCall.GetOutputIconicParamRegion("RegionOfInterest"); //LightCheck = ProcCall.GetOutputIconicParamRegion("RegionOfInterest"); StartBtn.Enabled = false; shotComboBox.Enabled = false; Savebtn.Enabled = true; StopBtn.Enabled = true; while (true) { if (!StopBtn.Enabled) { StartBtn.Enabled = true; StopBtn.Enabled = false; Framegrabber.Dispose(); Image.Dispose(); return; } //Framegrabber.GrabImageStart(-1); Image = Framegrabber.GrabImageAsync(-1); // display results Image.DispObj(Window); Window.SetColor("red"); Window.DispObj(CirclesRegion); Window.SetPart(0, 0, -2, -2); HSystem.WaitSeconds(0.108); } } catch (HOperatorException Ex) { MessageBox.Show(Ex.Message, "HALCON Exception"); } catch (HDevEngineException Ex) { MessageBox.Show(Ex.Message, "HDevEngine Exception"); } Framegrabber.Dispose(); }
// 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; }