數(shù)字媒體技術(shù)基礎(chǔ)OpenCV自學(xué)材料概述和作用_第1頁(yè)
數(shù)字媒體技術(shù)基礎(chǔ)OpenCV自學(xué)材料概述和作用_第2頁(yè)
數(shù)字媒體技術(shù)基礎(chǔ)OpenCV自學(xué)材料概述和作用_第3頁(yè)
數(shù)字媒體技術(shù)基礎(chǔ)OpenCV自學(xué)材料概述和作用_第4頁(yè)
數(shù)字媒體技術(shù)基礎(chǔ)OpenCV自學(xué)材料概述和作用_第5頁(yè)
已閱讀5頁(yè),還剩110頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、數(shù)字媒體技術(shù)基礎(chǔ)OpenCV自學(xué)材料概述和作用 第一部分OpenCV概述基礎(chǔ)結(jié)構(gòu)與操作基本OpenCV程序與示例注:本講義中部分代碼來(lái)自O(shè)penCV樣例,部分代碼來(lái)自O(shè)penCV教程基礎(chǔ)篇,部分代碼則從網(wǎng)上收集。OpenCV概述OpenCV是Intel開(kāi)源計(jì)算機(jī)視覺(jué)庫(kù)。它由一系列 C 函數(shù)和少量 C+ 類構(gòu)成,實(shí)現(xiàn)了圖像處理和計(jì)算機(jī)視覺(jué)方面的很多通用算法。OpenCV 的特點(diǎn)擁有包括 300 多個(gè)C函數(shù)的跨平臺(tái)的中、高層 API跨平臺(tái):Windows, Linux免費(fèi)(FREE):無(wú)論對(duì)非商業(yè)應(yīng)用和商業(yè)應(yīng)用 速度快使用方便OpenCV structureCXCOREbasic structur

2、es and algoritms,XML support, drawing functionsCVImage processingand visionHighGUIGUI, Image and Video I/OMLMachine Learning algorithmsCVCamvideo stream processingThe Functionality in PicturesOpenCV的下載與安裝1、下載地址 :/download/2、下的安裝與配置參見(jiàn) 3、.NET 2003下的安裝與配置參見(jiàn)Configuring MSVS .net 2k3Create a Win32 Consol

3、e ProjectMake it an Empty Project by selecting the box under Application SettingsA project is initially created by selecting:File - New - ProjectCreating the ProjectConfiguring MSVS .net 2k3Right Click the Source Files Folder under the project name (Tutorial in this case)Add - Add new ItemCreate the

4、 First FileSelect C+ file and give it a nameCreating a it possible to set Additional Include Directives in the C/C+ pane under the project properties.Configuring MSVS .net 2k3In order to build projects using OpenCV the required libraries and directives must be included in the projects propertiesRigh

5、t Click the name of the project and select Properties (Tutorial in this case)Open the Properties PaneConfiguring MSVS .net 2k3Under the C/C+ tab select GeneralSet Additional Include DirectivesSelect the Additional Include DirectivesAdd the full path to each of the folders which contain .h files requ

6、ired to use OpenCVBe sure to include trailing C:Program FilesOpenCVcvauxincludeC:Program FilesOpenCVcxcoreincludeC:Program FilesOpenCVcvincludeC:Program FilesOpenCVotherlibshighguiC:Program FilesOpenCVotherlibscvcaminclude Utilized DirectivesConfiguring MSVS .net 2k3Under the Linker tab select Input

7、Set Additional DependenciesSelect the Additional DependenciesC:Program FilesOpenCVlibcv.libC:Program FilesOpenCVlibcvaux.libC:Program FilesOpenCVlibcxcore.libC:Program FilesOpenCVlibcvcam.libC:Program FilesOpenCVlibhighgui.libUtilized DependenciesAdd the full path to each of the .lib files required

8、to use OpenCVBe sure to keep the paths in quotesTesting MSVS .net 2k3Now that the environment is configured it would be a good idea to test it to make sure that a program will correctly build and run.#include #include /*This will pop up a small box with Hello World as the text.*/int main( int argc,

9、char* argv ) /declare for the height and width of the image int height = 320; int width = 240; /specify the point to place the text CvPoint pt = cvPoint( height/4, width/2 ); /Create an 8 bit, 3 plane image IplImage* hw = cvCreateImage(cvSize(height, width), 8, 3); /initialize the font CvFont font;

10、cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX,1.0, 1.0, 0, 1, CV_AA); /place the text on the image using the font cvPutText(hw, Hello World, pt, &font, CV_RGB(150, 0, 0) ); /create the window container cvNamedWindow(Hello World, 0); /display the image in the container cvShowImage(Hello World, hw); /hol

11、d the output windows cvWaitKey(0); return 0;The enclosed code can be cut and pasted into the in the project space to test OpenCVTesting the First ProgramTesting MSVS .net 2k3Output of ProgramThe program is built by selecting:Build - Build SolutionOr by pressing F7Building the ProgramThe program is r

12、un by selecting:Debug - Start|Start without DebuggingOr by pressing F5 or -F5Running the ProgramOpenCV 編碼樣式指南1、文件命名:有cv和cvaux庫(kù)文件的命名必須服從于以下規(guī)則: 所有的CV庫(kù)文件名前綴為cv 混合的C/C+接口頭文件擴(kuò)展名為 .h 純C+接口頭文件擴(kuò)展名為 .hpp 實(shí)現(xiàn)文件擴(kuò)展名為 .cpp 為了與POSIX兼容,文件名都以小寫(xiě)字符組成OpenCV 編碼樣式指南2、文件結(jié)構(gòu)每個(gè)文件以BSD兼容的許可聲明(模板在文件中可以找到)開(kāi)頭;一行最多90個(gè)字符,不包括行結(jié)束符 不使

13、用制表符 縮進(jìn)為4個(gè)空格符,所以制表符應(yīng)該用1-4個(gè)空格替換(依據(jù)開(kāi)始列確定) 頭文件必須使用保護(hù)宏,防止文件被重復(fù)包含?;旌螩/C+接口頭文件用extern C 包含C語(yǔ)言定義。為了使預(yù)編譯頭機(jī)制在Visual C+中工作正常,源文件必須在其它頭文件前包含頭文件。OpenCV 編碼樣式指南3、命名約定OpenCV中使用大小寫(xiě)混合樣式來(lái)標(biāo)識(shí)外部函數(shù)、數(shù)據(jù)類型和類方法。宏全部使用大寫(xiě)字符,詞間用下劃線分隔。所有的外部或內(nèi)部名稱,若在多個(gè)文件中可見(jiàn),則必須含有前綴: 外部函數(shù)使用前綴cv 內(nèi)部函數(shù)使用前綴Icv 數(shù)據(jù)結(jié)構(gòu)(C結(jié)構(gòu)體、枚舉、聯(lián)合體、類)使用前綴Cv 外部或某些內(nèi)部宏使用前綴CV_ 內(nèi)

14、部宏使用前綴ICV_ OpenCV 編碼樣式指南4、函數(shù)接口設(shè)計(jì):為了保持庫(kù)的一致性,以如下方式設(shè)計(jì)接口非常重要。函數(shù)接口元素包括: 功能 名稱 返回值 參數(shù)類型 參數(shù)順序 參數(shù)默認(rèn)值 函數(shù)功能必須定義良好并保持精簡(jiǎn)。函數(shù)應(yīng)該容易鑲?cè)氲绞褂闷渌麿penCV函數(shù)的不同處理過(guò)程。函數(shù)名稱應(yīng)該簡(jiǎn)單并能體現(xiàn)函數(shù)的功能。大多數(shù)函數(shù)名形式:cvA Simple OpenCV Program1. #include 2. #include 3. #include 4. int main( int argc, char* argv ) 5. CvPoint center;6. double scale=-3;7

15、. IplImage* image = argc=2 ? cvLoadImage(argv1) : 0;8. if(!image) return -1;9. center = cvPoint(image-width/2,image-height/2);10. for(int i=0;iheight;i+)11. for(int j=0;jwidth;j+) 12. double dx=(double)(j-center.x)/center.x;13. double dy=(double)(i-center.y)/center.y;14. double weight=exp(dx*dx+dy*d

16、y)*scale);15. uchar* ptr = &CV_IMAGE_ELEM(image,uchar,i,j*3);16. ptr0 = cvRound(ptr0*weight);17. ptr1 = cvRound(ptr1*weight);18. ptr2 = cvRound(ptr2*weight); 19. cvSaveImage(copy.png, image );20. cvNamedWindow( test, 1 );21. cvShowImage( test, image );22. cvWaitKey();23. return 0; 基本數(shù)據(jù)結(jié)構(gòu)點(diǎn):CvPoint 、C

17、vPoint2D32f、CvPoint3D32f 矩形框大?。篊vSize 、CvSize2D32f 矩形框:CvRect 可以存放1-4個(gè)數(shù)值的數(shù)組:CvScalar 定義迭代算法的終止規(guī)則:CvTermCriteria 矩陣:CvMat 、CvMatND 、CvSparseMat IPL圖像頭部:IplImage 定義不確定的數(shù)組:CvArr (僅作函數(shù)參數(shù))點(diǎn)數(shù)據(jù)結(jié)構(gòu)CvPoint 二維坐標(biāo)系下的點(diǎn),類型為整型 typedef struct CvPoint int x; /* X坐標(biāo), 通常以0為基點(diǎn) */int y; /* y坐標(biāo), 通常以0為基點(diǎn) */CvPoint;/* 構(gòu)造函數(shù)

18、*/inline CvPoint cvPoint( int x, int y );/* 從 CvPoint2D32f類型轉(zhuǎn)換得來(lái) */inline CvPoint cvPointFrom32f( CvPoint2D32f point )CvPoint2D32f :二維坐標(biāo)下的點(diǎn),類型為浮點(diǎn)CvPoint3D32f :三維坐標(biāo)下的點(diǎn),類型為浮點(diǎn)矩形框大小數(shù)據(jù)結(jié)構(gòu)CvSize 矩形框大小,以像素為精度 typedef struct CvSizeint width; /* 矩形寬 */int height; /* 矩形高 */CvSize;/* 構(gòu)造函數(shù) */inline CvSize cvSize

19、( int width, int height );CvSize2D32f 矩形框數(shù)據(jù)結(jié)構(gòu)CvRect 矩形框的偏移和大小 typedef struct CvRectint x; /* 方形的最左角的x-坐標(biāo) */int y; /* 方形的最上或者最下角的y-坐標(biāo) */int width; /* 寬 */int height; /* 高 */CvRect;/* 構(gòu)造函數(shù)*/inline CvRect cvRect(int x, int y, int width, int height);矩陣數(shù)據(jù)結(jié)構(gòu)CvMat 二維矩陣 typedef struct CvMat int type; /* CvM

20、at 標(biāo)識(shí), 元素類型和標(biāo)記 */int step; /* 以字節(jié)為單位的行數(shù)據(jù)長(zhǎng)度*/int rows; /*行數(shù)*/int cols; /*列數(shù)*/ int* refcount; /* 數(shù)據(jù)引用計(jì)數(shù) */unionuchar* ptr;short* s;int* i;float* fl;double* db; data; /* data 指針 */ 矩陣數(shù)據(jù)結(jié)構(gòu)CvMatND:多維、多通道密集數(shù)組 CvSparseMat:多維、多通道稀疏數(shù)組CvArr:不確定數(shù)組圖像頭數(shù)據(jù)IplImage:IPL 圖像頭圖像頭數(shù)據(jù)IplImage:IPL 圖像頭typedef struct _IplIma

21、ge int nSize; /* IplImage大小 */int ID; /* 版本 (=0)*/int nChannels; /* 大多數(shù)OPENCV函數(shù)支持1,2,3 或4 個(gè)通道 */int alphaChannel; /* 被OpenCV忽略 */int depth; /* 像素的位深度: IPL_DEPTH_8U,IPL_DEPTH_8S, IPL_DEPTH_16U,IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F 可支持 */char colorModel4; char channelSeq4; /* 被

22、OpenCV忽略 */int dataOrder; /* 0 - 交叉存取顏色通道, 1 - 分開(kāi)的顏色通道.cvCreateImage只能創(chuàng)建交叉存取圖像 */int origin; /* 0 - 頂左結(jié)構(gòu),1 - 底左結(jié)構(gòu) (BMP風(fēng)格) */int align; /* 圖像行排列 (4 or 8). OpenCV 用widthStep 代替 */int width; /* 圖像寬像素?cái)?shù) */int height; /* 圖像高像素?cái)?shù)*/struct _IplROI *roi;/* 圖像感興趣區(qū)域. 當(dāng)該值非空只對(duì)該區(qū)域進(jìn)行處理 */struct _IplImage *maskROI; /

23、* 在 OpenCV中必須置NULL */void *imageId; /* 同上*/struct _IplTileInfo *tileInfo; /*同上*/int imageSize; /* 圖像數(shù)據(jù)大小,單位字節(jié)*/char *imageData; /* 指向排列的圖像數(shù)據(jù) */int widthStep; /* 排列的圖像行大小,以字節(jié)為單位 */int BorderMode4; int BorderConst4; /* 邊際結(jié)束模式, 被忽略*/char *imageDataOrigin; /* 指針指向一個(gè)不同的圖像數(shù)據(jù)結(jié)構(gòu),是為了糾正圖像內(nèi)存分配準(zhǔn)備的 */IplImage;圖像

24、頭數(shù)據(jù)IplImage結(jié)構(gòu)來(lái)自于 Intel Image Processing Library。OpenCV 只支持其中的一個(gè)子集: alphaChannel 在OpenCV中被忽略。colorModel 和channelSeq 被OpenCV忽略。dataOrder 必須是IPL_DATA_ORDER_PIXEL (顏色通道是交叉存取),然而平面圖像的被選擇通道可以被處理,就像COI(感興趣的通道)被設(shè)置過(guò)一樣。align 是被OpenCV忽略的,而用 widthStep 去訪問(wèn)后繼的圖像行。不支持maskROI 。處理MASK的函數(shù)把他當(dāng)作一個(gè)分離的參數(shù)。MASK在 OpenCV 里是 8

25、-bit,然而在 IPL他是 1-bit。tileInfo 不支持。BorderMode和BorderConst是不支持的。OpenCV處理ROI有不同的要求。要求原圖像和目標(biāo)圖像的尺寸或 ROI的尺寸必須精確匹配。基本OpenCV操作矩陣的使用與操作GUI命令圖像的使用與操作視頻的使用與操作矩陣的使用與操作(1)創(chuàng)建矩陣 CreateMatCvMat* cvCreateMat( int rows, int cols, int type );rows 矩陣行數(shù)。 cols 矩陣列數(shù)。 type 矩陣元素類型。 通常以 CV_(S|U|F)C型式描述, 例如: CV_8UC1 意思是一個(gè)8-bi

26、t 無(wú)符號(hào)單通道矩陣, CV_32SC2 意思是一個(gè)32-bit 有符號(hào)二個(gè)通道的矩陣。 函數(shù) cvCreateMat 為新的矩陣分配頭和下面的數(shù)據(jù),并且返回一個(gè)指向新創(chuàng)建的矩陣的指針。矩陣按行存貯。所有的行以4個(gè)字節(jié)對(duì)齊。刪除矩陣 ReleaseMat void cvReleaseMat( CvMat* mat );例如:CvMat* M = cvCreateMat( 4, 4, CV_32FC1); cvReleaseMat( &M);矩陣的使用與操作(2)復(fù)制矩陣CloneMatCvMat* cvCloneMat( const CvMat* mat );例如: CvMat* M1 = c

27、vCreateMat( 4, 4, CV_32FC1);CvMat* M2;M2 = cvCloneMat(M1);初始化矩陣方法1:用cvMat初始化 double a = 1, 2, 3, 4,5, 6, 7, 8,9, 10, 11, 12 ; CvMat Ma = cvMat( 3, 4, CV_64FC1, a);方法2:用cvCreateMatHeader初始化 CvMat Ma; cvInitMatHeader( &Ma, 3, 4, CV_64FC1, a);初始化單位矩陣CvMat* M = cvCreateMat( 4, 4, CV_32FC1);cvSetIdentity

28、(M);矩陣的使用與操作(3)訪問(wèn)矩陣元素(1)直接訪問(wèn)cvmSet(M, i, j, 2, 0); /Set M(i,j)t = cvmGet(M, i, j); /Get M(i,j)(2)已知對(duì)齊方式的直接訪問(wèn)CvMat* M = cvCreateMat( 4, 4, CV_32FC1); int n = M-cols;float *data = M-data.fl;datai*n+j = 3.0; /假設(shè)32位對(duì)齊(3)未知對(duì)齊方式的直接訪問(wèn)CvMat* M = cvCreateMat( 4, 4, CV_32FC1); int step = M-step/sizeof(float);

29、float *data = M-data.fl;(data + i*step)j = 3.0;(4)直接訪問(wèn)一個(gè)已初始化的矩陣Mai*4 + j = 2.0;矩陣的使用與操作(4)矩陣間的操作CvMat *Ma, *Mb, *Mc;cvAdd(Ma, Mb, Mc); /Ma + Mb - MccvSub(Ma, Mb, Mc); /Ma - Mb - MccvMatMul(Ma, Mb, Mc); /Ma * Mb - Mc矩陣元素間的操作cvMul(Ma, Mb, Mc); /Ma. * Mb - MccvDiv(Ma, Mb, Mc); /Ma. / Mb - MccvAddS(Ma,

30、cvScalar(-10.0), Mc); /Ma. -10 - Mc單個(gè)矩陣的操作cvTranspose(Ma, Mb); /transpose(Ma) - MbCvScalar t=cvTrace(Ma); /trace(Ma) -t.val0double d = cvDet(Ma); /det(Ma) -dcvInvert(Ma, Mb); /inv(Ma) -Mb矩陣的使用與操作(5)向量乘法假設(shè)Va, Vb, Vc均為n元素向量double res = cvDotProduct(&Va, &Vb); /Va Vb - rescvCrossProduct(&Va, &Vb , &Vc)

31、; / Va Vb - Vc矩陣特征值分解假設(shè)A, E均為n*n方陣,I為n元素向量cvEigenVV(&A, &E , &I); SVD 假設(shè)A, U, D, V均為n*n方陣cvSVD(A, D, U, V, CV_SVD_U_T| CV_SVD_V_T); /A=UDVT標(biāo)志使U和V以轉(zhuǎn)置方式返回非齊次線性系統(tǒng)的求解假設(shè)A為n*n方陣,x,b均為n元素向量cvSolve(&A, &b, &x)矩陣的使用與操作:示例(1)#include cv.h#include highgui.h#include void PrintMat(CvMat *A); / 顯示矩陣void Test_Mult

32、iply(); / 測(cè)試矩陣乘法void Test_DCT(); / 計(jì)算DCT變換int main() Test_Multiply(); Test_DCT(); return 0;矩陣的使用與操作:示例(2)void PrintMat(CvMat* A) / 顯示矩陣int i,j;for(i=0;irows;i+) printf(n); switch( CV_MAT_DEPTH(A-type) ) case CV_32F: case CV_64F: for(j=0; jcols; j+) printf(%9.3f , (float) cvGetReal2D( A, i, j ); brea

33、k; case CV_8U: case CV_16U: for(j=0;jcols;j+) printf(%6d,(int)cvGetReal2D( A, i, j ); break; default: break; printf(n);矩陣的使用與操作:示例(3)void Test_Multiply()/ Test matrix multiply double a = 1, 2, 3, 4,5, 6, 7, 8, 9, 10, 11, 12 ; double b = 1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12 ; double c9; CvMat Ma, Mb

34、, Mc; printf(n= Test multiply =); cvInitMatHeader( &Ma, 3, 4, CV_64FC1, a, CV_AUTOSTEP ); cvInitMatHeader( &Mb, 4, 3, CV_64FC1, b, CV_AUTOSTEP ); cvInitMatHeader( &Mc, 3, 3, CV_64FC1, c, CV_AUTOSTEP ); cvMatMulAdd( &Ma, &Mb, 0, &Mc ); PrintMat(&Ma); PrintMat(&Mb); PrintMat(&Mc); return;矩陣的使用與操作:示例(4

35、)void Test_DCT()/ test 1-d and 2-d dct transform float data = 1, 2, 3, 4, 5, 6, 7, 8 ; CvMat a; a = cvMat(2,4,CV_32FC1,data); printf(n= Test DCT =); printf(nOriginal matrix = ); PrintMat(&a); cvDCT(&a, &a, CV_DXT_FORWARD); printf(n2-D DCT = ); PrintMat(&a); cvDCT(&a, &a, CV_DXT_INVERSE); printf(n2-D

36、 IDCT = ); PrintMat(&a);Discrete Cosine Transform(DCT)Transform kernel of two-dimensional DCT iswhereClearly, the kernel for the DCT is both separable and symmetric, and hence the DCT may be implemented as a series of one dimensional DCTs.Discrete Cosine Transform(DCT)Forward transform and Inverse t

37、ransformwhere u, v, x, y = 0, 1, 2, , N-1Transform Matrix of DCTwhere:Example of DCTDCT的幾點(diǎn)說(shuō)明DCT變換避免了復(fù)數(shù)運(yùn)算。由于圖像矩陣是實(shí)數(shù)矩陣,那么它的DCT也是實(shí)數(shù)DCT是正交變換,其變換矩陣是正交陣,變換核是可分離的。DCT有快速算法DCT與IDCT具有相同的變換核,因此具有相同的變換矩陣,即正變換與逆變換公用同一個(gè)算法模塊DCT具有更強(qiáng)的信息集中能力,能將最多的信息放到最小的系數(shù)上去。HighGUI操作Smart windowsImage I/O, renderingProcessing keybo

38、ard and other events, timeoutsTrackbarsMouse callbacksVideo I/OWindowscvNamedWindow(window_name, fixed_size_flag)cvNamedWindow(window_name, x, y)cvDestroyWindow(window_name)creates window accessed by its name. Window handles repaint, resize events. Its position is remembered in registry.cvNamedWindo

39、w(ViewA,1);cvMoveWindow(ViewA,300,100);cvDestroyWindow(ViewA);cvShowImage(window_name, image);copies the image to window buffer, then repaints it when necessary. 8u|16s|32s|32fC1|3|4 are supported.only the whole window contents can be modified. Dynamic updates of parts of the window are done using o

40、perations on images, drawing functions etc.Windows等待按鍵cvWaitKeyint cvWaitKey( int delay=0 )如果delay=0, 則無(wú)限等待,則等待delay毫秒則返回在程序循環(huán)中,有時(shí)候由于程序一直處于計(jì)算中,窗口無(wú)法重新恢復(fù)(如讀出視頻中的所有幀并顯示),可以加入cvWaitKey,使之等待幾毫秒,讓窗口完成重新繪制再執(zhí)行其他操作圖像的使用與操作(1)創(chuàng)建頭并分配數(shù)據(jù) CreateImageIplImage* cvCreateImage( CvSize size, int depth, int channels );

41、size:圖像寬、高. depth:圖像元素的位深度,IPL_DEPTH_8U|8S|16U|16S|32S|32F|64F channels:每個(gè)元素(像素)的顏色通道數(shù)量.可以是 1, 2, 3 或 4.釋放頭和圖像數(shù)據(jù) ReleaseImagevoid cvReleaseImage( IplImage* image );復(fù)制圖像 CloneImageIplImage* cvCloneImage( const IplImage* image );圖像的使用與操作(2)頭分配CreateImageHeaderIplImage* cvCreateImageHeader( CvSize size

42、, int depth, int channels )初始化被用圖分配的圖像頭 InitImageHeaderIplImage* cvInitImageHeader( IplImage* image, CvSize size, int depth,int channels, int origin=0, int align=4 );origin IPL_ORIGIN_TL 或 IPL_ORIGIN_BL. align 圖像行排列, 典型的 4 或 8 字節(jié). 函數(shù) cvInitImageHeader 初始化圖像頭結(jié)構(gòu), 指向用戶指定的圖像并且返回這個(gè)指針。釋放頭 ReleaseImageHead

43、ervoid cvReleaseImageHeader( IplImage* image );程序示例:Create image#include cv.h#include highgui.h #include int main() IplImage *cvImg; / image used for visualisationCvSize imgSize; / size of visualisation imageint i = 0, j = 0;imgSize.width = 640; imgSize.height = 480; cvImg = cvCreateImage( imgSize,

44、8, 1 ); / creation of a 8 bits depth gray imagefor ( i = 0; i imgSize.width; i+ ) / image is filled with gray values corresponding for ( j = 0; j imageData + cvImg-widthStep*j)i = ( char ) ( ( i * j ) % 256 );cvNamedWindow( Testing OpenCV., 1 ); / creation of a visualisation windowcvShowImage( Testi

45、ng OpenCV., cvImg ); / image visualisationcvWaitKey( 0 ); / wait for keycvDestroyWindow( image ); / close windowcvReleaseImage( &cvImg ); / memory release return( 0 ); / stopping the program程序示例:Create image圖像的使用與操作(3)從文件讀圖像cvLoadImageIplImage* cvLoadImage(char* , int flag=1)OpenCV支持的圖像格式:BMP、DIB、JP

46、G、PNG、PBM、PGM、PPM、SR、RAS和TIFF寫(xiě)圖像到文件cvSaveImageIplImage* cvSaveImage(char* , IplImage* img)圖像轉(zhuǎn)換cvConvertImage(IplImage* src, IplImage* dst, int flags=0); /灰度圖彩色圖cvCvtColor(IplImage* src, IplImage* dst, int code); /彩色圖彩色圖/灰度圖Code=CV_2:,=RGB,BGR,GRAY,HSV,YCrCb,XYZ,Luv,HLS 圖像裝載與顯示示例#include cv.h#include

47、 highgui.h#include / in the images directorychar name0 = images/example.bmp;/ in the images directorychar name1 = images/example.jpg;圖像裝載與顯示示例int main() IplImage* img0 = NULL;IplImage* img1 = NULL;img0 = cvLoadImage( name0, -1 );img1 = cvLoadImage( name1, -1 );cvNamedWindow( image0, 1 );cvNamedWindo

48、w( image1, 1 );cvShowImage( image0, img0 );cvShowImage( image1, img1 );cvWaitKey(0); / wait for key to close the windowscvReleaseImage( &img0 );cvReleaseImage( &img1 );return(0);圖像處理函數(shù)cvSub( img0, img1, res, 0 ) subtract img0 from img1 and store the result in res休息第二部分基本視頻操作OpenCV的動(dòng)態(tài)結(jié)構(gòu)及操作基于OpenCV的圖像

49、/視頻處理(6個(gè)示例)基于OpenCV的研究與開(kāi)發(fā):DEMO視頻的使用和操作(1)打開(kāi)攝像頭CvCapture* cvCaptureFromCAM(camera_id=0);打開(kāi)文件CvCapture* cvCaptureFromFile(video);CvCapture* cvCaptureFromAVI(inflie.avi);捕捉某一幀 cvGrabFrame(capture) /抓住一幀,為快速遍歷視頻幀 IplImage* img=cvRetrieveImage(capture);/把Grab的幀取出,或 IplImage* cvQueryFrame(capture);釋放捕捉源cv

50、ReleaseCapture(&capture);視頻的使用和操作(2)保存視頻文件typedef struct CvVideoWriter; CvVideoWriter* cvCreateVideoWriter( const char* , int fourcc, double fps, CvSize frame_size, int is_color=1 );int cvWriteFrame( CvVideoWriter* writer, const IplImage* image );void cvReleaseVideoWriter( CvVideoWriter* writer );獲取

51、/設(shè)置視頻幀信息cvGetCaptureProperty(capture, property_id);cvSetCaptureProperty(capture, property_id, value);CV_CAP_PROP_POS_MSEC - video capture timestampCV_CAP_PROP_POS_FRAMES - 0-based index of the frame CV_CAP_PROP_POS_AVI_RATIO - relative position of video(0-start, 1-end)CV_CAP_PROP_FRAME_WIDTH - width

52、 of frames in the video streamCV_CAP_PROP_FRAME_HEIGHT - height of frames in the video streamCV_CAP_PROP_FPS - frame rateCV_CAP_PROP_FOURCC - 4-character code of codec CV_CAP_PROP_FRAME_COUNT - number of frames in video file視頻操作示例(1)功能:從攝像頭或者AVI文件中得到視頻流,對(duì)視頻流進(jìn)行邊緣檢測(cè),并輸出結(jié)果。主要思想:獲取視頻( cvCaptureFromCAM 或

53、cvCaptureFromAVI)循環(huán)處理每幀( cvQueryFrame )將多通道數(shù)組分割為多個(gè)單通道數(shù)組(cvCvtPixToPlan)在每個(gè)通道內(nèi)進(jìn)行Laplace變換( cvLaplace),計(jì)算圖像的邊緣信息恢復(fù)多通道圖像( cvCvtPlaneToPix )并顯示Laplace變換Laplace變換:工程數(shù)學(xué)中常用的一種積分變換。普拉斯變換的存在性:關(guān)于一個(gè)函數(shù)f(t)的拉普拉斯變換,只有在拉普拉斯積分是收斂的情況下才存在。也就是說(shuō), f(t)必須是在對(duì)于t0的每一個(gè)有限區(qū)間內(nèi)都是片斷性連續(xù)的,且當(dāng)t趨于無(wú)窮大的時(shí)候, f(t)是指數(shù)階地變化。拉普拉斯變換的重大意義在于:將一個(gè)信

54、號(hào)從時(shí)域上,轉(zhuǎn)換為復(fù)頻域(s域)上來(lái)表示Laplace變換void cvLaplace(const CvArr *src, CvArr * dst, int aperture_size=0)1、用Sobel算子計(jì)算圖像二階x和y的差分src:輸入圖像;dst:輸出圖像;xorder:x方向的差分階數(shù);yorder:y方向的差分階數(shù)2、按如下公式求和對(duì)aperture_size=1則給出最快計(jì)算結(jié)果,相當(dāng)于對(duì)每個(gè)圖像采用如下內(nèi)核做卷積視頻操作示例(1)#include cv.h#include highgui.h#include #include int main( int argc, char

55、* argv ) IplImage* laplace = 0; IplImage* colorlaplace = 0; IplImage* planes3 = 0, 0, 0 ; / 多個(gè)圖像面 CvCapture* capture = 0;If(argc=1|(argc=2& strlen(argv1) = 1&isdigit(argv10) capture = cvCaptureFromCAM(argc = 2 ? argv10 - 0 : 0 ); else if( argc = 2 ) capture = cvCaptureFromAVI( argv1 ); if( !capture

56、) fprintf(stderr,Could not initialize capturing.n); return -1; 視頻操作示例(2) cvNamedWindow( Laplacian, 0 ); for(;) / 循環(huán)捕捉,直到用戶按鍵跳出循環(huán)體 IplImage* frame = 0; int I; frame = cvQueryFrame( capture ); if( !frame ) break; if( !laplace ) for( i = 0; i width,frame-height),8,1); laplace = cvCreateImage( cvSize(fr

57、ame-width,frame-height), IPL_DEPTH_16S, 1 ); /存放單通道Laplace結(jié)果 colorlaplace = cvCreateImage( cvSize(frame-width,frame-height), 8, 3 ); /存放多通道Laplace結(jié)果 cvCvtPixToPlane( frame, planes0, planes1, planes2, 0 );/將多通道/數(shù)組分割為多個(gè)單通道數(shù)組 for( i = 0; i origin = frame-origin; cvShowImage(Laplacian, colorlaplace ); i

58、f( cvWaitKey(10) = 0 ) break; cvReleaseCapture( &capture ); cvDestroyWindow(Laplacian); return 0;OpenCV的動(dòng)態(tài)結(jié)構(gòu)及操作動(dòng)態(tài)結(jié)構(gòu)內(nèi)存存儲(chǔ)序列集合與稀疏矩陣數(shù)據(jù)保存配置文件動(dòng)態(tài)結(jié)構(gòu):when 2d array is not enoughThe sample task: collect the locations of all non-zero pixels in the image.Where to store the locations? Possible solutions:Allocate

59、 array of maximum size (sizeof(CvPoint)*width*height a huge value)Use two-pass algorithmUse list or similar data structure (Any other ideas?)/ construct sequence of non-zero pixel locationsCvSeq* get_non_zeros( const IplImage* img, CvMemStorage* storage ) CvSeq* seq = cvCreateSeq( CV_32SC2, sizeof(C

60、vSeq), sizeof(CvPoint), storage ); for( int i = 0; i height; i+ ) for( int j = 0; j width; j+ ) if( CV_IMAGE_ELEM(img, uchar, i, j) ) CvPoint pt=j,i; cvSeqPush( seq, &pt ); return seq; /CvSeq:定義非固定元素的序列內(nèi)存管理Memory storage is a linked list of memory blocks.Functions to remember: cvCreateMemStorage(blo

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論