เราใช้คลาส 'set()' ของ OpenCV การใช้คลาส '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