【OpenCV_12】旋转视频以及图片 Rotate Image & Video
来源:互联网 发布:网络主播靠什么赚钱 编辑:程序博客网 时间:2024/05/13 13:46
旋转图像 Rotate Image
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/imgproc.hpp"using namespace cv; int main( int argc, char** argv ) { // Load the image Mat imgOriginal = imread( "/Users/iDreamboat/Desktop/OpenCV/RG.JPG", 1 ); //show the original image const char* pzOriginalImage = "Original Image"; namedWindow( pzOriginalImage, CV_WINDOW_AUTOSIZE ); imshow( pzOriginalImage, imgOriginal ); const char* pzRotatedImage = "Rotated Image"; namedWindow( pzRotatedImage, CV_WINDOW_AUTOSIZE ); int iAngle = 180; createTrackbar("Angle", pzRotatedImage, &iAngle, 360); int iImageHieght = imgOriginal.rows / 2; int iImageWidth = imgOriginal.cols / 2; while (true) { Mat matRotation = getRotationMatrix2D( Point(iImageWidth, iImageHieght), (iAngle - 180), 1 ); // Rotate the image Mat imgRotated; warpAffine( imgOriginal, imgRotated, matRotation, imgOriginal.size() ); imshow( pzRotatedImage, imgRotated ); int iRet = waitKey(30); if ( iRet == 27 ) { break; } } return 0;}///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Original Image/Rotated Image
代码详解
- Mat getRotationMatrix2D( Point2f center, double angle, double scale )
这个函数返回2x3仿射变换矩阵来对应2D旋转
参数 -
- center - 源图像旋转中心的位置
- angle - 旋转的角度(正的为逆时针方向,负的为顺时针方向)
- scale - 图像比例因子(比例因子为1意味着原始大小)
可以尝试不同的旋转中心,角度及比例因子观察输出结果。
- void warpAffine( InputArray src, OutputArray dst, InputArray M, Size dsize, int flags = INTER_LINEAR, int bordreMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar() )
调用OpenCV函数对图像进行仿射变换。
参数 -
- src - 源图像
- dst - 目标图像应与源图像属性一致(变换之后的图像存储在这个位置)
- M - 2x3 仿射变换矩阵
- dsize - 目标图像的尺寸
- flags - 插值方法
- borderMode - 像素插值方法(尝试下面值BORDER_REPLICATE, BORDER_CONSTANT, BORDER_REFLECT, BORDER_WRAP, BORDER_REFLECT_101, BORDER_TRANSPARENT and BORDER_ISOLATED)
- borderValue - If you use BORDER_CONSTANT for borderMode, this argument define the value used for the border
旋转图像另一种方法 OpenCV
还有另外一种旋转图像的方法,这里,我用的回调函数采用旋转而不是采用while无限循环,除了旋转,还可以动态的采用插值方法缩放图像。
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/imgproc.hpp"using namespace cv;int iAngle = 180;int iScale = 50;int iBorderMode = 0;Mat imgOriginal ;int iImageCenterY = 0;int iImageCenterX = 0;const char* pzRotatedImage = "Rotated Image";void CallbackForTrackBar(int, void*){ Mat matRotation = getRotationMatrix2D( Point( iImageCenterX, iImageCenterY ), (iAngle - 180), iScale / 50.0 ); // Rotate the image Mat imgRotated; warpAffine( imgOriginal, imgRotated, matRotation, imgOriginal.size(), INTER_LINEAR, iBorderMode, Scalar() ); imshow( pzRotatedImage, imgRotated ); } int main( int argc, char** argv ) { // Load the image imgOriginal = imread( "/Users/iDreamboat/Desktop/OpenCV/RG.JPG", 1 ); iImageCenterY = imgOriginal.rows / 2; iImageCenterX = imgOriginal.cols / 2; //show the original image const char* pzOriginalImage = "Original Image"; namedWindow( pzOriginalImage, CV_WINDOW_AUTOSIZE ); imshow( pzOriginalImage, imgOriginal ); //create the "Rotated Image" window and 3 trackbars in it namedWindow( pzRotatedImage, CV_WINDOW_AUTOSIZE ); createTrackbar("Angle", pzRotatedImage, &iAngle, 360, CallbackForTrackBar); createTrackbar("Scale", pzRotatedImage, &iScale, 100, CallbackForTrackBar); createTrackbar("Border Mode", pzRotatedImage, &iBorderMode, 5, CallbackForTrackBar); int iDummy = 0; CallbackForTrackBar(iDummy, &iDummy); waitKey(0); return 0;}///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
旋转视频
旋转视频也很简单,
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/imgproc.hpp"#include <iostream>using namespace cv;using namespace std;int main(int argc, char* argv[]){ // open the video file for reading VideoCapture cap("/Users/iDreamboat/Desktop/OpenCV/MyVideo.mp4"); // if not success, exit program if ( !cap.isOpened() ) { cout << "Cannot open the video file" << endl; return -1; } const char* pzOriginalWindowName = "Original Video"; namedWindow(pzOriginalWindowName, CV_WINDOW_AUTOSIZE); const char* pzRotatingWindowName = "Rotated Video"; namedWindow( pzRotatingWindowName, CV_WINDOW_AUTOSIZE ); int iAngle = 180; createTrackbar("Angle", pzRotatingWindowName, &iAngle, 360); while (true) { Mat matOriginalFrame; // read a new frame from video bool bSuccess = cap.read(matOriginalFrame); //if not success, break loop if (!bSuccess) { cout << "Cannot read the frame from video file" << endl; break; } imshow(pzOriginalWindowName, matOriginalFrame); //get the affine transformation matrix Mat matRotation = getRotationMatrix2D( Point(matOriginalFrame.cols / 2, matOriginalFrame.rows / 2), (iAngle - 180), 1 ); // Rotate the image Mat matRotatedFrame; warpAffine( matOriginalFrame, matRotatedFrame, matRotation, matOriginalFrame.size() ); imshow( pzRotatingWindowName, matRotatedFrame ); //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop if (waitKey(30) == 27) { cout << "esc key is pressed by user" << endl; break; } } return 0;}///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0 0
- 【OpenCV_12】旋转视频以及图片 Rotate Image & Video
- Leetcode:Rotate Image 旋转图片
- Rotate Image------旋转图片(in-place版本)
- Rotate Image 旋转图像
- 【旋转矩阵】Rotate Image
- Rotate Image 旋转矩阵 @LeetCode
- LeetCode Rotate Image旋转图像
- [LeetCode] Rotate Image 旋转图像
- LeetCode Rotate Image旋转图像
- [LintCode] 旋转图像 Rotate Image
- jquery 图片旋转 rotate
- 图片的旋转rotate
- 图片旋转jquery.rotate
- rotate an image 图像旋转代码
- LeetCode | Rotate Image(旋转图像)
- Rotate Image 数组顺时针旋转90度
- Rotate Image 二维数组旋转90度
- LeetCode Rotate Image(矩阵的旋转)
- HBuilder配置SVN
- H.264 Quantization
- PHP中is_file,file_exists的区别,is_file 不能替代file_exits的理由
- 第十五周项目2洗牌
- Java中Properties类的使用
- 【OpenCV_12】旋转视频以及图片 Rotate Image & Video
- Lintcode:数飞机
- java.sql.SQLException: oracle.jdbc.driver.OracleDriver
- linux常用vi编辑器命令
- 移动端头部meta、link书写比较好的案例
- android 小知识点
- H.264 Transform
- android framework 源码结构图
- git 上的 python package 的安装