1. 首先獲取ffmpeg,不裝這個OpenCV打不開很多視頻文件格式。 很多人找不到怎麼下載,其實之前ffmpeg可以通過cvs下載,不過最近他已經換成了更加強大的svn 如何使用SVN我這裡不再介紹,網上還有大量的安裝和使用的文章可以借鑒,這裡簡單羅列幾個SVN輔助的軟體: SubVersion,從 http://subversion.tigris.org/ 下載,支持linux,我們這裡就裝這個 TortoiseSVN,從 http://tortoisesvn.tigris.org/ 下載,是很不錯的SVN客戶端程序,為windows外殼程序集成到windows資源管理器和文件管理系統的Subversion客戶端,用起來很方便,commit動作變得就像Winrar右鍵壓縮一樣方便。 ok,那我們先裝subversion,記住最好之前裝過apr和apr-util,在apache.org網站能下到 wget http://subversion.tigris.org/downloads/subversion-1.3.2.tar.gz tar zvxf subversion-1.3.2.tar.gz cd subversion-1.3.2 ./configure --with-apr=/usr/local/apr-httpd --with-apr-util=/usr/local/apr-util-httpd/ make make install 到此,我們就可以通過svn命令獲取最新的ffmpeg了 svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg 你會發現在你所在的目錄,自動出現一個ffmpeg的目錄,就是你下載的源代碼。 2.下面是ffmpeg的編譯 ./configure --enable-libogg --enable-shared --enable-gpl (一定要加上 --enable-shared,不然OpenCV找不到ffmpeg庫) ================================================ 2006-10-3 1. 下載:opencv-0.9.9.tar.gz http://puzzle.dl.sourceforge.net ... opencv-0.9.9.tar.gz 2. #tar -xzvf opencv-0.9.9.tar.gz 3. #./configure --with-quicktime=no --with-ffmpeg (OpenCV默認用quicktime打開視頻文件,我把他改成ffmpeg) 4. #make 5. #make install 6. 相關配置 修改/etc/ld.so.conf 添加一行/usr/local/lib # ldconfig (root用戶) 然後將/usr/local/lib/pkgconfig中的opencv.pc 拷貝到/usr/lib/pkgconfig中,(如果不做這步,根本編譯不起) 可以採用這個操作 # cp /usr/local/lib/pkgconfig/opencv.pc /usr/lib/pkgconfig 7. 編輯opencv程序的方法 以編輯cvtest.c文件為例子(因為highgui中採用了c++,所以一定要用g++編譯才可以) A. g++ `pkg-config --cflags opencv` -o cvtest cvtest.c `pkg-config --libs opencv` B. 編譯: g++ `pkg-config --cflags opencv` -c cvtest.c 鏈接: g++ `pkg-config --libs opencv` -o cvtest cvtest.o ===================================================== 2006-10-3 例子: /************************************************** * 背景建模,運動物體檢測 * **************************************************/ /*********************************************************************** * OpenCV example * Copyright (C) 2006 Shiqi Yu * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ***********************************************************************/ #include #include #include #include int main( int argc, char** argv ) { //聲明IplImage指針 IplImage* pFrame = NULL; IplImage* pFrImg = NULL; IplImage* pBkImg = NULL; CvMat* pFrameMat = NULL; CvMat* pFrMat = NULL; CvMat* pBkMat = NULL; CvCapture* pCapture = NULL; int nFrmNum = 0; //創建窗口 cvNamedWindow("video", 1); cvNamedWindow("background",1); cvNamedWindow("foreground",1); //使窗口有序排列 cvMoveWindow("video", 30, 0); cvMoveWindow("background", 360, 0); cvMoveWindow("foreground", 690, 0); if( argc != 2 ) { fprintf(stderr, "Usage: bkgrd \n"); return -1; } //打開視頻文件 if( !(pCapture = cvCreateFileCapture(argv[1]))) { fprintf(stderr, "Can not open video file %s\n", argv[1]); return -2; } //逐幀讀取視頻 while(pFrame = cvQueryFrame( pCapture )) { nFrmNum++; //如果是第一幀,需要申請內存,並初始化 if(nFrmNum == 1) { pBkImg = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U,1); pFrImg = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U,1); pBkMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1); pFrMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1); pFrameMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1); //轉化成單通道圖像再處理 cvCvtColor(pFrame, pBkImg, CV_BGR2GRAY); cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY); cvConvert(pFrImg, pFrameMat); cvConvert(pFrImg, pFrMat); cvConvert(pFrImg, pBkMat); } else { cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY); cvConvert(pFrImg, pFrameMat); //高斯濾波先,以平滑圖像 //cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0); //當前幀跟背景圖相減 cvAbsDiff(pFrameMat, pBkMat, pFrMat); //二值化前景圖 cvThreshold(pFrMat, pFrImg, 60, 255.0, CV_THRESH_BINARY); //進行形態學濾波,去掉噪音 //cvErode(pFrImg, pFrImg, 0, 1); //cvDilate(pFrImg, pFrImg, 0, 1); //更新背景 cvRunningAvg(pFrameMat, pBkMat, 0.003, 0); //將背景轉化為圖像格式,用以顯示 cvConvert(pBkMat, pBkImg); //顯示圖像 cvShowImage("video", pFrame); cvShowImage("background", pBkImg); cvShowImage("foreground", pFrImg); //如果有按鍵事件,則跳出循環 //此等待也為cvShowImage函數提供時間完成顯示 //等待時間可以根據CPU速度調整 if( cvWaitKey(2) >= 0 ) break; } } //銷毀窗口 cvDestroyWindow("video"); cvDestroyWindow("background"); cvDestroyWindow("foreground"); //釋放圖像和矩陣 cvReleaseImage(&pFrImg); cvReleaseImage(&pBkImg); cvReleaseMat(&pFrameMat); cvReleaseMat(&pFrMat); cvReleaseMat(&pBkMat); return 0; }
[火星人
]
linux下的OpenCV安裝&學習筆記(修訂版,解決無法打開視頻文件問題 已經有490 次圍觀
本文地址: http://coctec.com/docs/linux/show-post-155384.html