Example #1
0
        private static ImageShapeContext CreateShapeContext(string path, string fontFamily, int samplecount, char c)
        {
            MyTimer timer = new MyTimer();
            Font font = new Font(fontFamily, 240);
            string file = Path.Combine(path, String.Format("{0}-{1}.bmp", fontFamily, c));
            string fontpath = Path.Combine(path, fontFamily);
            Image<Bgr, Byte> img = new Image<Bgr, byte>(400, 400);
            using (Graphics g = Graphics.FromImage(img.Bitmap)) {
                g.Clear(Color.Black);
                g.DrawString(c.ToString(), font, Brushes.White, 0, 0);
            }
            timer.Restart();
            var canny = img.Convert<Gray, Byte>().Canny(new Gray(100), new Gray(60));
            timer.StopAndSay("Canny");

            timer.Restart();
            List<Point> edge = new List<Point>();
            for (int y = 0; y < canny.Height; ++y) {
                for (int x = 0; x < canny.Width; ++x) {
                    if (canny[y, x].Intensity > 0) {
                        edge.Add(new Point(x, y));
                    }
                }
            }
            var sample = edge.Sample(samplecount);
            timer.StopAndSay("Sample");
            using (var sw = new StreamWriter(Path.Combine(path, String.Format("!Sample-{0}-{1}.txt", fontFamily, c)))) {
                foreach (var p in sample) {
                    sw.WriteLine("{0},{1}", p.X, p.Y);
                }
            }

            timer.Restart();
            double min, max;
            var norm = Jim.OCR.Algorithms.ShapeContext.Normalize(sample, out min, out max);
            timer.StopAndSay("Normalize");

            timer.Restart();
            var scs = new ImageShapeContext(sample);
            for (int i = 0; i < norm.Length; ++i) {
                scs.ShapeContests[i] = new Jim.OCR.Algorithms.ShapeContext(norm[i]);
                //var his = sc.Histogram.ToImage();
                var p = sample[i];
                //string hisfile = Path.Combine(fontpath, string.Format("[{0},{1}].bmp", p.X, p.Y));
                //his.Save(hisfile);
                img[p] = new Bgr(0, 0, 255);
            }
            timer.StopAndSay("ShapeContext");
            //foreach (var p in sample) {
            //    var sc = new ShapeContext(p, sample);
            //    var his = sc.Histogram.ToImage();
            //    string hisfile = Path.Combine(fontpath, string.Format("[{0},{1}].bmp", p.X, p.Y));
            //    his.Save(hisfile);
            //    img[p] = new Bgr(0, 0, 255);
            //}

            //var contours = img.Convert<Gray, Byte>().FindContours();
            //img.Draw(contours, new Bgr(0, 0, 255), new Bgr(0, 255, 0), 2, 1);

            //dfsContour(img, contours, 0, 2);
            //foreach (var ii in contours.) {
            //    //Console.WriteLine
            //    img[ii] = new Bgr(0, 0, 255);
            //}
            //img.Save(file);

            return scs;
        }
Example #2
0
        private static void MatchShapeContext(string path, string fa, string fb, char c)
        {
            MyTimer timer = new MyTimer();
            Console.WriteLine("Doing...{0}", c);
            var sca = CreateShapeContext(path, fa, 100, c);
            var scb = CreateShapeContext(path, fb, 100, c);

            var match = new ShapeContextMatching(sca.ShapeContests, scb.ShapeContests);

            timer.Restart();
            match.BuildCostGraph();
            timer.StopAndSay("Build Graph");

            timer.Restart();
            var km = match.Match();
            timer.StopAndSay("Match");
            Console.WriteLine("Match Result:{0}", km.MatchResult);

            int ax = (int)sca.Points.Average(p => p.X),
                ay = (int)sca.Points.Average(p => p.Y),
                bx = (int)scb.Points.Average(p => p.X),
                by = (int)scb.Points.Average(p => p.Y);
            int dx = bx - ax,
                dy = by - ay;
            int scale = 4;
            Image<Bgr, Byte> img = new Image<Bgr, byte>(400 * scale, 400 * scale);
            string scdir = Path.Combine(path, c.ToString());
            if (Directory.Exists(scdir)) Directory.Delete(scdir, true);
            Directory.CreateDirectory(scdir);
            Font fs = new Font("Arial", 12);
            using (Graphics g = Graphics.FromImage(img.Bitmap)) {
                g.Clear(Color.Black);

                Font font1 = new Font(fa, 240 * scale);
                Font font2 = new Font(fb, 240 * scale);
                g.DrawString(c.ToString(), font1, new SolidBrush(Color.FromArgb(30, 255, 0, 0)), new Point(0, 0));
                g.DrawString(c.ToString(), font2, new SolidBrush(Color.FromArgb(30, 0, 255, 0)), new Point(-dx * scale, -dy * scale));

                for (int j = 0; j < scb.Points.Count; ++j) {
                    int i = km.MatchPair[j + sca.Points.Count];
                    Point pb = scb.Points[j], pa = sca.Points[i];
                    pb.X -= dx;
                    pb.Y -= dy;
                    int r = 3;
                    g.DrawEllipse(Pens.Red, pa.X * scale - r, pa.Y * scale - r, r * 2 + 1, r * 2 + 1);
                    g.DrawEllipse(Pens.Green, pb.X * scale - r, pb.Y * scale - r, r * 2 + 1, r * 2 + 1);

                    g.DrawLine(Pens.Gray, new Point(pa.X * scale, pa.Y * scale), new Point(pb.X * scale, pb.Y * scale));

                    Point ps = new Point(pa.X * scale, pa.Y * scale);
                    g.DrawString(i.ToString(), fs, Brushes.White, ps);

                    string hisa = Path.Combine(scdir, String.Format("{0}-{1}.bmp", i, fa));
                    string hisb = Path.Combine(scdir, String.Format("{0}-{1}.bmp", i, fb));
                    sca.ShapeContests[i].Histogram.ToImage().Save(hisa);
                    scb.ShapeContests[j].Histogram.ToImage().Save(hisb);
                }
            }
            img.Save(Path.Combine(path, String.Format("{0}-{1}-{2}.bmp", fa, fb, c)));
        }