Esempio n. 1
0
        private void button6_Click_1(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(imagePath);

            bmp = RemoveBlackEdge(bmp);
            gmseDeskew sk        = new gmseDeskew(bmp);
            double     skewangle = sk.GetSkewAngle();
            Bitmap     bmp11     = RotateImage(bmp, -skewangle);

            //新建第二个bitmap类型的bmp2变量,我这里是根据我的程序需要设置的。
            Bitmap bmp2 = new Bitmap(1120, 1435, PixelFormat.Format16bppRgb555);
            //将第一个bmp拷贝到bmp2中
            Graphics draw = Graphics.FromImage(bmp2);

            draw.DrawImage(bmp11, 0, 0);
            //          pictureBox1.Image = bmp2;//读取bmp2到picturebox
            //     name = openFileDialogEmpImage.FileName;
            //      openFileDialogEmpImage.Dispose();
            draw.Dispose();
            bmp.Dispose();//释放bmp文件资源
            //blackedge remove and return bmp
            pictureBox1.Image = null;
            pictureBox1.Image = bmp2;
            pictureBox1.Refresh();
        }
Esempio n. 2
0
        private void deskew()
        {
            Bitmap     bm = new Bitmap(img);
            gmseDeskew sk = new gmseDeskew(bm);

            sk.Binary(bm, true);
            sk.NoiseRemoval(bm);
            double skewangle = sk.GetSkewAngle();

            img = RotateImage(img, (float)-skewangle);
        }
Esempio n. 3
0
        private void button6_Click_1(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialogEmpImage = new OpenFileDialog();
                openFileDialogEmpImage.Filter = "*.jpg|*.jpg|*.png|*.png|*.bmp|*.bmp|*.tiff|*.tiff";//图片格式
                if (openFileDialogEmpImage.ShowDialog() == DialogResult.OK)
                {
                    //           isUpLoadPicture = true;//记录是否上传了相片,用于后面保存操作使用:是一个成员变
                    try
                    {
                        empUpLoadPictureRealPos = openFileDialogEmpImage.FileName;//实际的文件路径+文件名
                        String[] empImageData = empUpLoadPictureRealPos.Split('.');
                        //empImageData[1]:是上传的图片的后缀名
                        empUpLoadPictureFormat         = empImageData[1];
                        pictureBox1.Image              = Image.FromFile(empUpLoadPictureRealPos);//将图片显示在pitureBox控件中
                        this.pictureBox1.ImageLocation = openFileDialogEmpImage.FileName;
                    }
                    catch
                    {
                        MessageBox.Show("您选择的图片不能被读取或文件类型不对!", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("上传相片出错: " + ex.Message);
            }
            string st = "";

            st = pictureBox1.ImageLocation;
            st = st.Replace("\\", "\\\\");
            //           AutoCutBlackEdge(st);
            Bitmap     bmp       = new Bitmap(st);
            Bitmap     bmpOut1   = RemoveBlackEdge(bmp);
            gmseDeskew sk        = new gmseDeskew(bmpOut1);
            double     skewangle = sk.GetSkewAngle();
            Bitmap     bmpOut    = RotateImage(bmpOut1, -skewangle);

            //         pictureBox1.Image = bmpOut;
            string fnOut = "d:\\test\\555.jpg";

            //保存图像
            bmpOut.Save(fnOut, ImageFormat.Jpeg);
            MessageBox.Show("成功");
            pictureBox1.Image = null;
            pictureBox1.Refresh();
            pictureBox1.Image = bmpOut;
        }
Esempio n. 4
0
        protected override void deskewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (imageList == null)
            {
                MessageBox.Show(this, Properties.Resources.LoadImage, strProgName);
                return;
            }
            this.pictureBox1.Deselect();
            this.Cursor = Cursors.WaitCursor;

            gmseDeskew deskew         = new gmseDeskew((Bitmap)this.pictureBox1.Image);
            double     imageSkewAngle = deskew.GetSkewAngle();

            if ((imageSkewAngle > MINIMUM_DESKEW_THRESHOLD || imageSkewAngle < -(MINIMUM_DESKEW_THRESHOLD)))
            {
                imageList[imageIndex]  = ImageHelper.Rotate((Bitmap)imageList[imageIndex], -imageSkewAngle);
                this.pictureBox1.Image = new Bitmap(imageList[imageIndex]);
            }
            this.Cursor = Cursors.Default;
        }
Esempio n. 5
0
        /// <summary>
        /// 二值化识别
        /// </summary>
        /// <param name="imageFile"></param>
        /// <returns></returns>
        private string BinaryzationDiscern(string imageFile)
        {
            string number_str = "";

            using (Bitmap bmp = new Bitmap(imageFile))
            {
                // 灰度处理
                var bmps = Utils.ToGray(bmp);
                // 处理自动校正
                gmseDeskew sk        = new gmseDeskew(bmps);
                double     skewangle = sk.GetSkewAngle();
                Bitmap     bmpOut    = Utils.RotateImage(bmps, -skewangle);
                var        tempFile  = GetImageFileName(1);
                // 将二值化后的图像保存下
                Utils.ToBinaryImage(bmpOut).Save(tempFile);
                number_str = Discern(tempFile);
                File.Delete(tempFile);
            }
            return(number_str);
        }
Esempio n. 6
0
        protected override void deskewToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (imageList == null)
            {
                MessageBox.Show(this, Properties.Resources.LoadImage, strProgName);
                return;
            }
            this.imageCanvas.Deselect();
            this.Cursor = Cursors.Wait;

            gmseDeskew deskew         = new gmseDeskew((Bitmap)imageList[imageIndex]);
            double     imageSkewAngle = deskew.GetSkewAngle();

            if ((imageSkewAngle > MINIMUM_DESKEW_THRESHOLD || imageSkewAngle < -(MINIMUM_DESKEW_THRESHOLD)))
            {
                originalImage = imageList[imageIndex];
                stack.Push(originalImage);
                imageList[imageIndex] = ImageHelper.Rotate((Bitmap)originalImage, -imageSkewAngle);
                this.imageMain.Source = ImageConverter.BitmapToImageSource(imageList[imageIndex]);
            }
            this.Cursor = null;
        }
Esempio n. 7
0
        public static void EnhanceImageQuality(string ImageDirectory)
        {
            // do image Enhance then call Do OCR
            // convert - density 600 input.pdf output.tif;
            //System.Drawing.Image SelectedImage = new System.Drawing.Image();
            DirectoryInfo EnhancedImageDir = new DirectoryInfo(@"C:\OCR\EnhancedImage\");
            Bitmap        orgimg           = (Bitmap)Bitmap.FromFile(ImageDirectory);
            Bitmap        bitmap           = (Bitmap)Bitmap.FromFile(ImageDirectory);
            Bitmap        image2           = Grayscale.CommonAlgorithms.BT709.Apply(bitmap);


            Rectangle  rec        = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData bitmapdata = bitmap.LockBits(rec, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
            // create instance of skew checker
            DocumentSkewChecker skewChecker = new DocumentSkewChecker();
            // get documents skew angle
            double angle = skewChecker.GetSkewAngle(bitmapdata);
            // create rotation filter
            RotateBilinear rotationFilter = new RotateBilinear(-angle);

            rotationFilter.FillColor = Color.White;
            // rotate image applying the filter
            bitmap.UnlockBits(bitmapdata);

            Bitmap rotatedImage = rotationFilter.Apply(orgimg);

            rotatedImage.Save(ImageDirectory + ".tif");

            using (MagickImage image = new MagickImage(ImageDirectory))
            {
                TextCleanerScript Cleaner = new TextCleanerScript();

                // Cleaner.FilterSize = 12; // filter to clean up background;
                // Cleaner.FilterOffset = (Percentage)3; //filter in percent to reduce noise
                // Cleaner.Saturation = (Percentage)200;  // high value to Saturation
                //// Cleaner.Unrotate = true;
                // var newImage1 = Cleaner.Execute(image);
                // //newImage1.CannyEdge(); selcet the inner objects in the image

                // newImage1.Density = new PointD(600, 600);
                // newImage1.Write(EnhancedImageDir + Path.GetFileName(ImageDirectory)
                //     + ".tif");

                AutotrimScript WhiteB = new AutotrimScript();
                // WhiteB.InnerTrim = true;
                var a = WhiteB.GetLargestAreaP(image);

                WhiteboardScript AoutoTrim = new WhiteboardScript();
                //AoutoTrim.SetCoordinates(new Coordinate(01, 53), new Coordinate(313, 31),
                //new Coordinate(331, 218), new Coordinate(218, 200));
                //AoutoTrim.Enhance = WhiteboardEnhancements.Both;
                //AoutoTrim.FilterSize = 25; // filter to clean up background;
                //AoutoTrim.FilterOffset = (Percentage)3; //filter in percent to reduce noise
                //AoutoTrim.Saturation = (Percentage)200;  // high value to Saturation
                //AoutoTrim.SetCoordinates(new Coordinate(a[0].X, a[0].Y), new Coordinate(a[1].X, a[1].Y),
                //new Coordinate(a[2].X, a[2].Y), new Coordinate(a[3].X, a[3].Y));
                var new2mage2 = AoutoTrim.Execute(image);
                //  new2mage2.Deskew((Percentage)2);
                AoutoTrim.SetCoordinates(new Coordinate(13, 3), new Coordinate(342, 6),
                                         new Coordinate(331, 467), new Coordinate(38, 482));
                AoutoTrim.Enhance = WhiteboardEnhancements.Both;


                //  new2mage2.Density= new PointD(300, 300);
                new2mage2 = WhiteB.Execute(new2mage2);
                new2mage2 = Cleaner.Execute(new2mage2);

                new2mage2.Density = new PointD(600, 600);
                new2mage2.Write(EnhancedImageDir + Path.GetFileName(ImageDirectory)
                                + "2" + ".tif");
                string dir12 = (EnhancedImageDir + Path.GetFileName(ImageDirectory)
                                + "2" + ".tif");
                Bitmap     imageTobeDeske = new Bitmap(dir12);
                gmseDeskew deskew         = new gmseDeskew();
                deskew.New(imageTobeDeske);
                double angel = deskew.GetSkewAngle();
                if (angel > 0)
                {
                    angel = angel * -1;
                }



                WhiteB.BorderColorLocation = new Coordinate(10, 10);


                //AoutoTrim.SetCoordinates(ad);

                //WhiteB.ColorFuzz = (Percentage)20;
                //var new2mage3 = WhiteB.Execute(image);
                //new2mage3 = Cleaner.Execute(new2mage3);

                //new2mage3.Write(EnhancedImageDir + Path.GetFileName(ImageDirectory)
                //    + "3" + ".tif");
            }
        }