不得不說(shuō)opencv是個(gè)強(qiáng)大的東東,以前做一個(gè)項(xiàng)目的一個(gè)模塊時(shí)使用到進(jìn)行圖形處理,這次是想將一個(gè)視頻的播放放慢,以前在網(wǎng)上看到opencv有這個(gè)功能,今天就不小心嘗試了下,東西不多,主要是做個(gè)小記錄還有一點(diǎn)要注意的小問(wèn)題說(shuō)一下,代碼不多,基本上也都是copy的網(wǎng)上的
#include <iostream> #include <assert.h> #include <opencv/cv.h> #include <opencv/highgui.h> #include <math.h>  using namespace std; #ifdef NDEBUG #pragma comment(lib, "highgui210.lib") #pragma comment(lib, "cxcore210.lib") #pragma comment(lib, "ml210.lib") #pragma comment(lib, "cv210.lib") #else #pragma comment(lib, "highgui210d.lib") #pragma comment(lib, "cxcore210d.lib") #pragma comment(lib, "ml210d.lib") #pragma comment(lib, "cv210d.lib") #endif  char g_fileName[] = "C://Users//Desktop//test.avi"; char g_winodwName[] = "Cv Test"; int main() {  ::cvNamedWindow("g_winodwName", CV_WINDOW_AUTOSIZE);  CvCapture *pCvCapture = NULL;  pCvCapture = cvCreateFileCapture(g_fileName);  assert(NULL != pCvCapture);  IplImage *pIplFrame = NULL;   char out1[] = "C://Users//Desktop//out1.avi";  double fps1 = cvGetCaptureProperty(pCvCapture,      CV_CAP_PROP_FPS);   CvSize size1 = cvSize((int)cvGetCaptureProperty(pCvCapture,        CV_CAP_PROP_FRAME_WIDTH),     (int)cvGetCaptureProperty(pCvCapture,        CV_CAP_PROP_FRAME_HEIGHT));    CvVideoWriter *wrVideo1 = cvCreateVideoWriter(out1,        CV_FOURCC('X','V','I','D'),        10,        size1);  IplImage *gray1 = cvCreateImage(size1,8,1);   while (true)  {  pIplFrame = cvQueryFrame(pCvCapture);  if (NULL == pIplFrame)  {   break;  }  else  {   ::cvShowImage(g_winodwName, pIplFrame);   //保存視頻文件   cvCvtColor(pIplFrame,gray1,CV_RGB2GRAY);   cvWriteFrame(wrVideo1,gray1);    if (27 == ::cvWaitKey(120))   {   break;   }  }  }    ::cvReleaseImage(&pIplFrame);  ::cvDestroyWindow(g_winodwName);  ::cvReleaseImage(&gray1);  ::cvReleaseVideoWriter(&wrVideo1);   return cin.get(); } 有幾個(gè)小地方需要注意下,第一個(gè)就是cvCreateFileCapture()這個(gè)函數(shù)如果路徑正確還是返回NULL,則一般是因?yàn)闆](méi)有安裝解碼器,一般有兩個(gè)解碼器k_lite和xvid,具體哪一個(gè)能行我不肯定,因?yàn)槲以嚨臅r(shí)候兩個(gè)都裝了然后就正確運(yùn)行了,還有一個(gè)就是cvCreateVideoWriter() 這個(gè)函數(shù)里面的參數(shù),第三個(gè)參數(shù)是設(shè)置視頻播放幀,數(shù)字越小,播放的速度越慢,應(yīng)該每秒的幀數(shù),保存視頻的原理也是將圖像幀保存到視頻文件中。也就是說(shuō)視頻的播放是一個(gè)個(gè)圖片幀的快閃。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選