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);
Here is my Python translation (and thanks to all the posters):
import cv2
def rot90(img, rotflag):
""" rotFlag 1=CW, 2=CCW, 3=180"""
if rotflag == 1:
img = cv2.transpose(img)
img = cv2.flip(img, 1) # transpose+flip(1)=CW
elif rotflag == 2:
img = cv2.transpose(img)
img = cv2.flip(img, 0) # transpose+flip(0)=CCW
elif rotflag ==3:
img = cv2.flip(img, -1) # transpose+flip(-1)=180
elif rotflag != 0: # if not 0,1,2,3
raise Exception("Unknown rotation flag({})".format(rotflag))
return img
This is the first result when you Google it and none of these solutions really answer the question or is correct or succinct.
Core.rotate(Mat src, Mat dst, Core.ROTATE_90_CLOCKWISE); //ROTATE_180 or ROTATE_90_COUNTERCLOCKWISE
Here is a solution using the Android API. Here, I am using it to rotate images from a camera which could be mounted in various orientations.
if (mCameraOrientation == 270) {
// Rotate clockwise 270 degrees
Core.flip(src.t(), dst, 0);
} else if (mCameraOrientation == 180) {
// Rotate clockwise 180 degrees
Core.flip(src, dst, -1);
} else if (mCameraOrientation == 90) {
// Rotate clockwise 90 degrees
Core.flip(src.t(), dst, 1);
} else if (mCameraOrientation == 0) {
// No rotation
dst = src;
}
I don't know the java api very well, this codes are developed by c++. The logics should be the same, use transpose + flip to rotate the image with 90n(n belongs to N = -minimum value of int, ....., -3, -2, -1, 0, 1, 2, 3, ..., max value of int)
/*
*@brief rotate image by multiple of 90 degrees
*
*@param source : input image
*@param dst : output image
*@param angle : factor of 90, even it is not factor of 90, the angle
* will be mapped to the range of [-360, 360].
* {angle = 90n; n = {-4, -3, -2, -1, 0, 1, 2, 3, 4} }
* if angle bigger than 360 or smaller than -360, the angle will
* be map to -360 ~ 360.
* mapping rule is : angle = ((angle / 90) % 4) * 90;
*
* ex : 89 will map to 0, 98 to 90, 179 to 90, 270 to 3, 360 to 0.
*
*/
void rotate_image_90n(cv::Mat &src, cv::Mat &dst, int angle)
{
if(src.data != dst.data){
src.copyTo(dst);
}
angle = ((angle / 90) % 4) * 90;
//0 : flip vertical; 1 flip horizontal
bool const flip_horizontal_or_vertical = angle > 0 ? 1 : 0;
int const number = std::abs(angle / 90);
for(int i = 0; i != number; ++i){
cv::transpose(dst, dst);
cv::flip(dst, dst, flip_horizontal_or_vertical);
}
}
Edit : Improve performance, thanks for the comments of TimZaman and the implementation of 1''
void rotate_90n(cv::Mat const &src, cv::Mat &dst, int angle)
{
CV_Assert(angle % 90 == 0 && angle <= 360 && angle >= -360);
if(angle == 270 || angle == -90){
// Rotate clockwise 270 degrees
cv::transpose(src, dst);
cv::flip(dst, dst, 0);
}else if(angle == 180 || angle == -180){
// Rotate clockwise 180 degrees
cv::flip(src, dst, -1);
}else if(angle == 90 || angle == -270){
// Rotate clockwise 90 degrees
cv::transpose(src, dst);
cv::flip(dst, dst, 1);
}else if(angle == 360 || angle == 0 || angle == -360){
if(src.data != dst.data){
src.copyTo(dst);
}
}
}
Noone notices this simple method. use cv2.rotate
to rotate the image 90 degrees in clockwise
image = cv2.rotate(src, cv2.cv2.ROTATE_90_CLOCKWISE)
Other flags
ROTATE_90_CLOCKWISE Python: cv.ROTATE_90_CLOCKWISE
ROTATE_180 Python: cv.ROTATE_180
ROTATE_90_COUNTERCLOCKWISE Python: cv.ROTATE_90_COUNTERCLOCKWISE
cv2 official link https://docs.opencv.org/3.4/d2/de8/group__core__array.html#ga4ad01c0978b0ce64baa246811deeac24
Use the numpy.rot90
,if you want 180 degrees,just do it twice.
import numpy as np
import cv2
img = cv2.imread('img.png',1)
cv2.imshow('',img)
cv2.waitKey(0)
img90 = np.rot90(img)
cv2.imshow('',img90)
cv2.waitKey(0)