예제 #1
0
        private void GetCharPath()
        {
            const string charPath = "MYsource\\font";
            var          dir      = new DirectoryInfo(charPath);
            var          fil      = dir.GetFiles();

            foreach (var fileInfo in fil)
            {
                var name   = fileInfo.FullName;
                var index  = name.IndexOf(".", StringComparison.Ordinal);
                var suffix = name.Substring(index + 1);
                if (!suffix.Equals("bmp"))
                {
                    continue;
                }
                _charList.Add(name);
            }
            PositioningLicence.WriteToFile("font number is: " + _charList.Count + Environment.NewLine);
        }
예제 #2
0
        private void CharRegonize_OnClick(object sender, RoutedEventArgs e)
        {
            GetNumPath();
            GetCharPath();
            //MessageBox.Show(_charList.Count + "  " + _numList.Count);
            foreach (var s in _charList)
            {
                PositioningLicence.WriteToFile(s + Environment.NewLine);
            }

            _recognizeResult = "";
            for (var i = 0; i < _bitmaplist.Count; i++)
            {
                var picBitmap = _bitmaplist[i];
                if (i == 0)
                {
                    var minDiss  = 1000;
                    var filename = "";
                    foreach (var charPath in _charList)
                    {
                        var charBitmap = (Bitmap)Image.FromFile(charPath, false);
                        var x          = 0;
                        BitmapDistance(picBitmap, charBitmap, ref x);
                        if (minDiss <= x)
                        {
                            continue;
                        }
                        minDiss  = x;
                        filename = charPath;
                    }
                    PositioningLicence.WriteToFile("最接近的汉字字符:" + filename + Environment.NewLine);
                    var fontArray = filename.Split('\\');
                    PositioningLicence.WriteToFile("分割成字串个数:" + fontArray.Length + Environment.NewLine);
                    var reChar = fontArray[fontArray.Length - 1][0];
                    PositioningLicence.WriteToFile("字符是:" + reChar + Environment.NewLine);
                    _recognizeResult += reChar;
                }
                else
                {
                    var minDiss  = 1000;
                    var filename = "";
                    foreach (var numPath in _numList)
                    {
                        var numBitmap = (Bitmap)Image.FromFile(numPath, false);
                        var x         = 0;
                        BitmapDistance(picBitmap, numBitmap, ref x);
                        if (minDiss <= x)
                        {
                            continue;
                        }
                        minDiss  = x;
                        filename = numPath;
                    }
                    PositioningLicence.WriteToFile("最接近的数字字符:" + filename + Environment.NewLine);
                    var fontArray = filename.Split('\\');
                    PositioningLicence.WriteToFile("分割成字串个数:" + fontArray.Length + Environment.NewLine);
                    var reChar = fontArray[fontArray.Length - 1][0];
                    PositioningLicence.WriteToFile("字符是:" + reChar + Environment.NewLine);
                    _recognizeResult += reChar;
                }
            }

            TextBlockrecogResult.Text = _recognizeResult;
        }
예제 #3
0
        private void CutPlant_OnClick(object sender, RoutedEventArgs e)
        {
            output_bitmap(_bitmapLicence);
            var rowSum = new int[_bitmapLicence.Height];

            for (var j = 0; j < _bitmapLicence.Height; j++)
            {
                var sum = 0;
                for (var i = 0; i < _bitmapLicence.Width; i++)
                {
                    int x = _bitmapLicence.GetPixel(i, j).R;
                    x    = x == 0 ? 0 : 1;
                    sum += x;
                }
                rowSum[j] = sum;
            }
            int py0 = 0, py1 = _bitmapLicence.Width - 1;

            for (var i = 0; i < _bitmapLicence.Height; i++)
            {
                var rate = (double)rowSum[i] / _bitmapLicence.Width * 1.0;
                if (rate > 0.3 && rate < 0.6)
                {
                    break;
                }
                py0 = i;
            }

            for (var i = _bitmapLicence.Height - 1; i >= 0; i--)
            {
                var rate = (double)rowSum[i] / _bitmapLicence.Width * 1.0;
                if (rate > 0.3 && rate < 0.6)
                {
                    break;
                }
                py1 = i;
            }
            PositioningLicence.WriteToFile("Py0 is:" + py0 + Environment.NewLine + "Py1 is:" + py1 + Environment.NewLine);
            PositioningLicence.WriteToFile("height is:" + _bitmapLicence.Height + Environment.NewLine);

            var colSum = new int[_bitmapLicence.Width];

            for (var i = 0; i < _bitmapLicence.Width; i++)
            {
                var sum = 0;
                for (var j = py0; j <= py1; j++)
                {
                    int x = _bitmapLicence.GetPixel(i, j).R;
                    x    = x == 0 ? 0 : 1;
                    sum += x;
                }
                colSum[i] = sum;
            }

            var x0 = 0;
            var x1 = _bitmapLicence.Width - 1;

            while (x0 < _bitmapLicence.Height && (colSum[x0] == 0 || colSum[x0] == _bitmapLicence.Height))
            {
                x0++;
            }
            while (x0 < _bitmapLicence.Height && (colSum[x0] == 0 || colSum[x0] == py1 - py0))
            {
                x0++;
            }

            while (x1 >= 0 && (colSum[x1] == 0 || colSum[x1] == _bitmapLicence.Height))
            {
                x1--;
            }
            while (x1 >= 0 && (colSum[x1] == 0 || colSum[x1] == py1 - py0))
            {
                x1--;
            }
            PositioningLicence.WriteToFile("x0 is:" + x0 + Environment.NewLine + "x1 is:" + x1 + Environment.NewLine);
            PositioningLicence.WriteToFile("width is:" + _bitmapLicence.Width + Environment.NewLine);

            _smallBitmap = _bitmapLicence.Clone(new Rectangle(x0, py0, x1 - x0, py1 - py0), PixelFormat.DontCare);
            t_show_pic(_smallBitmap);


            var subColSum = new int[_smallBitmap.Width];

            for (var i = 0; i < _smallBitmap.Width; i++)
            {
                var sum = 0;
                for (var j = 0; j < _smallBitmap.Height; j++)
                {
                    int x = _smallBitmap.GetPixel(i, j).R;
                    x    = x == 0 ? 0 : 1;
                    sum += x;
                }
                subColSum[i] = sum;
            }

            PositioningLicence.WriteToFile("smallbitmap is:" + Environment.NewLine);
            for (var j = 0; j < _smallBitmap.Height; j++)
            {
                for (var i = 0; i < _smallBitmap.Width; i++)
                {
                    int x = _smallBitmap.GetPixel(i, j).R;
                    x = x == 0 ? 0 : 1;
                    PositioningLicence.WriteToFile(x + " ");
                }
                PositioningLicence.WriteToFile(Environment.NewLine);
            }

            PositioningLicence.WriteToFile("每列像素值" + Environment.NewLine);
            foreach (var i in subColSum)
            {
                PositioningLicence.WriteToFile(i + " ");
            }

            var len  = subColSum.Count(t => t == 0);
            var mark = new int[len];
            var ct   = 0;

            for (var i = 0; i < _smallBitmap.Width; i++)
            {
                if (subColSum[i] == 0)
                {
                    mark[ct++] = i;
                }
            }

            PositioningLicence.WriteToFile(Environment.NewLine + "0 的位置:" + Environment.NewLine);
            foreach (var i in mark)
            {
                PositioningLicence.WriteToFile(i + " ");
            }
            PositioningLicence.WriteToFile(Environment.NewLine);

            var c    = 0;
            var list = new List <List <int> >();

            while (c < len)
            {
                var j = c + 1;
                while (j < len)
                {
                    if (mark[j] - mark[j - 1] == 1)
                    {
                        j++;
                    }
                    else
                    {
                        break;
                    }
                }
                var li = new List <int> {
                    mark[c], mark[j - 1]
                };
                list.Add(li);
                c = j;
            }

            PositioningLicence.WriteToFile("0间隔如下:" + Environment.NewLine);
            foreach (var fir in list)
            {
                foreach (var i in fir)
                {
                    PositioningLicence.WriteToFile(i + " ");
                }
                PositioningLicence.WriteToFile(Environment.NewLine);
            }

            len = _smallBitmap.Width;
            var templist = new List <int>();

            foreach (var lir in list)
            {
                foreach (var i in lir)
                {
                    templist.Add(i);
                }
            }
            templist.Insert(0, -1);
            templist.Add(len);
            var charsub = new List <List <int> >();

            for (var i = 0; i < templist.Count - 2; i += 2)
            {
                charsub.Add(new List <int> {
                    templist[i] + 1, templist[i + 1] - 1
                });
            }

            PositioningLicence.WriteToFile("字间隔如下:" + Environment.NewLine);
            foreach (var fir in charsub)
            {
                foreach (var i in fir)
                {
                    PositioningLicence.WriteToFile(i + " ");
                }
                PositioningLicence.WriteToFile(Environment.NewLine);
            }

            PositioningLicence.WriteToFile("各个字符大小:" + Environment.NewLine);

            //MessageBox.Show(smallBitmap.Width + "  " + smallBitmap.Height);
            //var tpt = smallBitmap.Clone(new Rectangle(0, 4, smallBitmap.Width, smallBitmap.Height), PixelFormat.DontCare);
            foreach (var fir in charsub)
            {
                var left       = fir[0];
                var right      = fir[1];
                var tempbitmap = _smallBitmap.Clone(new Rectangle(left, 0, right - left, _smallBitmap.Height),
                                                    PixelFormat.DontCare);

                var bitmap = new Bitmap(9, 16);
                var g      = Graphics.FromImage(bitmap);
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(tempbitmap, new Rectangle(0, 0, 9, 16),
                            new Rectangle(0, 0, tempbitmap.Width, tempbitmap.Height), GraphicsUnit.Pixel);
                g.Dispose();

                PositioningLicence.WriteToFile(bitmap.Width + "x" + bitmap.Height + Environment.NewLine);
                _bitmaplist.Add(bitmap);
            }

            var cnt = 0;

            foreach (var image in images.Children.OfType <System.Windows.Controls.Image>())
            {
                SetImages(image, _bitmaplist[cnt++]);
                if (cnt > _bitmaplist.Count)
                {
                    break;
                }
            }
        }
예제 #4
0
        private void Plant_binary_OnClick(object sender, RoutedEventArgs e)
        {
            const int n = 256;
            var       plantPixelCount = new int[n];

            for (var i = 0; i < _bitmapLicence.Width; i++)
            {
                for (var j = 0; j < _bitmapLicence.Height; j++)
                {
                    var x = _bitmapLicence.GetPixel(i, j).R;
                    plantPixelCount[x]++;
                }
            }

            var totalValue = plantPixelCount.Select((t, i) => i * t).Sum();

            //MessageBox.Show("totalValue is :" + totalValue+Environment.NewLine);

            PositioningLicence.WriteToFile("totalValue is :" + totalValue + Environment.NewLine);
            double sum0    = 0;
            var    w0      = 0;
            double maximun = 0;
            var    total   = _bitmapLicence.Width * _bitmapLicence.Height;
            byte   level   = 0;

            for (byte i = 0; i < plantPixelCount.Length; i++)
            {
                w0 += plantPixelCount[i];
                if (w0 == 0)
                {
                    continue;
                }
                var w1 = total - w0;
                if (w1 == 0)
                {
                    break;
                }
                sum0 += i * plantPixelCount[i];
                var m0  = sum0 / w0;
                var m1  = (totalValue - sum0) / w1;
                var icv = w0 * w1 * (m0 - m1) * (m0 - m1);
                if (!(icv >= maximun))
                {
                    continue;
                }
                level   = i;
                maximun = icv;
                //PositioningLicence.WriteToFile(m0+" "+m1+" "+icv+" "+Environment.NewLine);
            }

            //MessageBox.Show(@"level is: "+level);
            PositioningLicence.WriteToFile(@"level is: " + level + Environment.NewLine);
            for (var i = 0; i < _bitmapLicence.Width; i++)
            {
                for (var j = 0; j < _bitmapLicence.Height; j++)
                {
                    int x     = _bitmapLicence.GetPixel(i, j).R;
                    var value = x > level ? 255 : 0;
                    _bitmapLicence.SetPixel(i, j, Color.FromArgb(value, value, value));
                }
            }
            t_show_pic(_bitmapLicence);

            /*var cnt = 0;
             * var str = "";
             * foreach (var b in plantPixelCount)
             * {
             *  Console.Write(b+@" ");
             *  str += b + " ";
             *  if (cnt++ % 20 ==0 &&cnt-1!=0)
             *      str += Environment.NewLine;
             *
             * }
             * PositioningLicence.WriteToFile(str);*/
        }