呃,網上的學習筆記好亂@@
還是自己邊學邊寫吧。不然幾個月后又忘記怎么弄了。
OpenCV如何在VS2010下配置的方法及OpenCV的介紹就不寫了。
如有需要,再寫如何配置的筆記。
第一個練習——【打開圖片】
鍵入代碼:
#include "stdafx.h"
#include "cv.h"
#include
#include
int _tmain(int argc, _TCHAR* argv[])
{
IplImage *img = cvLoadImage("funny-pictures.jpg");
cvNamedWindow("Image:",1);
cvShowImage("Image:",img);
cvWaitKey();
cvDestroyWindow("Image:");
cvReleaseImage(&img);
return 0;
}
程序運行后顯示如下:
代碼解析:
#include "stdafx.h"
#include "cv.h"
#include
#include
引用不再解釋,不太理解可詳見c++ premier。稍后轉錄過來。
int _tmain(int argc, _TCHAR* argv[])
在main函數中定義一個argc用于讀取輸入參數個數,argv[]數組用于存放輸入的參數。
如輸入test E:jay.jpg,argc讀取參數個數為2,argv[0]為test,而argv[1]中的E:jay.jpg讀出為E:\jay.jpg。這個具體原因待整理。更多詳見http://www.opencv.org.cn/index.p ... v%E8%AF%B4%E6%98%8E
IplImage *img = cvLoadImage("E:lena.jpg");
此行代碼是將圖像加載到內存,cvLoadImage()函數通過文件名確定被加載文件的格式并自動分配圖像所需內存。cvLoadImage()函數可以打開大部分常用圖像格式,如BMP,JPEG,JPG,PNG等。該函數執行完后會返回一個指針,該指針指向描述圖像文件數據結構IplImage分配的內存。E:lena.jpg為指定圖像所在地址,也可直接為lena.jpg,此處為程序默認儲存地址([VS2010默認項目文件夾]項目文件夾項目文件夾 下)內的圖片。lena.jpg原為大家圖像都很熟悉的草帽女,但是我測試的時候沒有去找就直接在e盤下重命名了張圖片。
cvNamedWindow("Image:",1);
cvNamedWindow()函數將在屏幕上創建一個窗口用于顯示圖像。函數中第一個參數為窗口命名為"Image:",第二個參數為定義窗口屬性,默認值為0,表示窗口大小不會因圖像的大小而改變,圖像將根據窗口大小進行變化充滿窗口。為1或CV_WINDOW_AUTOSIZE時,窗口將根據圖像的實際大小自動變化適應圖像。
cvShowImage("Image:",img);
cvShowImage()函數通過第一個參數確定在已創建的哪個窗口中顯示圖像,且該函數被調用時窗口將被重繪并將圖像顯示到窗口中。
cvWaitKey();
函數功能為使程序暫停,等待觸發按鍵。函數中參數為正值時,程序將暫停該整數值個毫秒后繼續執行程序,沒有按下按鍵也會如此。設置為0或者負數將一直等待用戶觸發按鍵。
vc++_2008和vc++_2010安裝方法可參考:http://www.opencv.org.cn/index.p ... E8%A3%85OpenCV2.3.1
vs2010配置VC++目錄可參考:http://blog.csdn.net/zhangyafengcpp/article/details/6847821
視頻文件處理:
第一段測試代碼
#include "stdafx.h"
#include
#include
//#include
#include
#include
#include
using namespace std;
int main()
{
IplImage *frame = NULL;
CvCapture *capture = NULL;
capture = cvCaptureFromAVI("D:\123\1.AVI");
frame = cvQueryFrame(capture);
cvNamedWindow("frame");
while(frame){
cvShowImage("frame", frame);
cvWaitKey(20);
cout << "Frame Grabbed." << endl;
frame = cvQueryFrame(capture);
}
return 0;
}
運行后界面顯示如下
前面的不再做過多解析@@
其實我也想不起來了,四天沒碰。。浪費時間啥也沒記住。。
cvCaptureFromAVI()通過參數讀入AVI文件及其所有信息并返回一個CvCapture指針,對人AVI視頻時參數為該指針的cvQueryFrame()將為圖像分配內存,與圖像顯示不同,不需要cvLoadImage為圖像分配內存。CvCapture被釋放后,每一幀圖像的內存空間也將被釋放。
cout函數讓圖像播放時將不斷輸出 Frame Grabbed.
第二段測試代碼
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
// 使用標準命名空間
using namespace std;
CvCapture* g_capture1 = NULL;
int main(int argc, char** argv )
{
// 建立播放窗口
cvNamedWindow( "Video Test 1", CV_WINDOW_AUTOSIZE );
// 捕捉視頻文件
argv[1]="D:\123\1.avi";
g_capture1 = cvCreateFileCapture( argv[1] );
// 開始播放并保存視頻
IplImage* frame1;
while(1)
{
// 獲取、顯示源文件的幀畫面
frame1 = cvQueryFrame( g_capture1 );
if( !frame1 ) break;
cvShowImage( "Video Test 1", frame1 );
// 若按下 ESC 鍵,則退出程序
char c = cvWaitKey(33);
if( c==27 ) break;
}
// 釋放內存,關閉窗口
cvReleaseCapture( &g_capture1 );
cvDestroyWindow( "Video Test 1" );
return 0;
}
下面段函數我電腦上運行不出結果。。待解決
// test2_video.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
//#include
#include
#include
// 使用標準命名空間
using namespace std;
// 初始化進度條的位置
int g_slider_position1 = 0;
int g_slider_position2 = 0;
CvCapture* g_capture1 = NULL;
CvCapture* g_capture2 = NULL;
// 定義回調函數用于播放進度的控制
void onTrackbarSlide1( int pos1 )
{
cvSetCaptureProperty( g_capture1, CV_CAP_PROP_POS_FRAMES, pos1 );
}
void onTrackbarSlide2( int pos2 )
{
cvSetCaptureProperty( g_capture2, CV_CAP_PROP_POS_FRAMES, pos2 );
}
int main(int argc, char** argv )
{
// 建立播放窗口
cvNamedWindow( "Video Test 1", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "Video Test 2", CV_WINDOW_AUTOSIZE );
// 捕捉視頻文件
g_capture1 = cvCreateFileCapture( argv[1] );
g_capture2 = cvCreateFileCapture( argv[2] );
// 讀取、顯示視頻文件的幀數
int frames1 = (int) cvGetCaptureProperty( g_capture1, CV_CAP_PROP_FRAME_COUNT );
cout << "frames1 = " << frames1 << endl;
// 建立進度條
if( frames1 != 0 )
cvCreateTrackbar(
"Position",
"Video Test 1",
&g_slider_position1,
frames1,
onTrackbarSlide1
);
int frames2 = (int) cvGetCaptureProperty( g_capture2, CV_CAP_PROP_FRAME_COUNT );
cout << "frames2 = " << frames2 << endl;
if( frames2 != 0 )
cvCreateTrackbar(
"Position",
"Video Test 2",
&g_slider_position2,
frames2,
onTrackbarSlide2
);
// 讀取視頻文件信息
double fps1 = (int) cvGetCaptureProperty( g_capture1, CV_CAP_PROP_FPS );
double fps2 = (int) cvGetCaptureProperty( g_capture2, CV_CAP_PROP_FPS );
CvSize size1 = cvSize(
(int)cvGetCaptureProperty(g_capture1, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(g_capture1, CV_CAP_PROP_FRAME_HEIGHT));
CvSize size2 = cvSize(
(int)cvGetCaptureProperty(g_capture2, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(g_capture2, CV_CAP_PROP_FRAME_HEIGHT));
// 創建 VideoWriter
CvVideoWriter* wrVideo1 = cvCreateVideoWriter(argv[3], CV_FOURCC('M','J','P','G'), fps1, size1);
CvVideoWriter* wrVideo2 = cvCreateVideoWriter(argv[4], CV_FOURCC('M','J','P','G'), fps2, size2);
int frs = 0;
// 開始播放并保存視頻
IplImage* frame1;
IplImage* frame2;
while( frs < frames1 && frs < frames2 )
{
// 獲取、顯示源文件的幀畫面
frame1 = cvQueryFrame( g_capture1 );
if( !frame1 ) break;
cvShowImage( "Video Test 1", frame1 );
frame2 = cvQueryFrame( g_capture2 );
if( !frame2 ) break;
cvShowImage( "Video Test 2", frame2 );
// 保存:將當前幀寫入到目標視頻文件
cvWriteFrame( wrVideo1, frame1 );
cvWriteFrame( wrVideo2, frame2 );
// 若按下 ESC 鍵,則退出程序
char c = cvWaitKey(33);
if( c==27 ) break;
}
// 釋放內存,關閉窗口
cvReleaseCapture( &g_capture1 );
cvReleaseCapture( &g_capture2 );
cvReleaseVideoWriter( &wrVideo1 );
cvReleaseVideoWriter( &wrVideo2 );
cvDestroyWindow( "Video Test 1" );
cvDestroyWindow( "Video Test 2" );
return 0;
}
呃,先貼過來,因為太多要測試的,邊測試邊寫
----------------------------------------
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include
#include
int main( int argc, char** argv )
{
IplImage* img = 0;
int nFrames = 50;
CvCapture* capture = 0;
CvVideoWriter *writer = 0;
int isColor = 1;
int fps = 25; // or 30
int frameW = 640; //
int frameH = 480; //
if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
if( !capture )
{
fprintf(stderr,"Could not initialize capturing...");
return -1;
}
writer=cvCreateVideoWriter("D:\out.avi",CV_FOURCC('X','V','I','D'),
fps,cvSize(frameW,frameH),isColor);
//存儲視頻文件 CvCapture* capture = cvCaptureFromCAM(0); // capture from video device #0
for(int i=0;i<nframes;i++)
{
cvGrabFrame(capture); // 抓取幀
img=cvRetrieveFrame(capture); // 恢復圖像
cvWriteFrame(writer,img); // 將幀添加入視頻文件
//顯示所抓視頻
cvNamedWindow("Live", CV_WINDOW_AUTOSIZE);//創建窗口
cvShowImage("Live", img);//顯示所抓視頻
cvWaitKey(50); // wait 20 ms
}
cvReleaseVideoWriter(&writer);
return 0;
}
錄制avi視頻
#include "cv.h"
#include "highgui.h"
#include
#include
int main( int argc, char** argv )
{
IplImage* img = 0;
int nFrames = 500;
CvCapture* capture = 0;
CvVideoWriter *writer = 0;
int isColor = 1;
int fps = 25; // or 30
int frameW = 640; //
int frameH = 480; //
if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
if( !capture )
{
fprintf(stderr,"Could not initialize capturing...");
return -1;
}
writer=cvCreateVideoWriter("D:\out.mp4",CV_FOURCC('D','I','V','X'),
fps,cvSize(frameW,frameH),isColor);
//存儲視頻文件CvCapture* capture = cvCaptureFromCAM(0); // capture from video device #0
for(int i=0;i<nframes;i++)
{
cvGrabFrame(capture); // 抓取幀
img=cvRetrieveFrame(capture); // 恢復圖像
cvWriteFrame(writer,img); // 將幀添加入視頻文件
//顯示所抓視頻
cvNamedWindow("Live", CV_WINDOW_AUTOSIZE);//創建窗口
cvShowImage("Live", img);//顯示所抓視頻
cvWaitKey(20); // wait 20 ms
}
cvReleaseVideoWriter(&writer);
return 0;
}
錄制mpga視頻
------------------------------------------------
#include "stdafx.h"
#include "highgui.h"
#include "cv.h"
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) //消除console窗口
//此處稍后寫學習筆記講解
//初始化進度條的位置
int g_slider_position=0;
CvCapture* g_capture=NULL;
//進度條的回調函數,播放進度控制
void onTrackbarSlide(int pos)
{
cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos);//設置視頻
}
int main(int argc, char* argv[])
{
//創建播放窗口
cvNamedWindow("Window Name", CV_WINDOW_AUTOSIZE);
//捕獲視頻
g_capture=cvCreateFileCapture("D:\out1.avi");
//獲取視頻的幀數
int frames = (int) cvGetCaptureProperty(g_capture,CV_CAP_PROP_FRAME_COUNT);
//建立進度條
if(frames!=0)
{
cvCreateTrackbar("Trackbar Name","Window Name",&g_slider_position,frames,onTrackbarSlide);
}
//捕獲、播放視頻
IplImage* frame;
while(1)
{
frame=cvQueryFrame(g_capture);
if( !frame ) break;
//獲取視頻播放位置
int trapos=(int)cvGetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES);
//設置進度條位置,使其和視頻播放同步
cvSetTrackbarPos("Trackbar Name","Window Name", trapos);
//播放視頻
cvShowImage("Window Name",frame);
//等待按鍵
char c=cvWaitKey(33);
if(c==27) break;
}
//釋放資源
cvReleaseCapture(&g_capture);
cvDestroyWindow( "Window Name");
return 0;
}
添加進度條成功
呃。由于是上一條錄制視頻的時候錄制的自己
所以播放也就播放自己了。。截圖截個搓搓的頭發吧
int g_slider_position=0;
CvCapture* g_capture=NULL;
為進度條添加全局變量。而回調函數需要使用CvCapture對象,故將它定義為全局變量。為增加程序可讀性,前加g_方便修改者尋找。
void onTrackbarSlide(int pos)
{
cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos);//設置視頻
}
定義一個在進度條被拖動時調用的回調函數。拖動條位置會被作為一個32位的整數以參數形式傳入。
CV_CAP_PROP_POS_FRAMES表示我們以幀數來設置讀入位置,FRAMES若用AVI_RATIO代替表示通過視頻長度比例來設置讀入位置。最后,新的進度條位置作為參數傳入。
int frames = (int) cvGetCaptureProperty(g_capture,CV_CAP_PROP_FRAME_COUNT);
需要從CvCapture結構查詢數據時,可使用cvGetCaptureProperty函數,我們希望獲得視頻文件的總幀數以對進度條進行設置。
if(frames!=0)
{
cvCreateTrackbar("Trackbar Name","Window Name",&g_slider_position,frames,onTrackbarSlide);
}
借助cvCreateTrackbar()創建進度條,可設置進度條名稱和所屬窗口。將一個變量綁定到進度條來表示其進度條最大值及一個回調函數(不需要回調函數的時候為空,進度條被拖動時觸發)。cvCreateTrackbar()返回幀數為0時,進度條不會被創建。因為有些編碼方式無法獲得幀數,這時候只能播放而無法獲得進度條。
------------------------------------------------------
圖像變換:
#include "stdafx.h"
#include "cv.h"
#include
#include
void example2_4( IplImage* image )
{
// Create some windows to show the inpu
// and output images in.
//
cvNamedWindow( "Example2_4-in", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "Example2_4-out", CV_WINDOW_AUTOSIZE );
// Create a window to show our input image
//
cvShowImage( "Example2_4-in", image );
// Create an image to hold the smoothed output
IplImage* out = cvCreateImage(
cvGetSize(image),
IPL_DEPTH_8U,
3
);
// Do the smoothing
//
cvSmooth( image, out, CV_GAUSSIAN, 5,5 );
cvSmooth( out, out, CV_GAUSSIAN, 5, 5);
// Show the smoothed image in the output window
//
cvShowImage( "Example2_4-out", out );
// Be tidy
//
cvReleaseImage( &out );
// Wait for the user to hit a key, then clean up the windows
//
cvWaitKey( 0 );
cvDestroyWindow("Example2_4-in" );
cvDestroyWindow("Example2_4-out" );
}
int main( int argc, char** argv )
{
argv[1]="E:lena.jpg";
IplImage* img = cvLoadImage( argv[1] );
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img );
example2_4( img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow("Example1");
}
cvShowImage()和學習筆記之初試中無異,cvCreateImage()來為新的幀分配空間且只分配一幀圖像的空間,再次調用時覆蓋前一次的數據(這樣每次調用返回的指針是一樣的)。
cvGetsize(image)獲得CvSize結構,第一個參數說明里當前圖像的結構大學結構,第二個參數告訴了我們各通道每個像素點的數據類型,最后一個參數說明了通道的總數。程序中圖像通道是3個(每個通道為8位)。圖像大小同image。
該例程為平滑處理函數,通過使用每個像素周圍3*3區域進行高斯平滑處理。
#include "stdafx.h"
#include "cv.h"
#include
#include
IplImage* doPyrDown(
IplImage* in,
int filter = IPL_GAUSSIAN_5x5)
{
// Best to make sure input image is divisible by two.
assert( in->width%2 == 0 && in->height%2 == 0 );
IplImage* out = cvCreateImage(
cvSize( in->width/2, in->height/2 ),
in->depth,
in->nChannels
);
cvPyrDown( in, out );
return( out );
};
int main( int argc, char** argv )
{
argv[1]="E:lena.jpg";
IplImage* img = cvLoadImage( argv[1] );
IplImage* img2 = cvCreateImage(
cvSize( img->width/2,img->height/2 ),
img->depth, img->nChannels);
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img );
img2 = doPyrDown( img );
cvShowImage("Example2", img2 );
cvWaitKey(0);
cvReleaseImage( &img );
cvReleaseImage( &img2 );
cvDestroyWindow("Example1");
cvDestroyWindow("Example2");
}
#include "stdafx.h"
#include "cv.h"
#include
#include
IplImage* doCanny(
IplImage* in,
double lowThresh,
double highThresh,
double aperture)
{
if (in->nChannels != 1)
return(0);
// Canny only handles gray scale images
IplImage* out = cvCreateImage(
cvGetSize( in ),
in->depth,
// IPL_DEPTH_8U,
1);
cvCanny( in, out, lowThresh, highThresh, aperture );
return( out );
};
int main( int argc, char** argv )
{
argv[1]="E:lena.jpg";
IplImage* img_rgb = cvLoadImage( argv[1] );
IplImage* img_gry = cvCreateImage(
cvSize( img_rgb->width,img_rgb->height ),
img_rgb->depth,
1);
cvCvtColor(img_rgb, img_gry ,CV_BGR2GRAY);
cvNamedWindow("Example Gray", CV_WINDOW_AUTOSIZE );
cvNamedWindow("Example Canny", CV_WINDOW_AUTOSIZE );
cvShowImage("Example Gray", img_gry );
IplImage* img_cny = doCanny( img_gry, 10, 100, 3 );
cvShowImage("Example Canny", img_cny );
cvWaitKey(0);
cvReleaseImage( &img_rgb);
cvReleaseImage( &img_gry);
cvReleaseImage( &img_cny);
cvDestroyWindow("Example Gray");
cvDestroyWindow("Example Canny");
}
#include "stdafx.h"
#include "cv.h"
#include
#include
IplImage* doCanny(
IplImage* in,
double lowThresh,
double highThresh,
double aperture)
{
IplImage* out = cvCreateImage(
cvGetSize( in ),
in->depth,
//IPL_DEPTH_8U,
1);
cvCanny( in, out, lowThresh, highThresh, aperture );
return( out );
};
IplImage* doPyrDown(
IplImage* in,
int filter = IPL_GAUSSIAN_5x5)
{
// Best to make sure input image is divisible by two.
// assert( in->width%2 == 0 && in->height%2 == 0 );
IplImage* out = cvCreateImage(
cvSize( in->width/2, in->height/2 ),
in->depth,
in->nChannels
);
cvPyrDown( in, out );
return( out );
};
int main( int argc, char** argv )
{
IplImage* img_rgb = cvLoadImage("E:\lena.jpg");
IplImage* img_gry = cvCreateImage( cvSize( img_rgb->width,img_rgb->height ), img_rgb->depth, 1);
cvCvtColor(img_rgb, img_gry ,CV_BGR2GRAY);
IplImage* img_pyr = doPyrDown( img_gry, IPL_GAUSSIAN_5x5 );
IplImage* img_pyr2 = doPyrDown( img_pyr, IPL_GAUSSIAN_5x5 );
IplImage* img_cny = doCanny( img_pyr2, 10, 100, 3 );
cvNamedWindow("Example Gray", CV_WINDOW_AUTOSIZE );
cvNamedWindow("Example Pyr", CV_WINDOW_AUTOSIZE );
cvNamedWindow("Example Canny", CV_WINDOW_AUTOSIZE );
cvShowImage("Example Gray", img_gry );
cvShowImage("Example Pyr", img_pyr2 );
cvShowImage("Example Canny", img_cny );
cvWaitKey(0);
cvReleaseImage( &img_rgb);
cvReleaseImage( &img_gry);
cvReleaseImage( &img_pyr);
cvReleaseImage( &img_pyr2);
cvReleaseImage( &img_cny);
cvDestroyWindow("Example Gray");
cvDestroyWindow("Example Pyr");
cvDestroyWindow("Example Canny");
}
|