http://blog.csdn.net/chenyusiyuan/article/details/5628813
OpenCV2.1 的 ffmpeg 似乎編譯有問題,不能正常進行視頻讀寫方面的操作,因此嘗試調用 Matlab 來完成,不過也還沒成功,詳細記錄如下:
1、在 Matlab 中通過 mex –setup 和 mbuild –setup 指定 VS2008 SP1 作為 C 編譯器。
2、編寫保存視頻的 fun_saveVideo.m 文件如下:
- function fun_saveVideo(img, fps, flag)
- if flag == 0
- mov = avifile('disp.avi','compression','Indeo5','fps',fps,'quality',90);
- end
- if flag == 1
- mov = addframe(mov,img);
- end
- if flag == 2
- mov = close(mov);
- end
3、使用如下指令將 fun_saveVideo.m 編譯為 動態鏈接庫 供 VC 調用:
4、將生成的 libSaveVideo.h, libSaveVideo.lib, libSaveVideo.dll 三個文件復制到項目文件夾下,其中 lib 和 dll 文件復制到 debug 和 release 子文件夾內。
5、在 VS 界面 " /Tools / Options / Projects and Solutions / VC++ Directories" 中,在 " Include files " 和 " Library files " 中分別添加下列目錄(默認安裝位置):
6、在項目屬性" /Project / Properties / Configuration Properties / Linker / Input " 中添加 mclmcrrt.lib, libmx.lib, libmat.lib, mclmcr.lib 。
7、在項目的 Solution Explorer 的 Header Files 中添加 libSaveVideo.h ,在該文件的最下面可以看到函數的聲明:
- extern LIB_libSaveVideo_CPP_API void MW_CALL_CONV fun_saveVideo(const mwArray& img
- , const mwArray& fps
- , const mwArray& flag);
8、在 ******(項目名稱).h 中加入如下代碼(如果不加 『#pragma comment(lib,』」libSaveVideo」)』,則編譯鏈接時會出現錯誤「error LNK2019: unresolved external symbol」):
- #pragma once
- #pragma comment(lib,"libSaveVideo.lib") // new add
- #ifndef __AFXWIN_H__
- #error "include 'stdafx.h' before including this file for PCH"
- #endif
- #include "resource.h" // main symbols
- #include "cxcore.h"
- #include "cv.h"
- #include "highgui.h"
- #include "camerads.h"
- #include "mclmcr.h" // new add
- #include "matrix.h" // new add
- #include "mclcppclass.h" // new add
- #include "libSaveVideo.h" // new add
- ...
- ...
9、函數調用的具體代碼:
- // 初始化Matlab生成的saveVideo函數
- libSaveVideoInitialize();
- double fps =10, flag = 0;
- mwSize dims[3];
- dims[0] = img_size.height; dims[1] = img_size.width; dims[2] = 3;
- int len = img_size.height * img_size.width *3;
- mwArray mwfps(1, 1, mxDOUBLE_CLASS);
- mwArray mwflag(1, 1, mxDOUBLE_CLASS);
- mwArray mwdisp( 3, dims, mxUINT8_CLASS, mxREAL);
- mwfps.SetData(&fps, 1);
- mwflag.SetData(&flag, 1);
- ...
- while(...)
- {
- ...
- if (saveVideo)
- {
- IplImage* dispT = cvCreateImage(cvSize(img_size.height, img_size.width), CV_8UC3, 3);
- cvTranspose(tmp_img1, dispT);
- mwdisp.SetData((double*)tmp_img1->imageData, len);
- if (flag==0)
- {
- fun_saveVideo(mwdisp, mwfps, mwflag);
- flag = 1;
- }
- else
- {
- fun_saveVideo(mwdisp, mwfps, mwflag);
- }
- cvReleaseImage(&dispT);
- }
- ...
- }
- // 結束保存視頻
- flag = 2;
- fun_saveVideo(mwdisp, mwfps, mwflag);
- libSaveVideoTerminate();
- mclTerminateApplication();
10、編譯鏈接都通過,但實際運行時出錯,可能是 m 文件編寫不合理 或其它原因,有待進一步分析。