How can we change the resolution of a video in OpenCV using C++?

We used 'set()' class of OpenCV. Using 'set()' class, we can set the height and width of the frames. The following lines are setting the height and width of the video in our program.

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

The first line is setting the width of the frames into 320 pixel and the second line is setting the height of the frames to 240 pixels. These two lines together is forming a 320 x 240 resolution video stream. This is how we can simply change the resolution of video using OpenCV.

The following program changes the resolution of the video stream taken from default camera −


#include<opencv2/opencv.hpp>//OpenCV header to use VideoCapture class//
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;
   while (true){ //Taking an everlasting loop to show the video//
      cap >> myImage;
      if (myImage.empty()){ //Breaking the loop if no video frame is detected//
      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//
   cap.release();//Releasing the buffer memory//
   return 0;

This program will play the video at 320 x 240 resolution.