分辨率
修改分辨率
通过cap函数进行修改
#include <opencv2\opencv.hpp>
#include<ctime>
using namespace cv;
using namespace std;
int main( )
{
//【1】从摄像头读入视频
VideoCapture capture(0);
//【2】循环显示每一帧
int j=0;
capture.set(CV_CAP_PROP_FRAME_WIDTH, 1080); //设置宽度
capture.set(CV_CAP_PROP_FRAME_HEIGHT,720); //设置高度
while(1&&(j<100))
{ clock_t start,finish;
start=clock();
j++;
Mat frame; //定义一个Mat变量,用于存储每一帧的图像
capture>>frame; //读取当前帧
imshow("读取视频",frame); //显示当前帧
cout<<frame.size()<<endl;
imwrite("1.jpg",frame);
finish=clock();
cout << finish-start << "/" << CLOCKS_PER_SEC << " (s) "<< endl;
waitKey(1); //延时30ms
}
return 0;
}
对每一帧进行处理
但是上面的方法有的时候是修改不了的,尤其是网络摄像机,这时候就需要读取每一帧,然后对每一帧进行修改。
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
VideoCapture capture("F:\\leafseg\\video1.mp4");//获取视频,根据自己的视频位置更改
if (!capture.isOpened())
return -1;
Mat frame;
capture.set(CAP_PROP_FRAME_WIDTH, 640);
capture.set(CAP_PROP_FRAME_HEIGHT, 480);
Mat image;
//VideoWriter(const String & filename, int fourcc, double fps,Size frameSize, bool isColor = true);
VideoWriter videowriter("F:\\leafseg\\video1Change.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'),30,Size(640,480),true); //改变图像的帧速为30,图像帧的大小是(640,480)
while (capture.read(image))
{
resize(image, image, Size(640, 480), INTER_LINEAR);
//resize(image, image, Size(640, 360), 0, 0, INTER_CUBIC); //这两种方法
videowriter.write(image);
}
waitKey();
return 0;
}
这种方法视频的分辨率修改好了,但发现数据速率和比特率提高了,视频的大小也变大了,不太懂这个原因了。需要学习一些视频数据方面的内容,压缩和解压的问题。
亮度、饱和度等其他的参数
#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
char c = 0;
VideoCapture capture(0);
/*设置摄像头参数 不要随意修改
capture.set(CV_CAP_PROP_FRAME_WIDTH, 1080);//宽度
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 960);//高度
capture.set(CV_CAP_PROP_FPS, 30);//帧数
capture.set(CV_CAP_PROP_BRIGHTNESS, 1);//亮度 1
capture.set(CV_CAP_PROP_CONTRAST,40);//对比度 40
capture.set(CV_CAP_PROP_SATURATION, 50);//饱和度 50
capture.set(CV_CAP_PROP_HUE, 50);//色调 50
capture.set(CV_CAP_PROP_EXPOSURE, 50);//曝光 50
*/
//打印摄像头参数
printf("width = %.2f\n", capture.get(CV_CAP_PROP_FRAME_WIDTH));
printf("height = %.2f\n", capture.get(CV_CAP_PROP_FRAME_HEIGHT));
printf("fps = %.2f\n", capture.get(CV_CAP_PROP_FPS));
printf("brightness = %.2f\n", capture.get(CV_CAP_PROP_BRIGHTNESS));
printf("contrast = %.2f\n", capture.get(CV_CAP_PROP_CONTRAST));
printf("saturation = %.2f\n", capture.get(CV_CAP_PROP_SATURATION));
printf("hue = %.2f\n", capture.get(CV_CAP_PROP_HUE));
printf("exposure = %.2f\n", capture.get(CV_CAP_PROP_EXPOSURE));
while (1)
{
Mat frame;
capture >> frame;
namedWindow("video");
imshow("video", frame);
c = cvWaitKey(30);
if (c == 27)//Esc键退出
{
break;
}
}
return 0;
}
//原文链接:https://blog.csdn.net/csdnforyou/article/details/82257699
|