I need to rotate an image by either 90, 180 or 270 degrees. In OpenCV4Android I can use:
Imgproc.getRotationMatrix2D(new Point(center, center), degrees, 1);
This will rotate an image any number of degrees, using the most efficient means for multiples of 90.
void
rotate_cw(const cv::Mat& image, cv::Mat& dest, int degrees)
{
switch (degrees % 360) {
case 0:
dest = image.clone();
break;
case 90:
cv::flip(image.t(), dest, 1);
break;
case 180:
cv::flip(image, dest, -1);
break;
case 270:
cv::flip(image.t(), dest, 0);
break;
default:
cv::Mat r = cv::getRotationMatrix2D({image.cols/2.0F, image.rows/2.0F}, degrees, 1.0);
int len = std::max(image.cols, image.rows);
cv::warpAffine(image, dest, r, cv::Size(len, len));
break; //image size will change
}
}
But with opencv 3.0, this is done by just via the cv::rotate command:
cv::rotate(image, dest, e.g. cv::ROTATE_90_COUNTERCLOCKWISE);