Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

วิธีเปลี่ยนความละเอียดวิดีโอใน OpenCV ด้วย C++


ภาพรวม

สาธิตการ ตั้งค่าความละเอียด (Resolution) ของวิดีโอจากกล้อง โดยใช้ Method set() ของ VideoCapture

โค้ดตัวอย่าง C++

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;

int main() {
    Mat myImage;
    namedWindow("Video Player");
    VideoCapture cap(0);
    
    // ตั้งค่าความละเอียด 320x240
    cap.set(CAP_PROP_FRAME_WIDTH, 320);
    cap.set(CAP_PROP_FRAME_HEIGHT, 240);
    
    if (!cap.isOpened()) {
        cout << "No video stream detected" << endl;
        system("pause");
        return -1;
    }
    
    while (true) {
        cap >> myImage;
        if (myImage.empty()) break;
        imshow("Video Player", myImage);
        char c = (char)waitKey(25);
        if (c == 27) break;
    }
    
    cap.release();
    return 0;
}

คำอธิบาย

  • cap.set(CAP_PROP_FRAME_WIDTH, 320): ตั้งความกว้าง 320 pixels
  • cap.set(CAP_PROP_FRAME_HEIGHT, 240): ตั้งความสูง 240 pixels
  • รองรับได้ขึ้นอยู่กับ Hardware ของกล้อง (ไม่ทุกขนาดทำได้)
  • ค่าที่แนะนำ: 640x480, 320x240, 1280x720, 1920x1080
  • ใช้ cap.get(CAP_PROP_FRAME_WIDTH) ตรวจสอบค่าจริงที่ตั้งได้

ผลลัพธ์

เล่นวิดีโอจากกล้องที่ความละเอียด 320x240 pixels