如何使用 C++ 在 OpenCV 中更改影片的解析度?


我們使用了 OpenCV 的“set()”類。使用“set()”類,我們可以設定幀的高度和寬度。以下行正在我們的程式中設定影片的高度和寬度。

  • set(CAP_PROP_FRAME_WIDTH, 320);
  • set(CAP_PROP_FRAME_HEIGHT, 240);

第一行將幀的寬度設定為 320 個畫素,第二行將幀的高度設定為 240 個畫素。這兩行共同形成一個 320 x 240 解析度的影片流。這就是我們如何使用 OpenCV 簡單更改影片解析度的方法。

以下程式更改了從預設攝像頭獲取的影片流的解析度 −

示例

#include<opencv2/opencv.hpp>//OpenCV header to use VideoCapture class//
#include<iostream>
using namespace std;
using namespace cv;
int main() {
   Mat myImage;//Declaring a matrix to load the frames//
   namedWindow("Video Player");//Declaring the video to show the video//
   VideoCapture cap(0);//Declaring an object to capture stream of frames from default camera//
   cap.set(CAP_PROP_FRAME_WIDTH, 320);//Setting the width of the video
   cap.set(CAP_PROP_FRAME_HEIGHT, 240);//Setting the height of the video//
   if (!cap.isOpened()){ //This section prompt an error message if no video stream is found//
      cout << "No video stream detected" << endl;
      system("pause");
      return-1;
   }
   while (true){ //Taking an everlasting loop to show the video//
      cap >> myImage;
      if (myImage.empty()){ //Breaking the loop if no video frame is detected//
         break;
      }
      imshow("Video Player", myImage);//Showing the video//
      char c = (char)waitKey(25);//Allowing 25 milliseconds frame processing time and initiating break condition//
      if (c == 27){ //If 'Esc' is entered break the loop//
         break;
      }
   }
   cap.release();//Releasing the buffer memory//
   return 0;
}

此程式將在 320 x 240 解析度下播放影片。

更新於: 2021-03-10

3000+ 瀏覽次數

開啟你的職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.