Beispiel #1
0
        // Procedure to find the coins and to read the serialized item from a
        // file stream
        private void Action()
        {
            HRegion Coin, Circle;
            HTuple  Row, Column, Angle, Score, ResultModel;

            if (ImgNum == 0)
            {
                MatchingLabel.Text = "Deserialize shape models...";
                MatchingLabel.Refresh();
                //Reading names
                Stream s = File.OpenRead("serialized_shape_model");
                Names = HTuple.Deserialize(s);
                //Reading shape models
                for (int i = 0; i < 4; i++)
                {
                    Models[i] = HShapeModel.Deserialize(s);
                }
                s.Close();
                MatchingLabel.Text = "Deserialize shape models... OK";
                MatchingLabel.Refresh();
            }

            if (ImgNum == 13)
            {
                ImgNum = 0;
            }
            HTuple Zero;

            if (ImgNum + 1 < 10)
            {
                Zero = "0";
            }
            else
            {
                Zero = "";
            }

            //Find shape based model using the read models
            Img.ReadImage("coins/20cent_" + Zero + (ImgNum + 1).ToString() + ".png");
            ImgNum++;

            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);
            Circle = new HRegion(Coin.Row, Coin.Column, 35);
            HImage ImgReduced = Img.ReduceDomain(Circle);

            ImgReduced.FindShapeModels(Models, 0.0, new HTuple(360.0).TupleRad(),
                                       0.6, 1, 0, "interpolation", 0, 0.9,
                                       out Row, out Column, out Angle, out Score,
                                       out ResultModel);

            HXLDCont  ModelContours = Models[ResultModel.I].GetShapeModelContours(1);
            HHomMat2D HomMat2D      = new HHomMat2D();

            Window.SetColor("green");

            HomMat2D.VectorAngleToRigid(0, 0, 0, Row, Column, Angle);
            HXLDCont ContoursAffineTrans = ModelContours.AffineTransContourXld(HomMat2D);

            HSystem.SetSystem("flush_graphic", "false");
            Window.DispObj(Img);
            HSystem.SetSystem("flush_graphic", "true");
            Window.DispObj(ContoursAffineTrans);

            MatchingLabel.Text = "#" + (ImgNum + 1) + ": Found: " +
                                 Names[ResultModel.I] + " coin";
        }