Example #1
0
        // 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;
        }