Exemplo n.º 1
0
        // Save infromation about all databases and glyphs into XML writer
        public void Save(XmlTextWriter xmlOut)
        {
            foreach (KeyValuePair <string, GlyphDatabase> kvp in dbs)
            {
                xmlOut.WriteStartElement(databaseTag);
                xmlOut.WriteAttributeString(nameAttr, kvp.Key);
                xmlOut.WriteAttributeString(sizeAttr, kvp.Value.Size.ToString( ));
                xmlOut.WriteAttributeString(countAttr, kvp.Value.Count.ToString( ));

                // save glyps
                foreach (Glyph glyph in kvp.Value)
                {
                    xmlOut.WriteStartElement(glyphTag);
                    xmlOut.WriteAttributeString(nameAttr, glyph.Name);
                    xmlOut.WriteAttributeString(dataAttr, GlyphDataToString(glyph.Data));

                    if (glyph.UserData != null)
                    {
                        GlyphVisualizationData visualization = (GlyphVisualizationData)glyph.UserData;

                        // highlight color
                        xmlOut.WriteAttributeString(colorAttr, string.Format("{0},{1},{2}",
                                                                             visualization.Color.R, visualization.Color.G, visualization.Color.B));
                        // glyph's image
                        xmlOut.WriteAttributeString(iconAttr, visualization.ImageName);
                        // glyph's 3D model
                        xmlOut.WriteAttributeString(modelAttr, visualization.ModelName);
                    }

                    xmlOut.WriteEndElement( );
                }

                xmlOut.WriteEndElement( );
            }
        }
Exemplo n.º 2
0
        // Load information about databases and glyphs from XML reader
        public void Load(XmlTextReader xmlIn)
        {
            // read to the first node
            xmlIn.Read( );

            int startingDept = xmlIn.Depth;

            while ((xmlIn.Name == databaseTag) && (xmlIn.NodeType == XmlNodeType.Element) && (xmlIn.Depth >= startingDept))
            {
                string name  = xmlIn.GetAttribute(nameAttr);
                int    size  = int.Parse(xmlIn.GetAttribute(sizeAttr));
                int    count = int.Parse(xmlIn.GetAttribute(countAttr));

                // create new database and add it to collection
                GlyphDatabase db = new GlyphDatabase(size);
                AddGlyphDatabase(name, db);

                if (count > 0)
                {
                    // read all glyphs
                    for (int i = 0; i < count; i++)
                    {
                        // read to the next glyph node
                        xmlIn.Read( );

                        string glyphName    = xmlIn.GetAttribute(nameAttr);
                        string glyphStrData = xmlIn.GetAttribute(dataAttr);

                        // create new glyph and add it database
                        Glyph glyph = new Glyph(glyphName, GlyphDataFromString(glyphStrData, size));
                        db.Add(glyph);

                        // read visualization params
                        GlyphVisualizationData visualization = new GlyphVisualizationData(Color.Red);

                        visualization.ImageName = xmlIn.GetAttribute(iconAttr);
                        visualization.ModelName = xmlIn.GetAttribute(modelAttr);

                        string colorStr = xmlIn.GetAttribute(colorAttr);

                        if (colorStr != null)
                        {
                            string[] rgbStr = colorStr.Split(',');

                            visualization.Color = Color.FromArgb(
                                int.Parse(rgbStr[0]), int.Parse(rgbStr[1]), int.Parse(rgbStr[2]));
                        }

                        glyph.UserData = visualization;
                    }

                    // read to the end tag
                    xmlIn.Read( );
                }

                // read to the next node
                xmlIn.Read( );
            }
        }
Exemplo n.º 3
0
        public EditGlyphForm(Glyph glyph, ReadOnlyCollection <string> existingNames)
        {
            InitializeComponent( );

            this.glyph = glyph;
            if (glyph.UserData == null)
            {
                glyph.UserData = new GlyphVisualizationData(Color.Red);
            }
            visualizationData = (GlyphVisualizationData)glyph.UserData;

            forbiddenNames = existingNames;

            // show information about the glyph
            glyphEditor.GlyphData = (byte[, ])glyph.Data.Clone( );
            nameBox.Text          = glyph.Name;
            colorButton.BackColor = visualizationData.Color;
            UpdateGlyphIcon( );
        }
        // Process image searching for glyphs and highlighting them
        public List <ExtractedGlyphData> ProcessImage(Bitmap bitmap)
        {
            List <ExtractedGlyphData> glyphs = new List <ExtractedGlyphData>( );

            lock ( sync )
            {
                glyphTracker.ImageSize = bitmap.Size;

                // get list of recognized glyphs
                glyphs.AddRange(recognizer.FindGlyphs(bitmap));
                List <int> glyphIDs = glyphTracker.TrackGlyphs(glyphs);

                if (glyphs.Count > 0)
                {
                    if ((visualizationType == VisualizationType.BorderOnly) ||
                        (visualizationType == VisualizationType.Name))
                    {
                        Graphics g = Graphics.FromImage(bitmap);
                        int      i = 0;

                        // highlight each found glyph
                        foreach (ExtractedGlyphData glyphData in glyphs)
                        {
                            List <IntPoint> glyphPoints = (glyphData.RecognizedGlyph == null) ?
                                                          glyphData.Quadrilateral : glyphData.RecognizedQuadrilateral;

                            Pen pen = new Pen(((glyphData.RecognizedGlyph == null) || (glyphData.RecognizedGlyph.UserData == null)) ?
                                              Color.Red : ((GlyphVisualizationData)glyphData.RecognizedGlyph.UserData).Color, 3);

                            // highlight border
                            g.DrawPolygon(pen, ToPointsArray(glyphPoints));

                            string glyphTitle = null;

                            // prepare glyph's title
                            if ((visualizationType == VisualizationType.Name) && (glyphData.RecognizedGlyph != null))
                            {
                                glyphTitle = string.Format("{0}: {1}",
                                                           glyphIDs[i], glyphData.RecognizedGlyph.Name);
                            }
                            else
                            {
                                glyphTitle = string.Format("Tracking ID: {0}", glyphIDs[i]);
                            }

                            // show glyph's title
                            if (!string.IsNullOrEmpty(glyphTitle))
                            {
                                // get glyph's center point
                                IntPoint minXY, maxXY;
                                PointsCloud.GetBoundingRectangle(glyphPoints, out minXY, out maxXY);
                                IntPoint center = (minXY + maxXY) / 2;

                                // glyph's name size
                                SizeF nameSize = g.MeasureString(glyphTitle, defaultFont);

                                // paint the name
                                Brush brush = new SolidBrush(pen.Color);

                                g.DrawString(glyphTitle, defaultFont, brush,
                                             new System.Drawing.Point(center.X - (int)nameSize.Width / 2, center.Y - (int)nameSize.Height / 2));

                                brush.Dispose( );
                            }

                            i++;
                            pen.Dispose( );
                        }
                    }
                    else if (visualizationType == VisualizationType.Image)
                    {
                        // lock image for further processing
                        BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                                                                ImageLockMode.ReadWrite, bitmap.PixelFormat);
                        UnmanagedImage unmanagedImage = new UnmanagedImage(bitmapData);

                        // highlight each found glyph
                        foreach (ExtractedGlyphData glyphData in glyphs)
                        {
                            if ((glyphData.RecognizedGlyph != null) && (glyphData.RecognizedGlyph.UserData != null))
                            {
                                GlyphVisualizationData visualization =
                                    (GlyphVisualizationData)glyphData.RecognizedGlyph.UserData;

                                if (visualization.ImageName != null)
                                {
                                    // get image associated with the glyph
                                    Bitmap glyphImage = EmbeddedImageCollection.Instance.GetImage(visualization.ImageName);

                                    if (glyphImage != null)
                                    {
                                        // put glyph's image onto the glyph using quadrilateral transformation
                                        quadrilateralTransformation.SourceImage = glyphImage;
                                        quadrilateralTransformation.DestinationQuadrilateral = glyphData.RecognizedQuadrilateral;

                                        quadrilateralTransformation.ApplyInPlace(unmanagedImage);
                                    }
                                }
                            }
                        }

                        bitmap.UnlockBits(bitmapData);
                    }
                }
            }

            return(glyphs);
        }