/// <summary>
        /// Converts the specified <paramref name="img"/> to a <see cref="Pix"/>.
        /// </summary>
        /// <param name="img">The source image to be converted.</param>
        /// <returns>The converted pix.</returns>
        public Pix Convert(Bitmap img)
        {
            var pixDepth = GetPixDepth(img.PixelFormat);
            var pix      = Pix.Create(img.Width, img.Height, pixDepth);

            pix.XRes = (int)Math.Round(img.HorizontalResolution);
            pix.YRes = (int)Math.Round(img.VerticalResolution);

            BitmapData imgData = null;
            PixData    pixData = null;

            try
            {
                // TODO: Set X and Y resolution

                if ((img.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed)
                {
                    CopyColormap(img, pix);
                }

                // transfer data
                imgData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, img.PixelFormat);
                pixData = pix.GetData();

                if (imgData.PixelFormat == PixelFormat.Format32bppArgb)
                {
                    TransferDataFormat32bppArgb(imgData, pixData);
                }
                else if (imgData.PixelFormat == PixelFormat.Format32bppRgb)
                {
                    TransferDataFormat32bppRgb(imgData, pixData);
                }
                else if (imgData.PixelFormat == PixelFormat.Format24bppRgb)
                {
                    TransferDataFormat24bppRgb(imgData, pixData);
                }
                else if (imgData.PixelFormat == PixelFormat.Format8bppIndexed)
                {
                    TransferDataFormat8bppIndexed(imgData, pixData);
                }
                else if (imgData.PixelFormat == PixelFormat.Format1bppIndexed)
                {
                    TransferDataFormat1bppIndexed(imgData, pixData);
                }
                return(pix);
            }
            catch (Exception)
            {
                pix.Dispose();
                throw;
            }
            finally
            {
                if (imgData != null)
                {
                    img.UnlockBits(imgData);
                }
            }
        }
Exemplo n.º 2
0
        public void ReadImageToPix()
        {
            Mock <IReadPicture> mock = new Mock <IReadPicture>();

            mock.Setup(m => m.ReadImageFromFile("Test"))
            .Returns(Pix.Create(200, 200, 32));

            Ocr ocr  = new Ocr(mock.Object);
            var test = ocr.ReadFile("Test");

            Assert.True(test);
        }
Exemplo n.º 3
0
        public void CanReadAndWriteData(int depth)
        {
            using (var pix = Pix.Create(Width, Height, depth))
            {
                var pixData = pix.GetData();

                for (var y = 0; y < Height; y++)
                {
                    var line = (uint *)pixData.Data + (y * pixData.WordsPerLine);
                    for (var x = 0; x < Width; x++)
                    {
                        var  val = (uint)((y * Width + x) % (1 << depth));
                        uint readVal;
                        if (depth == 1)
                        {
                            PixData.SetDataBit(line, x, val);
                            readVal = PixData.GetDataBit(line, x);
                        }
                        else if (depth == 2)
                        {
                            PixData.SetDataDIBit(line, x, val);
                            readVal = PixData.GetDataDIBit(line, x);
                        }
                        else if (depth == 4)
                        {
                            PixData.SetDataQBit(line, x, val);
                            readVal = PixData.GetDataQBit(line, x);
                        }
                        else if (depth == 8)
                        {
                            PixData.SetDataByte(line, x, val);
                            readVal = PixData.GetDataByte(line, x);
                        }
                        else if (depth == 16)
                        {
                            PixData.SetDataTwoByte(line, x, val);
                            readVal = PixData.GetDataTwoByte(line, x);
                        }
                        else if (depth == 32)
                        {
                            PixData.SetDataFourByte(line, x, val);
                            readVal = PixData.GetDataFourByte(line, x);
                        }
                        else
                        {
                            throw new NotSupportedException();
                        }

                        Assert.AreEqual(readVal, val);
                    }
                }
            }
        }
Exemplo n.º 4
0
        private int OCRNumber(Mat skillValue, string name = "")
        {
            if (!string.IsNullOrEmpty(name))
            {
                //skillValue.SaveImage($"{name}.png");
            }
            Pix pix;

            if (skillValue.Type() == MatType.CV_8UC3)
            {
                // 3 bytes
                pix      = Pix.Create(skillValue.Width, skillValue.Height, 32);
                pix.XRes = 72;
                pix.YRes = 72;
                PixData pixData = null;
                try
                {
                    pixData = pix.GetData();
                    MatOfByte3 mat3 = new MatOfByte3(skillValue);
                    TransferData(mat3, pixData);
                }
                catch (Exception)
                {
                    pix.Dispose();
                    //throw;
                    return(0);
                }
            }
            else
            {
                skillValue.SaveImage("temp.png");
                pix = Pix.LoadFromFile("temp.png");
            }

            using (Page resultPage = _tessEngine.Process(pix))
            {
                string data = resultPage.GetText();

                if (int.TryParse(data, out int result))
                {
                    return(result);
                }
            }

            return(0);
        }
Exemplo n.º 5
0
        public unsafe static OcrNode OCR(
            TesseractEngine engine,
            Mat input,
            int resolution)
        {
            using var pix = Pix.Create(input.Width, input.Height, 8);

            pix.XRes = resolution;
            pix.YRes = resolution;

            var pixData      = pix.GetData();
            var data         = (uint *)pixData.Data;
            var wordsPerLine = pixData.WordsPerLine;

            input.ForEachAsByte((value, position) =>
            {
                var y     = position[0];
                var x     = position[1];
                var color = *value;

                PixData.SetDataByte(data + y * wordsPerLine, x, color);
            });


            using var page = engine.Process(pix);

            var ocrPage = page.GetTsvText(1).
                          Split("\n", StringSplitOptions.RemoveEmptyEntries).
                          Select(line => line.Split("\t")).
                          Select(columns => new OcrNode
            {
                Level      = int.Parse(columns[0]),
                PageNum    = int.Parse(columns[1]),
                BlockNum   = int.Parse(columns[2]),
                ParNum     = int.Parse(columns[3]),
                LineNum    = int.Parse(columns[4]),
                WordNum    = int.Parse(columns[5]),
                Left       = int.Parse(columns[6]),
                Top        = int.Parse(columns[7]),
                Width      = int.Parse(columns[8]),
                Height     = int.Parse(columns[9]),
                Confidence = int.Parse(columns[10]),
                Text       = columns.Length > 11 ? columns[11] : null
            }).
                          Aggregate((prev, next) =>
            {
                if (prev == null)
                {
                    if (next.Level != 1)
                    {
                        throw new InvalidOperationException("nodes");
                    }
                }
                else
                {
                    if ((next.Level <= 0) || (next.Level > prev.Level + 1))
                    {
                        throw new InvalidOperationException("nodes");
                    }

                    while (next.Level != prev.Level + 1)
                    {
                        prev = prev.Parent;
                    }

                    next.Parent = prev;

                    if (prev.Children == null)
                    {
                        prev.Children = new List <OcrNode>();
                    }

                    next.Parent.Children.Add(next);
                }

                return(next);
            });

            while (ocrPage.Parent != null)
            {
                ocrPage = ocrPage.Parent;
            }

            ocrPage.Text = page.GetText();

            return(ocrPage);
        }