In the ch5 of "mastering openCV with practical computer vision projects", the author adjust the rotation of the plate as follow
void detectRegions::rotatedRect(cv::Mat const &input, cv::RotatedRect const &minRect)
{
// rotated rectangle drawing
cv::Point2f rect_points[4];
minRect.points(rect_points);
for( int j = 0; j != 4; ++j){
cv::line( result, rect_points[j], rect_points[(j+1)%4], cv::Scalar(0, 0, 255), 1, 8 );
}
//Get rotation matrix
float const aspect = static_cast<float>(minRect.size.width) / static_cast<float>(minRect.size.height);
float angle = minRect.angle;
if(aspect < 1){
angle += 90;
}
cv::Mat const rotmat = cv::getRotationMatrix2D(minRect.center, angle, 1);
//Create and rotate image
cv::warpAffine(input, imgRotated, rotmat, input.size(), CV_INTER_CUBIC);
}
The author use the height and width to determine the angle of the rotation matrix, but I don't know the reason behind of it.What I know is the consequence of how to make the car plate back to horizontal(-90)
- when width >= height(aspect >= 1), we need to apply clockwise rotation on the image
- when height < width(aspect < 1), we need to apply counter-clockwise rotation on the image