예제 #1
0
        public Tuple<string, int> Recognize(Image<Gray, byte> src)
        {
            var list = src.getEdge().Sample(100);

            #region 计算形状上下文
            int r = 15;
            var scall = Jim.OCR.ShapeContext2D.ShapeContext.ComputeSC(list);
            var scq = new double[r, 60];
            for (int i = 0; i < r; ++i) {
                int pos = rand.Next(list.Count);
                for (int k = 0; k < 60; ++k) {
                    scq[i, k] = scall[pos, k];
                }
            }
            #endregion
            #region 计算距离
            var dists = new ValueIndexPair<double>[SC.template_sc.Length];
            for (int i = 0; i < SC.template_sc.Length; ++i) {
                double[,] sci = SC.template_sc[i];
                double[,] costmat = Jim.OCR.ShapeContext2D.ShapeContext.HistCost(scq, sci);
                double cost = 0;
                for (int j = 0; j < r; ++j) {
                    double min = double.MaxValue;
                    for (int u = 0; u < 100; ++u) {
                        double val = costmat[j, u];
                        if (val < min) min = val;
                    }
                    cost += min;
                }
                dists[i] = new ValueIndexPair<double> { Value = cost, Index = i };
            }
            #endregion

            #region 对结果排序
            var arr = dists
                .OrderBy(p => p.Value)
                .Select(p => new { Distance = p.Value, Char = SC.template_chars[p.Index] })
                //.Where(p => "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789".IndexOf(p.Char) != -1)
                .Where(p => Filter.IndexOf(p.Char) != -1)
                .ToArray();
            Dictionary<string, int> matchcount = new Dictionary<string, int>();
            foreach (var pair in arr.Take(Knn)) {
                string ch = pair.Char;
                matchcount[ch] = matchcount.ContainsKey(ch) ? matchcount[ch] + 1 : 1;
            }
            var match = matchcount.Select(pair => new { Count = pair.Value, Ch = pair.Key })
                                  .Where(v => v.Count > 0)
                                  .OrderByDescending(v => v.Count).ToArray();
            //string result = "";
            //foreach (var m in match.Take(3)) {
            //    result += String.Format("Char:'{0}',Accuracy:{1}/{2}\t", m.Ch, m.Count, knn);
            //}
            #endregion

            return new Tuple<string, int> { First = match[0].Ch, Second = match[0].Count };
        }
예제 #2
0
        private void recognize_rsc()
        {
            if (template_histograms == null) {
                MessageBox.Show("尚未读取模板。");
                return;
            }
            new Thread(new ThreadStart(() => {
                clearLog();
                showStatus("搜索中");
                foreach (var challenge in splitted_chars) {
                    timer.Restart();
                    var list = challenge.getEdge().Sample(100);

                    #region 计算形状上下文
                    int r = 15;
                    var scall = Jim.OCR.ShapeContext2D.ShapeContext.ComputeSC(list);
                    var scq = new double[r, 60];
                    for (int i = 0; i < r; ++i) {
                        int pos = rand.Next(list.Count);
                        for (int k = 0; k < 60; ++k) {
                            scq[i, k] = scall[pos, k];
                        }
                    }
                    #endregion
                    #region 计算距离
                    var dists = new ValueIndexPair<double>[template_sc.Length];
                    for (int i = 0; i < template_sc.Length; ++i) {
                        double[,] sci = template_sc[i];
                        double[,] costmat = Jim.OCR.ShapeContext2D.ShapeContext.HistCost(scq, sci);
                        double cost = 0;
                        for (int j = 0; j < r; ++j) {
                            double min = double.MaxValue;
                            for (int u = 0; u < 100; ++u) {
                                double val = costmat[j, u];
                                if (val < min) min = val;
                            }
                            cost += min;
                        }
                        dists[i] = new ValueIndexPair<double> { Value = cost, Index = i };
                        showProgress((i + 1.0) / template_histograms.Length);
                    }
                    #endregion

                    #region 对结果排序
                    var arr = dists
                        .OrderBy(p => p.Value)
                        .Select(p => new { Distance = p.Value, Char = template_chars[p.Index] })
                        //.Where(p => "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789".IndexOf(p.Char) != -1)
                        .ToArray();
                    Dictionary<string, int> matchcount = new Dictionary<string, int>();
                    int knn = 10;
                    foreach (var pair in arr.Take(knn)) {
                        string ch = pair.Char;
                        matchcount[ch] = matchcount.ContainsKey(ch) ? matchcount[ch] + 1 : 1;
                    }
                    var match = matchcount.Select(pair => new { Count = pair.Value, Ch = pair.Key })
                                          .Where(v => v.Count > 0)
                                          .OrderByDescending(v => v.Count).ToArray();
                    string result = "";
                    foreach (var m in match.Take(3)) {
                        result += String.Format("Char:'{0}',Accuracy:{1}/{2}\t", m.Ch, m.Count, knn);
                    }
                    appendLine(result);
                    #endregion
                }
                appendLine("-----------------------------------------------------");
                showStatus("搜索完成,共用时{0}ms。", timer.Stop());
            })).Start();
        }
예제 #3
0
        public Tuple <string, int> Recognize(Image <Gray, byte> src)
        {
            var list = src.getEdge().Sample(100);

            #region 计算形状上下文
            int r     = 15;
            var scall = Jim.OCR.ShapeContext2D.ShapeContext.ComputeSC(list);
            var scq   = new double[r, 60];
            for (int i = 0; i < r; ++i)
            {
                int pos = rand.Next(list.Count);
                for (int k = 0; k < 60; ++k)
                {
                    scq[i, k] = scall[pos, k];
                }
            }
            #endregion
            #region 计算距离
            var dists = new ValueIndexPair <double> [SC.template_sc.Length];
            for (int i = 0; i < SC.template_sc.Length; ++i)
            {
                double[,] sci     = SC.template_sc[i];
                double[,] costmat = Jim.OCR.ShapeContext2D.ShapeContext.HistCost(scq, sci);
                double cost = 0;
                for (int j = 0; j < r; ++j)
                {
                    double min = double.MaxValue;
                    for (int u = 0; u < 100; ++u)
                    {
                        double val = costmat[j, u];
                        if (val < min)
                        {
                            min = val;
                        }
                    }
                    cost += min;
                }
                dists[i] = new ValueIndexPair <double> {
                    Value = cost, Index = i
                };
            }
            #endregion

            #region 对结果排序
            var arr = dists
                      .OrderBy(p => p.Value)
                      .Select(p => new { Distance = p.Value, Char = SC.template_chars[p.Index] })
                      //.Where(p => "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789".IndexOf(p.Char) != -1)
                      .Where(p => Filter.IndexOf(p.Char) != -1)
                      .ToArray();
            Dictionary <string, int> matchcount = new Dictionary <string, int>();
            foreach (var pair in arr.Take(Knn))
            {
                string ch = pair.Char;
                matchcount[ch] = matchcount.ContainsKey(ch) ? matchcount[ch] + 1 : 1;
            }
            var match = matchcount.Select(pair => new { Count = pair.Value, Ch = pair.Key })
                        .Where(v => v.Count > 0)
                        .OrderByDescending(v => v.Count).ToArray();
            //string result = "";
            //foreach (var m in match.Take(3)) {
            //    result += String.Format("Char:'{0}',Accuracy:{1}/{2}\t", m.Ch, m.Count, knn);
            //}
            #endregion

            return(new Tuple <string, int> {
                First = match[0].Ch, Second = match[0].Count
            });
        }
예제 #4
0
        private void recognize_rsc()
        {
            if (template_histograms == null)
            {
                MessageBox.Show("尚未读取模板。");
                return;
            }
            new Thread(new ThreadStart(() => {
                clearLog();
                showStatus("搜索中");
                foreach (var challenge in splitted_chars)
                {
                    timer.Restart();
                    var list = challenge.getEdge().Sample(100);

                    #region 计算形状上下文
                    int r     = 15;
                    var scall = Jim.OCR.ShapeContext2D.ShapeContext.ComputeSC(list);
                    var scq   = new double[r, 60];
                    for (int i = 0; i < r; ++i)
                    {
                        int pos = rand.Next(list.Count);
                        for (int k = 0; k < 60; ++k)
                        {
                            scq[i, k] = scall[pos, k];
                        }
                    }
                    #endregion
                    #region 计算距离
                    var dists = new ValueIndexPair <double> [template_sc.Length];
                    for (int i = 0; i < template_sc.Length; ++i)
                    {
                        double[,] sci     = template_sc[i];
                        double[,] costmat = Jim.OCR.ShapeContext2D.ShapeContext.HistCost(scq, sci);
                        double cost       = 0;
                        for (int j = 0; j < r; ++j)
                        {
                            double min = double.MaxValue;
                            for (int u = 0; u < 100; ++u)
                            {
                                double val = costmat[j, u];
                                if (val < min)
                                {
                                    min = val;
                                }
                            }
                            cost += min;
                        }
                        dists[i] = new ValueIndexPair <double> {
                            Value = cost, Index = i
                        };
                        showProgress((i + 1.0) / template_histograms.Length);
                    }
                    #endregion

                    #region 对结果排序
                    var arr = dists
                              .OrderBy(p => p.Value)
                              .Select(p => new { Distance = p.Value, Char = template_chars[p.Index] })
                              //.Where(p => "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789".IndexOf(p.Char) != -1)
                              .ToArray();
                    Dictionary <string, int> matchcount = new Dictionary <string, int>();
                    int knn = 10;
                    foreach (var pair in arr.Take(knn))
                    {
                        string ch      = pair.Char;
                        matchcount[ch] = matchcount.ContainsKey(ch) ? matchcount[ch] + 1 : 1;
                    }
                    var match = matchcount.Select(pair => new { Count = pair.Value, Ch = pair.Key })
                                .Where(v => v.Count > 0)
                                .OrderByDescending(v => v.Count).ToArray();
                    string result = "";
                    foreach (var m in match.Take(3))
                    {
                        result += String.Format("Char:'{0}',Accuracy:{1}/{2}\t", m.Ch, m.Count, knn);
                    }
                    appendLine(result);
                    #endregion
                }
                appendLine("-----------------------------------------------------");
                showStatus("搜索完成,共用时{0}ms。", timer.Stop());
            })).Start();
        }