I'm not sure I understood correctly everything that you are trying to do, but carry me.
Some cameras have lenses that cause slight image distortion, and for this purpose, OpenCV offers methods to assist in the process of calibrating the camera .
In fact, if you want to write an application that automatically corrects image distortion, first you need to find out what magic values you need to fix it. These values ββcome from the correct calibration procedure.
The chessboard image is used with the calibrateCamera() application: they are cameraMatrix and distCoeffs . Print them and write the data on a piece of paper.
In the end, your system should have a function / method to distort the image, where these 2 variables will be hardcoded inside the function, and then call cv::undistort() (if you use the OpenCV C ++ API):
cv::Mat undistorted; cv::undistort(image, undistorted, cameraMatrix, distCoeffs);
and what is he.
Detecting rotation automatically can be a little complicated, but the first thing to do is find the coordinates of the object you are interested in. But if the camera is in a fixed position, it will be easy.
For more information about the prospects for change and rotation using OpenCV, I suggest taking a look at these other questions:
Running cv :: warpPerspective for fake translation on cv :: Point set
Affine transformation, simple rotation and scaling, or something else?
Rotate cv :: Mat using cv :: warpAffine shifts target image