上面的答案太复杂了,占用了你的 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 确实具有像人脸检测这样的原生功能(这并没有真正保持有问题的性能)。卓越。
干杯