opencv学习-highgui-video视频操作
来源:互联网 发布:utorrent mac 中文 编辑:程序博客网 时间:2024/06/06 01:48
VideoCapture类提供了各项视频操作,目前opencv只支持avi格式,而且无声音,编解码一般为4字符,即XVID, DIVX or H264
获取编码code类型
VideoCapture inputVideo(source); // Open inputint ex = static_cast<int>(inputVideo.get(CV_CAP_PROP_FOURCC)); // Get Codec Type- Int form然后将整数转换成字符convert from the integer form to string
char EXT[] = {ex & 0XFF , (ex & 0XFF00) >> 8,(ex & 0XFF0000) >> 16,(ex & 0XFF000000) >> 24, 0};
//You can do the same thing with the union as:union { int v; char c[5];} uEx ;uEx.v = ex; // From Int to char via unionuEx.c[4]=’\0’;
打开摄像头或读取本地视频文件
VideoCapture cam(0);//打开摄像头VideoCapture cam("a.avi");//打开本地文件
判断是否成功加载视频
if ( !cam.isOpened()){cout << "Could not open reference " << sourceReference << endl;return -1;}
获取每一帧
cam>>frame;cam.read(frame);注意:使用read方法时,即使没有帧传过来也会读取;这时需用frame.empty判断;而>>不需要判断。
获取视频size
Size refS = Size((int) cam.get(CV_CAP_PROP_FRAME_WIDTH),(int) cam.get(CV_CAP_PROP_FRAME_HEIGHT)),
控制视频播放
captRefrnc.set(CV_CAP_PROP_POS_MSEC, 1.2); // go to the 1.2 second in the videocaptRefrnc.set(CV_CAP_PROP_POS_FRAMES, 10); // go to the 10th frame of the video
写视频
outputVideo.write(res); //oroutputVideo << res;
从RGB中提取一个颜色通道,提取通道为channel
split(src, spl); // process - extract only the correct channelfor( int i =0; i < 3; ++i)if (i != channel)spl[i] = Mat::zeros(S, spl[0].type());merge(spl, res);
例子代码如下:
#include <iostream> // for standard I/O#include <string> // for strings#include <iomanip> // for controlling float print precision#include <sstream> // string to number conversion#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur#include <opencv2/highgui/highgui.hpp> // OpenCV window I/Ousing namespace std;using namespace cv;double getPSNR ( const Mat& I1, const Mat& I2);//获取峰值信噪比Scalar getMSSIM( const Mat& I1, const Mat& I2);//平均结构相似度static void help(){cout<< "------------------------------------------------------------------------------" << endl<< "This program shows how to read a video file with OpenCV. In addition, it "<< "tests the similarity of two input videos first with PSNR, and for the frames "<< "below a PSNR trigger value, also with MSSIM." << endl<< "Usage:" << endl<< "./video-source referenceVideo useCaseTestVideo PSNR_Trigger_Value Wait_Between_Frames " << endl<< "--------------------------------------------------------------------------" << endl<< endl;}int main(int argc, char *argv[]){help();int psnrTriggerValue, delay;psnrTriggerValue=10;delay=100;char c;int frameNum = -1; // Frame counterVideoCapture captRefrnc("2.avi"), captUndTst("3.avi");if (!captRefrnc.isOpened()){cout << "Could not open reference " << endl;return -1;}if (!captUndTst.isOpened()){cout << "Could not open case test " << endl;return -1;}Size refS = Size((int) captRefrnc.get(CV_CAP_PROP_FRAME_WIDTH),(int) captRefrnc.get(CV_CAP_PROP_FRAME_HEIGHT)),uTSi = Size((int) captUndTst.get(CV_CAP_PROP_FRAME_WIDTH),(int) captUndTst.get(CV_CAP_PROP_FRAME_HEIGHT));if (refS != uTSi){cout << "Inputs have different size!!! Closing." << endl;return -1;}const char* WIN_UT = "Under Test";const char* WIN_RF = "Reference";// WindowsnamedWindow(WIN_RF, CV_WINDOW_AUTOSIZE);namedWindow(WIN_UT, CV_WINDOW_AUTOSIZE);cvMoveWindow(WIN_RF, 400 , 0); //750, 2 (bernat =0)cvMoveWindow(WIN_UT, refS.width, 0); //1500, 2cout << "Reference frame resolution: Width=" << refS.width << " Height=" << refS.height<< " of nr#: " << captRefrnc.get(CV_CAP_PROP_FRAME_COUNT) << endl;cout << "PSNR trigger value " << setiosflags(ios::fixed) << setprecision(3)<< psnrTriggerValue << endl;Mat frameReference, frameUnderTest;double psnrV;Scalar mssimV;for(;;) //Show the image captured in the window and repeat{captRefrnc >> frameReference;captUndTst >> frameUnderTest;if (frameReference.empty() || frameUnderTest.empty()){cout << " < < < Game over! > > > ";break;}++frameNum;cout << "Frame: " << frameNum << "# ";///////////////////////////////// PSNR ////////////////////////////////////////////////////psnrV = getPSNR(frameReference,frameUnderTest);cout << setiosflags(ios::fixed) << setprecision(3) << psnrV << "dB";//////////////////////////////////// MSSIM /////////////////////////////////////////////////if (psnrV < psnrTriggerValue && psnrV){mssimV = getMSSIM(frameReference, frameUnderTest);cout << " MSSIM: "<< " R " << setiosflags(ios::fixed) << setprecision(2) << mssimV.val[2] * 100 << "%"<< " G " << setiosflags(ios::fixed) << setprecision(2) << mssimV.val[1] * 100 << "%"<< " B " << setiosflags(ios::fixed) << setprecision(2) << mssimV.val[0] * 100 << "%";}cout << endl;////////////////////////////////// Show Image /////////////////////////////////////////////imshow(WIN_RF, frameReference);imshow(WIN_UT, frameUnderTest);c = (char)cvWaitKey(delay);if (c == 27) break;}return 0;}double getPSNR(const Mat& I1, const Mat& I2){Mat s1;absdiff(I1, I2, s1); // |I1 - I2|s1.convertTo(s1, CV_32F); // cannot make a square on 8 bitss1 = s1.mul(s1); // |I1 - I2|^2Scalar s = sum(s1); // sum elements per channeldouble sse = s.val[0] + s.val[1] + s.val[2]; // sum channelsif( sse <= 1e-10) // for small values return zeroreturn 0;else{double mse = sse / (double)(I1.channels() * I1.total());double psnr = 10.0 * log10((255 * 255) / mse);return psnr;}}Scalar getMSSIM( const Mat& i1, const Mat& i2){const double C1 = 6.5025, C2 = 58.5225;/***************************** INITS **********************************/int d = CV_32F;Mat I1, I2;i1.convertTo(I1, d); // cannot calculate on one byte large valuesi2.convertTo(I2, d);Mat I2_2 = I2.mul(I2); // I2^2Mat I1_2 = I1.mul(I1); // I1^2Mat I1_I2 = I1.mul(I2); // I1 * I2/*************************** END INITS **********************************/Mat mu1, mu2; // PRELIMINARY COMPUTINGGaussianBlur(I1, mu1, Size(11, 11), 1.5);GaussianBlur(I2, mu2, Size(11, 11), 1.5);Mat mu1_2 = mu1.mul(mu1);Mat mu2_2 = mu2.mul(mu2);Mat mu1_mu2 = mu1.mul(mu2);Mat sigma1_2, sigma2_2, sigma12;GaussianBlur(I1_2, sigma1_2, Size(11, 11), 1.5);sigma1_2 -= mu1_2;GaussianBlur(I2_2, sigma2_2, Size(11, 11), 1.5);sigma2_2 -= mu2_2;GaussianBlur(I1_I2, sigma12, Size(11, 11), 1.5);sigma12 -= mu1_mu2;///////////////////////////////// FORMULA ////////////////////////////////Mat t1, t2, t3;t1 = 2 * mu1_mu2 + C1;t2 = 2 * sigma12 + C2;t3 = t1.mul(t2); // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))t1 = mu1_2 + mu2_2 + C1;t2 = sigma1_2 + sigma2_2 + C2;t1 = t1.mul(t2); // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))Mat ssim_map;divide(t3, t1, ssim_map); // ssim_map = t3./t1;Scalar mssim = mean(ssim_map); // mssim = average of ssim mapreturn mssim;}
- opencv学习-highgui-video视频操作
- OpenCV学习笔记——VideoWriter生成视频流highgui
- OpenCV学习HighGUI
- 【学习OpenCV】——highgui
- opencv学习笔记2:HighGUI
- OpenCV--HighGUI视频读写和显示
- 【opencv学习之十一】opencv: HighGUI参考手册
- OpenCV学习笔记(四十七)——VideoWriter生成视频流highgui
- OpenCV学习笔记(四十七)——VideoWriter生成视频流highgui
- OpenCV学习笔记(四十七)——VideoWriter生成视频流highgui
- OpenCV学习笔记08--细说HighGUI
- OpenCV学习笔记---HighGUI的初步
- OpenCV学习——Highgui演示-简单绘制矩形
- opencv学习—HighGUI部分函数介绍及例子
- 第四章 学习OpenCV——细说HighGUI
- OpenCV 之 HighGUI 参考手册
- highgui基础 OpenCV trackbar
- opencv 3.0 绘图 highgui
- asp.net css 1 2013-11-20
- web2
- 沁路墨彦文集:《清风夕来》
- MongoDB 聚合管道(二)(Aggregation Pipeline)
- IOS atomic与nonatomic,assign,copy与retain的定义和区别
- opencv学习-highgui-video视频操作
- 63. 掌握(并重构)构建
- java集合系列
- Java获取汉子区位码
- oracle wm_concat
- EBS DBA 问题问答
- Android系统Camera录像过程分析
- iphone游戏开发中使用自定义字体的方法
- Android dispatchTouchEvent介绍