OpenCV學習筆記(12)OpenCV調用Matlab函數進行保存視頻的嘗試

来源:互联网 发布:那个约会软件免费 编辑:程序博客网 时间:2024/06/05 18:27

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 文件如下:

[cpp] view plaincopy
  1. function fun_saveVideo(img, fps, flag)  
  2. if flag == 0  
  3.     mov = avifile('disp.avi','compression','Indeo5','fps',fps,'quality',90);  
  4. end  
  5. if flag == 1  
  6.     mov = addframe(mov,img);  
  7. end   
  8. if flag == 2  
  9.     mov = close(mov);  
  10. end  

3、使用如下指令將 fun_saveVideo.m 編譯為 動態鏈接庫 供 VC 調用:

[cpp] view plaincopy
  1. mcc -W cpplib:libSaveVideo -T link:lib fun_saveVideo.m  

4、將生成的 libSaveVideo.h, libSaveVideo.lib, libSaveVideo.dll 三個文件復制到項目文件夾下,其中 lib 和 dll 文件復制到 debug 和 release 子文件夾內。

5、在 VS 界面 " /Tools / Options / Projects and Solutions / VC++ Directories" 中,在 " Include files " 和 " Library files " 中分別添加下列目錄(默認安裝位置):

[c-sharp] view plaincopy
  1. " D:/MATLAB_R2009a/extern/include "  
  2.   
  3. " D:/MATLAB_R2009a/extern/lib/win32/microsoft "  

 

6、在項目屬性" /Project / Properties / Configuration Properties / Linker / Input " 中添加 mclmcrrt.lib, libmx.lib, libmat.lib, mclmcr.lib 。

7、在項目的 Solution Explorer 的 Header Files 中添加 libSaveVideo.h ,在該文件的最下面可以看到函數的聲明:

[cpp] view plaincopy
  1. extern LIB_libSaveVideo_CPP_API void MW_CALL_CONV fun_saveVideo(const mwArray& img  
  2.                                                                 , const mwArray& fps  
  3.                                                                 , const mwArray& flag);  

8、在 ******(項目名稱).h 中加入如下代碼(如果不加 『#pragma comment(lib,』」libSaveVideo」)』,則編譯鏈接時會出現錯誤「error LNK2019: unresolved external symbol」):

[cpp] view plaincopy
  1. #pragma once  
  2. #pragma comment(lib,"libSaveVideo.lib")  // new add  
  3.   
  4. #ifndef __AFXWIN_H__  
  5.     #error "include 'stdafx.h' before including this file for PCH"  
  6. #endif  
  7.   
  8.   
  9. #include "resource.h"        // main symbols  
  10. #include "cxcore.h"  
  11. #include "cv.h"  
  12. #include "highgui.h"  
  13. #include "camerads.h"  
  14.   
  15. #include "mclmcr.h"        // new add  
  16. #include "matrix.h"        // new add  
  17. #include "mclcppclass.h"   // new add  
  18. #include "libSaveVideo.h"  // new add  
  19.   
  20. ...  
  21. ...  

 

9、函數調用的具體代碼:

[cpp] view plaincopy
  1. // 初始化Matlab生成的saveVideo函數  
  2.  libSaveVideoInitialize();  
  3.  double fps =10, flag = 0;  
  4.  mwSize dims[3];  
  5.  dims[0] = img_size.height; dims[1] = img_size.width; dims[2] = 3;  
  6.  int len = img_size.height * img_size.width *3;  
  7.  mwArray mwfps(1, 1, mxDOUBLE_CLASS);  
  8.  mwArray mwflag(1, 1, mxDOUBLE_CLASS);  
  9.  mwArray mwdisp( 3, dims, mxUINT8_CLASS, mxREAL);  
  10.  mwfps.SetData(&fps, 1);  
  11.  mwflag.SetData(&flag, 1);  
  12.  ...  
  13.  while(...)  
  14.  {  
  15.      ...  
  16.      if (saveVideo)  
  17.      {  
  18.          IplImage* dispT = cvCreateImage(cvSize(img_size.height, img_size.width), CV_8UC3, 3);  
  19.          cvTranspose(tmp_img1, dispT);  
  20.          mwdisp.SetData((double*)tmp_img1->imageData, len);  
  21.          if (flag==0)  
  22.          {  
  23.              fun_saveVideo(mwdisp, mwfps, mwflag);  
  24.              flag = 1;  
  25.          }  
  26.          else  
  27.          {  
  28.              fun_saveVideo(mwdisp, mwfps, mwflag);  
  29.          }  
  30.          cvReleaseImage(&dispT);  
  31.      }  
  32.      ...  
  33.  }  
  34.  // 結束保存視頻  
  35.  flag = 2;  
  36.  fun_saveVideo(mwdisp, mwfps, mwflag);  
  37.  libSaveVideoTerminate();  
  38.  mclTerminateApplication();  
10、編譯鏈接都通過,但實際運行時出錯,可能是 m 文件編寫不合理 或其它原因,有待進一步分析。

原创粉丝点击