O'Reilly logo

Learn OpenCV 4 by Building Projects - Second Edition by Prateek Joshi, Vinicius G. Mendonca, David Millan Escriva

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sending the output to a file

Let's change our main method to send the recognized output to a file. We do this by using a standard ofstream:

int main(int argc, char* argv[])  
{ 
   //Loads the ticket image and binarize it 
   Mat ticket = binarize(imread("ticket.png"));     
   auto regions = findTextAreas(ticket); 
 
   std::ofstream file;  
   file.open("ticket.txt", std::ios::out | std::ios::binary); 
 
   //For each region 
   for (const auto& region : regions) { 
         //Crop  
         auto cropped = deskewAndCrop(ticket, region); 
         auto text = identifyText(cropped, "por"); 
          
         file.write(text, strlen(text)); 
         file endl; 
   } 
    
   file.close(); 
} 

The following line opens the file in binary mode:

file.open("ticket.txt", std::ios::out | std::ios::binary); 

This is important since Tesseract returns text encoded ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required