将Opencv矩阵旋转90、180、270度

Rotate Opencv Matrix by 90, 180, 270 degrees

本文关键字:270度 旋转 Opencv      更新时间:2023-10-16

我正在从网络摄像头拍摄图像,我需要将其旋转直角。我发现自己有以下功能:

  1. getRotationMatrix2D-创建旋转矩阵(不管它是什么(
  2. transform-通过旋转矩阵将一个矩阵变换为另一个矩阵

但是,除了黑色区域,我什么都看不到。这是我的代码:

   if(rotate_button.click%4>0) {
       double angle = (rotate_button.click%4)*90;  //button increments its click by 1 per click
       Mat transform_m = getRotationMatrix2D(Point(cam_frame_width/2, cam_frame_height/2), angle, 1);  //Creating rotation matrix
       Mat current_frame;
       transform(cam_frame, current_frame, transform_m);  //Transforming captured image into a new one
       cam_frame = Mat((int)current_frame.cols,(int)current_frame.rows, cam_frame_type) = Scalar(0,128,0);  //resizing captured matrix, so I can copy the resized one on it
       current_frame.copyTo(cam_frame);  //Copy resized to original
   }

输出只是黑屏。

以上答案过于复杂,占用了您的CPU。你的问题不是任意旋转,而是"将Opencv矩阵旋转90、180、270度"。

2017年6月30日更新:

OpenCV支持此功能,但未记录:https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core.hpp#L1041

void rotate(InputArray src, OutputArray dst, int rotateCode);

带有

enum RotateFlags {
    ROTATE_90_CLOCKWISE = 0, //Rotate 90 degrees clockwise
    ROTATE_180 = 1, //Rotate 180 degrees clockwise
    ROTATE_90_COUNTERCLOCKWISE = 2, //Rotate 270 degrees clockwise
};

原始答案&任意度旋转:

你也可以通过使用翻转和转置操作来做到这一点,即对于90CW:

transpose(matSRC, matROT);  
flip(matROT, matROT,1); //transpose+flip(1)=CW

等等。通过文档中的转置和翻转操作来介绍自己,自己找出其他命令(思考=学习(。

void rot90(cv::Mat &matImage, int rotflag){
  //1=CW, 2=CCW, 3=180
  if (rotflag == 1){
    transpose(matImage, matImage);  
    flip(matImage, matImage,1); //transpose+flip(1)=CW
  } else if (rotflag == 2) {
    transpose(matImage, matImage);  
    flip(matImage, matImage,0); //transpose+flip(0)=CCW     
  } else if (rotflag ==3){
    flip(matImage, matImage,-1);    //flip(-1)=180          
  } else if (rotflag != 0){ //if not 0,1,2,3:
    cout  << "Unknown rotation flag(" << rotflag << ")" << endl;
  }
}

所以你这样称呼它,注意矩阵是通过引用传递的。

cv::Mat matImage;
//Load in sensible data
rot90(matImage,3); //Rotate it
//Note if you want to keep an original unrotated version of 
// your matrix as well, just do this
cv::Mat matImage;
//Load in sensible data
cv::Mat matRotated = matImage.clone();
rot90(matImage,3); //Rotate it

任意旋转当我在做的时候,下面是如何旋转任意度数,我预计这会贵50倍。请注意,以这种方式旋转将包括黑色填充,并且边缘将被旋转以取代图像的原始大小。

void rotate(cv::Mat& src, double angle, cv::Mat& dst){
    cv::Point2f ptCp(src.cols*0.5, src.rows*0.5);
    cv::Mat M = cv::getRotationMatrix2D(ptCp, angle, 1.0);
    cv::warpAffine(src, dst, M, src.size(), cv::INTER_CUBIC); //Nearest is too rough, 
}

将其称为10.5度的旋转显然是:

cv::Mat matImage, matRotated;
//Load in data
rotate(matImage, 10.5, matRotated);

我发现值得注意的是,这些极其基本的功能并不是OpenCV的一部分,而OpenCV确实有人脸检测等原生功能(这并没有真正以可疑的性能维护(。非凡的

干杯

使用warpAffine。:

尝试:

Point2f src_center(source.cols/2.0F, source.rows/2.0F);
Mat rot_mat = getRotationMatrix2D(src_center, angle, 1.0);
Mat dst;
warpAffine(source, dst, rot_mat, source.size());

dst是最终图像

@Abhishek Thakur的答案只适用于将图像旋转180度。它不能处理90度的旋转,因为

  • 提供给getRotationMatrix2D的旋转中心不正确,并且
  • 传递给warpAffline的输出矩阵大小不正确

以下是将图像旋转90度的代码:

Mat src = imread("image.jpg");
Mat dst;
double angle = 90;  // or 270
Size src_sz = src.size();
Size dst_sz(src_sz.height, src_sz.width); 
int len = std::max(src.cols, src.rows); 
Point2f center(len/2., len/2.);
Mat rot_mat = cv::getRotationMatrix2D(center, angle, 1.0);
warpAffine(src, dst, rot_mat, dst_sz);

编辑:将图像旋转90180或270度的另一种方法包括执行矩阵transpose,然后执行flip。这种方法可能更快。

上面的代码工作得很好,但由于浮点和warpAffine插值中的矩阵计算,在图像中引入了数字误差。

对于90度增量旋转,我更喜欢使用以下(在python/opencv-python中(

由于Python中的OpenCV图像是2d Numpy数组。

90度
theImage=numpy.rot90(图像,1(
270度
图像=numpy.rot90(图像,3(

注意:我只在形状(X,Y(的灰度图像上测试了这个。如果有彩色(或其他多重分离(图像,则可能需要首先对其进行整形,以确保旋转沿正确的轴进行。