首頁  >  文章  >  後端開發  >  如何使用C++在OpenCV中旋轉影片?

如何使用C++在OpenCV中旋轉影片?

WBOY
WBOY轉載
2023-09-07 18:37:01465瀏覽

旋轉影片與旋轉影像類似。唯一的區別是我們不是將靜態圖片載入到圖像矩陣中,而是加載了視訊或從相機獲取視訊串流。

這裡,我們不是加載視頻,而是使用相機拍攝視頻。如果要使用視訊文件,只需正確輸入視訊檔案的地址即可。

以下程式示範如何使用C 在OpenCV中旋轉影片。

範例 H2>
#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main(int argc, char* argv[]) {
   VideoCapture loadvideo(0);//capture video from default camera//
   namedWindow("OriginalVideo");//declaring window to show original video stream//
   namedWindow("RotatedVideo");//declaring window to show rotated video stream//
   int rotating_angle = 180;//initial rotation angle//
   createTrackbar("Rotation", "RotatedVideo", &rotating_angle, 360);//creating trackbar for rotation//
   while (true) {
      Mat before_Rotating;//declaring matrix for image before rotation//
      bool temp = loadvideo.read(before_Rotating);//load frames from video source to matrix//
      imshow("OriginalVideo", before_Rotating);//show image frames before rotation//
      Mat for_Rotation = getRotationMatrix2D(Point(before_Rotating.cols / 2, before_Rotating.rows / 2), (rotating_angle - 180), 1);//affine transformation matrix for the 2D rotation//
      Mat after_Rotating;//declaring matrix for image after rotation//
      warpAffine(before_Rotating, after_Rotating, for_Rotation, before_Rotating.size());//applying affine transformation//
      imshow("RotatedVideo", after_Rotating);//show image after rotating//
      if (waitKey(30) == 27){ //wait till Esc key is pressed from keyboard//
         break;
      }
   }
   return 0;
}

輸出

如何使用C++在OpenCV中旋轉影片?

以上是如何使用C++在OpenCV中旋轉影片?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除