Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How Convert Picture to scan form (consider perpective deskew,..), by opencv.
This is my assignment. I want to know, how and logic of that with code.(i totaly fresh student).

TQ

What I have tried:

NO idea
How Convert Picture to scan form (consider perpective deskew,..), by opencv.
This is my assignment. I want to know, how and logic of that with code.(i totaly fresh student).

TQ
Posted
Updated 14-Jun-19 0:53am

1 solution

C#
private byte[] AutoRotate(TessBaseAPI api, Bitmap im)
     {



         Image<Gray, byte> image = new Image<Gray, byte>(im);

         Bitmap image1 = image.ToBitmap();
         DocumentSkewChecker skewChecker = new DocumentSkewChecker();
         // get documents skew angle
         double angle = skewChecker.GetSkewAngle(image1);

         RotateBilinear rotationFilter = new RotateBilinear(-angle, false);
         rotationFilter.FillColor = Color.White;
         // rotate image applying the filter
         Bitmap rotatedImage = rotationFilter.Apply(image1);
         rotatedImage.Save("c:\\rotated0.png");


         RotateBilinear rotationFilter1 = new RotateBilinear(-90, false);
         Bitmap rotatedImage1 = rotationFilter1.Apply(rotatedImage);
         rotatedImage1.Save("c:\\rotated1.png");

         RotateBilinear rotationFilter2 = new RotateBilinear(-180, false);
         Bitmap rotatedImage2 = rotationFilter2.Apply(rotatedImage);
         rotatedImage2.Save("c:\\rotated2.png");

         RotateBilinear rotationFilter3 = new RotateBilinear(-270, false);
         Bitmap rotatedImage3 = rotationFilter3.Apply(rotatedImage);
         rotatedImage3.Save("c:\\rotated3.png");




         string first = "";
         string second = "";
         string third = "";
         string fourth = "";

         MemoryStream st = null;
         using (st = new MemoryStream())
         {
             rotatedImage.Save(st, ImageFormat.Png);
             api.SetImage(Pix1.pixReadFromMemoryStream(st));
             api.Recognize();

             first = api.GetUTF8Text();
         }


         st = null;
         using (st = new MemoryStream())
         {
             rotatedImage1.Save(st, ImageFormat.Png);
             api.SetImage(Pix1.pixReadFromMemoryStream(st));
             api.Recognize();

             second = api.GetUTF8Text();
         }

         st = null;
         using (st = new MemoryStream())
         {
             rotatedImage2.Save(st, ImageFormat.Png);
             api.SetImage(Pix1.pixReadFromMemoryStream(st));
             api.Recognize();

             third = api.GetUTF8Text();
         }

         st = null;
         using (st = new MemoryStream())
         {
             rotatedImage3.Save(st, ImageFormat.Png);
             api.SetImage(Pix1.pixReadFromMemoryStream(st));
             api.Recognize();

             fourth = api.GetUTF8Text();
         }

         string[] results = { first, second, third, fourth };
         string result = "";
         int son = 0;
         for (int i = 0; i < results.Length; i++)
         {
             if (results[i].Length > son)
             {
                 son = results[i].Length;
                 if (i == 0)
                 {
                     result = "first";
                 }
                 else if (i == 1)
                 {
                     result = "second";
                 }
                 else if (i == 2)
                 {
                     result = "third";
                 }
                 else
                 {
                     result = "fourth";
                 }

             }

         }
         Bitmap resultim;
         if (result == "first")
         {
             resultim = rotatedImage;
         }

         else if (result == "second")
         {
             resultim = rotatedImage1;
         }

         else if (result == "third")
         {
             resultim = rotatedImage2;
         }
         else
         {
             resultim = rotatedImage3;
         }

         resultim.Save("c:\\imresult.png");
         ImageConverter converter = new ImageConverter();
         return (byte[])converter.ConvertTo(resultim, typeof(byte[]));

     }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900