Advertisement
Sparrowvivek16

OCR (optical character Recognizer)

Nov 13th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. private string Ocr(Bitmap image_s, bool isFull, bool isNum = false)
  2.         {
  3.             string temp = "";
  4.             Image<Gray,byte> src = new Image<Gray,byte>(image_s);
  5.             double ratio = 1;
  6.             while (true)
  7.             {
  8.                 ratio = (double)CvInvoke.cvCountNonZero(src) / (src.Width * src.Height);
  9.                 if (ratio > 0.5) break;
  10.                 src = src.Dilate(2);
  11.             }
  12.             Bitmap image = src.ToBitmap();
  13.  
  14.             TesseractProcessor ocr;
  15.             if (isFull)
  16.                 ocr = full_tesseract;
  17.             else if (isNum)
  18.                 ocr = num_tesseract;
  19.             else
  20.                 ocr = ch_tesseract;
  21.  
  22.             int cou = 0;
  23.             ocr.Clear();
  24.             ocr.ClearAdaptiveClassifier();
  25.             temp = ocr.Apply(image);
  26.             while (temp.Length > 3)
  27.             {
  28.                 Image<Gray, byte> temp2 = new Image<Gray, byte>(image);
  29.                 temp2 = temp2.Erode(2);
  30.                 image = temp2.ToBitmap();
  31.                 ocr.Clear();
  32.                 ocr.ClearAdaptiveClassifier();
  33.                 temp = ocr.Apply(image);
  34.                 cou++;
  35.                 if (cou > 10)
  36.                 {
  37.                     temp = "";
  38.                     break;
  39.                 }
  40.             }
  41.             return temp;
  42.            
  43.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement