title: OCR: Image to text?
body: <blockquote>
    <p><strong>Before mark as copy or repeat question, please read the whole question first.</strong></p>
  </blockquote>
  
  <p>I am able to do at pressent is as below:</p>
  
  <ol>
  <li>To get image and crop the desired part for OCR.</li>
  <li>Process the image using <code>tesseract</code> and <code>leptonica</code>.</li>
  <li>When the applied document is cropped in chunks ie 1 character per image it provides 96% of accuracy.</li>
  <li>If I don't do that and the document background is in white color and text is in black color it gives almost same accuracy.</li>
  </ol>
  
  <p>For example if the input is as this photo :</p>
  
  <p><strong>Photo start</strong></p>
  
  <p><img src="https://i.stack.imgur.com/pY8of.png" alt="enter image description here"></p>
  
  <p><strong>Photo end</strong></p>
  
  <p>What I want is to able to get the same accuracy for this photo<img src="https://i.stack.imgur.com/hZIpR.jpg" alt="enter image description here"><br>
  without generating blocks.</p>
  
  <p>The code I used to init tesseract and extract text from image is as below:</p>
  
  <p>For init of tesseract</p>
  
  <p><strong>in .h file</strong></p>
  
  <pre><code>tesseract::TessBaseAPI *tesseract;
  uint32_t *pixels;
  </code></pre>
  
  <p><strong>in .m file</strong></p>
  
  <pre><code>tesseract = new tesseract::TessBaseAPI();
  tesseract-&gt;Init([dataPath cStringUsingEncoding:NSUTF8StringEncoding], "eng");
  tesseract-&gt;SetPageSegMode(tesseract::PSM_SINGLE_LINE);
  tesseract-&gt;SetVariable("tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  tesseract-&gt;SetVariable("language_model_penalty_non_freq_dict_word", "1");
  tesseract-&gt;SetVariable("language_model_penalty_non_dict_word ", "1");
  tesseract-&gt;SetVariable("tessedit_flip_0O", "1");
  tesseract-&gt;SetVariable("tessedit_single_match", "0");
  tesseract-&gt;SetVariable("textord_noise_normratio", "5");
  tesseract-&gt;SetVariable("matcher_avg_noise_size", "22");
  tesseract-&gt;SetVariable("image_default_resolution", "450");
  tesseract-&gt;SetVariable("editor_image_text_color", "40");
  tesseract-&gt;SetVariable("textord_projection_scale", "0.25");
  tesseract-&gt;SetVariable("tessedit_minimal_rejection", "1");
  tesseract-&gt;SetVariable("tessedit_zero_kelvin_rejection", "1");
  </code></pre>
  
  <p>For get text from image</p>
  
  <pre><code>- (void)processOcrAt:(UIImage *)image
  {
      [self setTesseractImage:image];
  
      tesseract-&gt;Recognize(NULL);
      char* utf8Text = tesseract-&gt;GetUTF8Text();
      int conf = tesseract-&gt;MeanTextConf();
  
      NSArray *arr = [[NSArray alloc]initWithObjects:[NSString stringWithUTF8String:utf8Text],[NSString stringWithFormat:@"%d%@",conf,@"%"], nil];
  
      [self performSelectorOnMainThread:@selector(ocrProcessingFinished:)
                             withObject:arr
                          waitUntilDone:YES];
      free(utf8Text);
  }
  
  - (void)ocrProcessingFinished0:(NSArray *)result
  {
      UIAlertView *alt = [[UIAlertView alloc]initWithTitle:@"Data" message:[result objectAtIndex:0] delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
     [alt show];
  }
  </code></pre>
  
  <p>But I don't get proper output for the number plate image either it is null or it gives some garbage data for the image.</p>
  
  <p>And if I use the image which is the first one ie white background with text as black then the output is 89 to 95% accurate.</p>
  
  <p>Please help me out.</p>
  
  <p>Any suggestion will be appreciated.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Thanks to @jcesar for providing the link and also to @konstantin pribluda to provide valuable information and guide. </p>
  
  <p>I am able to convert images in to proper black and white form (almost). and so the recognition is better for all images :)</p>
  
  <blockquote>
    <p>Need help with proper binarization of images. Any Idea will be appreciated</p>
  </blockquote>