本章内容
1.攝像頭屬性擷取
2.攝像頭屬性更改
3.儲存攝像頭擷取的圖像為視訊
1. 攝像頭屬性擷取
輸出結果:
2. 攝像頭屬性更改
輸出結果:
3.儲存攝像頭擷取的圖像為視訊
輸出結果:
源碼:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
int main(int argc, char *argv[])
{
cv::VideoCapture vCap;
vCap.open(0); // 打開攝像頭 ,cv::CAP_V4L
if(!vCap.isOpened()){
std::cout << "攝像頭打開失敗" << std::endl;
return -1;
}
cv::Mat frame;
// 1. 擷取攝像頭的屬性
std::cout << "攝像頭寬度:" << vCap.get(cv::CAP_PROP_FRAME_WIDTH) << std::endl;
std::cout << "攝像頭高度:" << vCap.get(cv::CAP_PROP_FRAME_HEIGHT) << std::endl;
std::cout << "幀數目:" << vCap.get(cv::CAP_PROP_FPS) << std::endl;
std::cout << "亮度:" << vCap.get(cv::CAP_PROP_BITRATE) << std::endl;
std::cout << "對比度:" << vCap.get(cv::CAP_PROP_CONTRAST) << std::endl;
std::cout << "飽和度:" << vCap.get(cv::CAP_PROP_SATURATION) << std::endl;
std::cout << "色調:" << vCap.get(cv::CAP_PROP_HUE) << std::endl;
std::cout << "普光:" << vCap.get(cv::CAP_PROP_EXPOSURE) << std::endl;
// 2.設定攝像頭屬性
vCap.set(cv::CAP_PROP_FRAME_WIDTH, 320);
vCap.set(cv::CAP_PROP_FRAME_HEIGHT, 240);
vCap.set(cv::CAP_PROP_SATURATION, 50);
// 擷取攝像頭的屬性
std::cout << "屬性設定後" << std::endl;
std::cout << "攝像頭寬度:" << vCap.get(cv::CAP_PROP_FRAME_WIDTH) << std::endl;
std::cout << "攝像頭高度:" << vCap.get(cv::CAP_PROP_FRAME_HEIGHT) << std::endl;
std::cout << "幀數目:" << vCap.get(cv::CAP_PROP_FPS) << std::endl;
std::cout << "亮度:" << vCap.get(cv::CAP_PROP_BITRATE) << std::endl;
std::cout << "對比度:" << vCap.get(cv::CAP_PROP_CONTRAST) << std::endl;
std::cout << "飽和度:" << vCap.get(cv::CAP_PROP_SATURATION) << std::endl;
std::cout << "色調:" << vCap.get(cv::CAP_PROP_HUE) << std::endl;
std::cout << "普光:" << vCap.get(cv::CAP_PROP_EXPOSURE) << std::endl;
bool ret = vCap.read(frame);
cv::imshow("cap", frame);
//3.儲存攝像頭擷取的圖像為視訊
cv::VideoWriter vWriter;
cv::String vFile = "/home/wang/dev/Image/myCap.avi";
int codec = cv::VideoWriter::fourcc('D', 'I', 'V', 'X');
vWriter.open(vFile,codec,10,frame.size());
int key;
while(1){
key = cv::waitKey(100);
if(('q'==key)|('Q'==key)) break;
ret = vCap.read(frame);
if(!ret) continue;
// cv::flip(frame,frame,1);
vWriter.write(frame); //寫視訊幀
cv::imshow("cap", frame);
}
vCap.release(); // 釋放攝像頭,讓出資源
vWriter.release();
std::cout << "按任意按鍵退出程式" << std::endl;
cv::waitKey(0);
return 1;
}