Skip to content

Instantly share code, notes, and snippets.

@nishanthbhat07
Created July 14, 2021 18:57
Show Gist options
  • Save nishanthbhat07/ee06c232c0f1d052b382315afaf37802 to your computer and use it in GitHub Desktop.
Save nishanthbhat07/ee06c232c0f1d052b382315afaf37802 to your computer and use it in GitHub Desktop.
Skew and tilt

Given an image containing a rotated block of text at an unknown angle, we need to correct the text skew by:

  1. Detecting the block of text in the image.
  2. Computing the angle of the rotated text.
  3. Rotating the image to correct for the skew.
Mat correctSkew()
{
    Mat warpMat = Imgproc.getAffineTransform( new MatOfPoint2f(srcTri), new MatOfPoint2f(dstTri) );
    Mat warpDst = Mat.zeros( src.rows(), src.cols(), src.type() );
    Imgproc.warpAffine( src, warpDst, warpMat, warpDst.size() );
    Point center = new Point(warpDst.cols() / 2, warpDst.rows() / 2);
    double angle = -50.0;
    double scale = 0.6;
    
    // Rotate matrix Opencv Function
    Mat rotMat = ##Imgproc.getRotationMatrix2D( center, angle, scale );
    
    Mat warpRotateDst = new Mat();
    Imgproc.warpAffine( warpDst, warpRotateDst, rotMat, warpDst.size() );
    
    return warpRotateDst;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment