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

Text extraction and skewing adjustment

Now, all we must do is extract the text and adjust the text skew. This is done by the deskewAndCrop function, as follows:

Mat deskewAndCrop(Mat input, const RotatedRect& box) 
{ 
   double angle = box.angle;      
   auto size = box.size; 
 
   //Adjust the box angle 
   if (angle -45.0)  
   { 
        angle += 90.0;         std::swap(size.width, size.height);         
   } 
    
   //Rotate the text according to the angle 
   auto transform = getRotationMatrix2D(box.center, angle, 1.0); 
   Mat rotated; 
   warpAffine(input, rotated, transform, input.size(), INTER_CUBIC); 
 
   //Crop the result 
   Mat cropped; 
   getRectSubPix(rotated, size, box.center, cropped); 
   copyMakeBorder(cropped,cropped,10,10,10,10,BORDER_CONSTANT,Scalar(0)); 
   return cropped; 
}

First, we start by reading the desired ...

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