視頻處理是對於前景的提取,混合高斯算法的調用是在cv空間域名中但是在opencv2.4.7中提示直接調用發生錯誤:
提示cv中沒有BackgroundSubtractorMOG這個成員;
我在網上查了一下分析說:2.4.7 使用時無法檢測到主頁中的例子。在只有我又將頭文件加上,發現只要加上:
#include <cvaux.h>就可以調用BackgroundSubtractorMOG這個成員;
附上代碼:
1 #include<cv.h> 2 #include<highgui.h> 3 #include <cvaux.h> 4 5 int main() 6 { 7 //打開視頻文件 8 cv::VideoCapture capture("D:\\1\\1.mp4"); 9 //檢查視頻是否成功打開 10 if(!capture.isOpened()) 11 return 0; 12 //當前視頻幀 13 cv::Mat frame; 14 //前景圖像 15 cv::Mat foreground; 16 cv::namedWindow("Extracted f"); 17 //使用默認參數的Mixture of Gaussian對象 18 cv::BackgroundSubtractorMOG mog; 19 bool stop(false); 20 //遍歷每一幀 21 while(!stop){ 22 //讀取下一幀 23 if(!capture.read(frame)) 24 break; 25 //更新背景並返回前景 26 mog(frame,foreground,0.01); 27 //對圖像取反 28 cv::threshold(foreground, foreground, 128, 255, cv::THRESH_BINARY_INV); 29 //顯示前景 30 cv::imshow("Extracted f",foreground); 31 //引入延遲;月可以通過按鍵Esc結束視頻 32 if(cv::waitKey(10)>=0) 33 stop = true; 34 } 35 }
總結:還是將常用的頭文件都寫上,以免出現這種沒必要的錯誤!!
- #include <cv.h>
- #include <cxcore.h>
- #include <highgui.h>
- #include <cvaux.h>
- #include <iostream>
- #include <string>