本例程功能捕获本地文件或者摄像头图像,获取对应图像信息以及图像缩放然后显示。
#include<iostream>
#include<opencv2/highgui.hpp>
#include<opencv2/imgcodecs.hpp>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
//#define USE_CAMERA //使用摄像头
#define USE_VIDEO
void main(int argc, char** argv)
{
#ifdef USE_VIDEO
Mat mCamVideo;
VideoCapture mCamCap("./1080P.h264");
int brightness = mCamCap.get(cv::CAP_PROP_BRIGHTNESS);
int contrast = mCamCap.get(cv::CAP_PROP_CONTRAST);
int saturation = mCamCap.get(cv::CAP_PROP_SATURATION);
int hue = mCamCap.get(cv::CAP_PROP_HUE);
int gain = mCamCap.get(cv::CAP_PROP_GAIN);
int exposure = mCamCap.get(cv::CAP_PROP_EXPOSURE);
int white_balance = mCamCap.get(cv::CAP_PROP_WHITE_BALANCE_BLUE_U);
int width = mCamCap.get(cv::CAP_PROP_FRAME_WIDTH);
int heigh = mCamCap.get(cv::CAP_PROP_FRAME_HEIGHT);
int fps = mCamCap.get(cv::CAP_PROP_FPS);
std::cout << "---------------------------------------------" << endl;
std::cout << "宽:" << width << endl;
std::cout << "高:" << heigh << endl;
std::cout << "fps:" << fps << endl;
std::cout << "---------------------------------------------" << endl;
namedWindow("文件");
resizeWindow("文件",1280,720);
moveWindow("文件",100,0);
while (mCamCap.isOpened())
{
double pos_msec = mCamCap.get(cv::CAP_PROP_POS_MSEC);
double pos_frame = mCamCap.get(cv::CAP_PROP_POS_FRAMES);
double pos_avi_ratio = mCamCap.get(cv::CAP_PROP_POS_AVI_RATIO);
cout << "---------------------------------------------" << endl;
cout << "视频文件中当前位置(ms):" << pos_msec << endl;
cout << "从0开始下一帧的索引:" << pos_frame << endl;
cout << "视频中的相对位置(范围为0.0到1.0):" << pos_avi_ratio << endl;
cout << "---------------------------------------------" << endl;
mCamCap >> mCamVideo;
cv::Size mSize;
mSize.width = 1280;
mSize.height = 720;
cv::resize(mCamVideo, mCamVideo, mSize);
if (mCamVideo.empty())
{
break;
}
else
{
imshow("文件", mCamVideo);
}
waitKey(1);
}
#else
Mat mCamVideo;
VideoCapture mCamCap(0);
int brightness = mCamCap.get(cv::CAP_PROP_BRIGHTNESS);
int contrast = mCamCap.get(cv::CAP_PROP_CONTRAST);
int saturation = mCamCap.get(cv::CAP_PROP_SATURATION);
int hue = mCamCap.get(cv::CAP_PROP_HUE);
int gain = mCamCap.get(cv::CAP_PROP_GAIN);
int exposure = mCamCap.get(cv::CAP_PROP_EXPOSURE);
int white_balance = mCamCap.get(cv::CAP_PROP_WHITE_BALANCE_BLUE_U);
int width = mCamCap.get(cv::CAP_PROP_FRAME_WIDTH);
int heigh = mCamCap.get(cv::CAP_PROP_FRAME_HEIGHT);
int fps= mCamCap.get(cv::CAP_PROP_FPS);
std::cout << "---------------------------------------------" << endl;
std::cout << "摄像头亮度:" << brightness << endl;
std::cout << "摄像头对比度:" << contrast << endl;
std::cout << "摄像头饱和度:" << saturation << endl;
std::cout << "摄像头色调:" << hue << endl;
std::cout << "摄像头增益:" << gain << endl;
std::cout << "摄像头曝光度:" << exposure << endl;
std::cout << "摄像头白平衡:" << white_balance << endl;
std::cout << "---------------------------------------------" << endl;
std::cout << "宽:" << width << endl;
std::cout << "高:" << heigh << endl;
std::cout << "fps:" << fps << endl;
std::cout << "---------------------------------------------" << endl;
namedWindow("摄像头");
while (mCamCap.isOpened())
{
mCamCap>> mCamVideo;
imshow("摄像头", mCamVideo);
waitKey(1);
}
#endif
mCamCap.release();
destroyAllWindows();
return ;
}