Chapter 2 - Opencv

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

CHAPTER 2

Introduction to OpenCV
Getting Started
Sau khi ci t OpenCV library, nhim v u tin ca ta, t nhin, bt u v lm g hp dn. lm iu ny, ta s cn thit lp mi trng lp trnh. Trong Visual Studio, iu cn to mt project v cu hnh setup sao cho (a) cc th vin highgui.lib, cxcore.lib, ml.lib, v cv.lib c lin kt* v (b) preprocessor s tm OpenCV /opencv/*/include directories cho cc header files. Cc include directories s in hnh c tn g nh C:/program files/opencv/cv/include, /opencv/cxcore/include, /opencv/ml/include, v /opencv/otherlibs/highgui. Mt khi bn lm xong iu ny, bn c th to mt C file mi v bt u program u tin.
Lu : Cc header file chnh c th lm bn d th hn nhiu. Nhiu macro hu ch trong cc header files /opencv/cxcore/include/cxtypes.h v cxmisc.h. Nhng ci ny c th lm cc th nh khi to cc structures v arrays trong mt dng, sort lists, v hn na. Hu ht cc headers quan trng cho bin dch are .../cv/include/cv.h v /cxcore/include/cxcore.h cho computer vision, /otherlibs/highgui/highgui.h I/O, v /ml/include/ml.h machine learning.

First ProgramDisplay a Picture

OpenCV cung cp cc tin ch c t mt mng ln cc image file type cng nh t video v cameras. Cc tin ch l mt phn ca mt toolkit gi l HighGUI, m l c include trong OpenCV package. Ta s dng vi trong nhng tin ch to mt program n gin m m mt image v hin n trn screen. Xem V d 2-1.
V d 2-1. Mt OpenCV program n gin m ti mt image t disk v displays n trn screen
#include highgui.h int main( int argc, char** argv ) { IplImage* img = cvLoadImage( argv[1] ); cvNamedWindow( Example1, CV_WINDOW_AUTOSIZE ); cvShowImage( Example1, img ); cvWaitKey(0); cvReleaseImage( &img ); cvDestroyWindow( Example1 ); }

Khi bin dch v chy t command line vi mt tham s n, program ny ti nh t memory v displays n trn screen. N sau i n khi user nhn mt phm, m l lc nhng ng ca s v thot. Hy i qua program tng dng v mt mt lc hiu iu mi lnh lm. Dng ny ti image.* Function cvLoadImage() l mt high-level routine m d file format c ti da trn file name; n cng t ng cp memory cn cho image data structure. Lu rng cvLoadImage() c th c lng ln cc image format, gm BMP, DIB, JPEG, JPE, PNG, PBM, PGM, PPM, SR, RAS, v TIFF. Mt pointer n image data structure c cp sau c tr v. Structure ny, gi l IplImage, l OpenCV construct m vi n bn s lm vic nhiu nht. OpenCV dng structure ny handle tt c cc kiu ca image: single-channel, multichannel, integer-valued, floating-point-valued, et cetera. Ta dng pointer m cvLoadImage() tr v thao tc image v image data. Mt high-level function khc, cvNamedWindow() , m mt window trn screen m c th cha v display mt image. Function ny, c cp bi HighGUI library, cng t tn
cvNamedWindow( Example1, CV_WINDOW_AUTOSIZE ); IplImage* img = cvLoadImage( argv[1] );

cho window (trong trng hp ny, Example1). Cc li gi HighGUI tng lai m tng tc vi window ny s refer n n theo tn ny. Argument th hai cho cvNamedWindow() nh ngha cc window properties. N c th c t mt trong thnh 0 (value mc nh) hay thnh CV_WINDOW_AUTOSIZE . Trong trng hp trc, size ca window s l ging nhau bt chp image size, v image s c chnh va bn trong window. Trong trng hp sau, window s m rng hay thu hp t ng khi image c ti sao cho va vi size thc ca image. Bt c khi no ta c mt image theo dng ca mt IplImage* pointer, ta c th display n trong mt window hin c bng cvShowImage() . cvShowImage() function i hi mt window c tn hin tn ti (to bi cvNamedWindow() ). Trn li gi n cvShowImage(), window s c redrawn bng image thch hp trong n, v window s t resize thch hp nu n c to dng c CV_WINDOW_AUTOSIZE .
cvWaitKey() function yu cu program stop v i mt keystroke. Nu mt positive
cvWaitKey(0); cvShowImage( Example1, img );

argument c cho, program s i s milliseconds v sau tip tc ngay c khng c g c nhn. Nu argument c t thnh 0 hay s m, program s i v thi hn mt keypress. Mt khi ta i qua image, ta c th t do vi memory c cp. OpenCV mong mt pointer n IplImage* pointer cho hot ng ny. Sau khi li gi c hon thnh, pointer img s c t thnh NULL. Cui cng, ta c th destroy chnh window. Function cvDestroyWindow() s close window v de-allocate bt k vic dng memory c cp (gm image buffer ni ca window , m ang gi mt copy ca thng tin pixel t *img). Cho mt program n gin, bn khng thc s phi gi cvDestroyWindow() hay cvReleaseImage() v tt c resources v windows ca application c ng t ng bi operating system ngay khi thot, nhng l thi quen tt. By gi ta c program n gin ny ta c th chi vi n theo vi cch, nhng ta khng mun t lm kh. Nhim v tip theo s l dng mt chupwng trnh rt n gin hu ht n gin nh ci nyprogram c v display mt AVI video file. Sau , ta s bt u hn gn nhiu hn mt t.
cvDestroyWindow( Example1 ); cvReleaseImage( &img );

Second ProgramAVI Video/*/

Playing mt video vi OpenCV l hu ht d nh displaying mt picture n. Ch vn ta i din l ta cn vi kiu lp c tng frame theo trnh t; ta c th cng cn cch ra khi vng lp nu movie qu chn. Xem V d 2-2.
Example 2-2. A n gin OpenCV program for playing a video file from disk
#include highgui.h int main( int argc, char** argv ) { cvNamedWindow( Example2, CV_WINDOW_AUTOSIZE ); CvCapture* capture = cvCreateFileCapture( argv[1] ); IplImage* frame; while(1) { frame = cvQueryFrame( capture ); if( !frame ) break; cvShowImage( Example2, frame ); char c = cvWaitKey(33); if( c == 27 ) break; } cvReleaseCapture( &capture ); cvDestroyWindow( Example2 ); } } }

y ta bt u function main() bng to bnh thng mt window c tn, trong trng hp ny Example2. Cc th c mt t hp dn sau .
CvCapture* capture = cvCreateFileCapture( argv[1] );

Function cvCreateFileCapture() ly argument l tn ca AVI file c ti v sau tr v mt pointer n CvCapture structure. Structure ny cha tt c thng tin v AVI file ang c c, gm thng tin trng thi. Khi to theo cch ny, CvCapture structure c bi to n bt u ca AVI. Mt khi bn trong ca while(1) loop, ta bt u c t AVI file. cvQueryFrame() ly argument ca n mt pointer n CvCapture structure. N sau grabs video frame tip vo memory (memory m thc s l mt phn ca CvCapture structure). Mt pointer c tr v cho frame . Khng ging cvLoadImage , m thc s cp memory cho image, cvQueryFrame dng memory c cp trong CvCapture structure. Do n s khng cn (hay wise) gi cvReleaseImage() cho frame pointer ny. Thay vo , frame image memory s c gii phng khi CvCapture structure c gii phng. Mt khi ta displayed frame, ta sau i 33 ms.* Nu user bm mt key, th c s c t thnh ASCII value ca key ; nu khng, th n s c t thnh 1. Nu user bm Esc key (ASCII 27), th ta s thot read loop. Ngc li, 33 ms s qua v ta s ch thc thi loop li. Khng c g ng k, trong v d n gin ny, ta r rng khng iu khin tc ca video theo bt k cch thng minh no. Ta da vo vai tr trong cvWaitKey() nhp loading cc frame. Trong ng dng phc tp hn s khn ngoan khi c frame rate thc t CvCapture structure (t AVI) v hnh ng phf hp! Khi ta thot read loopv khng c nhiu video data hn hay v user bm Esc keyta c th free memory kt hp vi CvCapture structure. iu ny cng s ng bt k open file handles n AVI file.
cvReleaseCapture( &capture ); c = cvWaitKey(33); if( c == 27 ) break; frame = cvQueryFrame( capture );

Moving Around

OK, tuyt. By gi l lc kt ni, tng cng cc toy program ca ta, v khm ph mt t v kh nng sn c. iu u tin ta c th thng bo v AVI player ca Example 2-2 l n khng c cch di chuyn nhanh chng bn trong video. Nhim v tip theo s l thm mt slider bar, m s cho ta kh nng ny. HighGUI toolkit cung cp mt s cng c n gin lm vic vi cc image v video hn cc hm display n gin ta va gii thiu. Mt c ch c bit hu ch l slider, m cho php ta nhy d dng t mt phn ca video n phn khc. to mt slider, ta gi cvCreateTrackbar() v nhn din window m ta mun xut hin trong. ly chc nng mong mun, ta ch cn cung cp mt callback m s thc hin relocation. Example 2-3 cho cc chi tit.
Example 2-3. Program thm trackbar slider vo viewer window c bn: khi slider b di chuyn, function onTrackbarSlide() c gi v sau chuyn gi tr mi ca slide
#include cv.h #include highgui.h 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( Example3, CV_WINDOW_AUTOSIZE ); g_capture = cvCreateFileCapture( argv[1] ); int frames = (int) cvGetCaptureProperty( g_capture, CV_CAP_PROP_FRAME_COUNT ); if( frames!= 0 ) { cvCreateTrackbar( Position,

V bn cht, sau , chin thut ny l thm mt bin ton cc biu din v tr slider v sau thm mt callback m cp nht bin ny v relocates v tr c trong video. Mt li gi to slider v gn callback, v ta xong v chy.* Hy quan st chi tit. u tin ta nh ngha bin ton cc cho v tr slider. Callback s cn truy cp n capture object, do ta a ra cho bin ton cc. V ta tt vi mi ngi v thch code d c v d hiu, ta chp nhp quy nh thm mt tin t g_ vi bt k bin ton cc.
void onTrackbarSlide(int pos) { cvSetCaptureProperty( g_capture, CV_CAP_PROP_POS_FRAMES, pos ); int g_slider_position = 0; CvCapture* g_capture = NULL;

); } IplImage* frame; // While loop (as in Example 2) capture & show video // Release memory v destroy window return(0);

Example3, &g_slider_position, frames, onTrackbarSlide

By gi ta nh ngha callback routine c dng khi user y slider. Routine ny s c chuyn thnh mt 32-bit integer, m s l v tr slider. Li gi n cvSetCaptureProperty() l ci ta s thy thng trong tng lai, cng vi bn sao cvGetCaptureProperty() ca n. Nhng routine ny cho php ta cu hnh (hay query trong trng hp sau) cc thuc tnh khc ca CvCapture object. Trong trng hp ny ta chuyn argument CV_CAP_PROP_POS_FRAMES , m nhn bit rng ta thch t v tr c theo n v ca frame. (Ta c th dng AVI_RATIO thay v FRAMES nu ta mun t v tr nh phn trm ca chiu di video ton b). Cui cng, ta chuyn gi tr mi ca v tr vo. V HighGUI c khai ha cao, n s t ng handle cc vn ny nh kh nng m frame ta yu cu khng l mt key-frame; n s bt u key-frame trc v chuyn nhanh n frame yu cu khng cn ta phi quan trng cc chi tit.
int frames = (int) cvGetCaptureProperty( g_capture, CV_CAP_PROP_FRAME_COUNT );

Nh ha, ta dng cvGetCaptureProperty() khi ta mun query vi data t CvCapture structure. Trong trng hp ny, ta mun tm thy bao nhiu frames trong video sao cho ta c th hiu chnh slider (trong bc tip theo).
if( frames!= 0 ) { cvCreateTrackbar( Position, Example3, &g_slider_position, frames, onTrackbarSlide ); }

Chi tit cui to chnh trackbar. Function cvCreateTrackbar() cho php ta cho trackbar mt label* (trong trng hp ny l Position) v ch nh window t trackbar vo. Ta sau cung cp mt bin m s c gn vi trackbar, value cc i ca trackbar, v mt callback (hay NULL nu ta khng mt ci) cho khi slider c di chuyn. Quan st thy ta khng to trackbar nu cvGetCaptureProperty() tr v frame count khng. y l v vi, ph thuc vo cch video c encod, tng s frame s khng sn c. Trong trng hp ny ta s ch play movie khng c cung cp trackbar.

ng khi slider to bi HighGUI khng chc nng nh vi slider bn ngoi. D nhin, khng c l do bn khng th dng windowing toolkit bn thch thay v HighGUI, nhng cc HighGUI tool l nhanh thc hin v tha mn ta. Cui cng, ta khng include extra tidbit ca code cn lm slider di chuyn khi video play. iu ny c nh bi tp cho ngi c.

A Simple Transformation

Tuyt, by gi bn c th dng OpenCV to video player ring, m s khng khc nhiu vi cc countless video player bn ngoi hin c. Nhng ta thch computer vision, v ta mun lm g . Nhiu vision task c bn lin quan ng dng ca cc filter vi mt video stream. Ta s thay i program ta phi lm mt tc v n gin trn mi frame ca video khi n play. Mt tc v c bit n gin trong lm trn image, m gim hiu qu ni dung thng tin ca bi convolving n vi mt Gaussian hay kernel function tng t khc. OpenCV lm cc convolution nh th l d. Ta c th bt u bi to mt window mi gi l Example4-out, ni ta c th display cc kt qu ca processing. Sau , sau khi ta gi cvShowImage() display captured frame mi trong input window, ta c th tnh v display nh lm trn trong output window. Xem Example 2-4.
Example 2-4. Loading v sau lm trn image trc khi n c hin th trn screen
#include cv.h #include highgui.h void example2_4( IplImage* image ) // to some windows to show the input // v output images in. // cvNamedWindow( Example4-in );

Example 2-4. Loading v then smoothing an image trc khi it is displayed on the screen (continued)
cvNamedWindow( Example4-out ); // to a window to show our input image // cvShowImage( Example4-in, image ); // to an image to hold the smoothed output // IplImage* out = cvCreateImage( cvGetSize(image), IPL_DEPTH_8U, 3 ); // Do the smoothing // cvSmooth( image, out, CV_GAUSSIAN, 3, 3 ); // Show the smoothed image in the output window // cvShowImage( Example4-out, out ); // Be tidy // cvReleaseImage( &out ); // Wait for the user to hit a key, then clean up the windows // cvWaitKey( 0 ); cvDestroyWindow( Example4-in ); cvDestroyWindow( Example4-out );

Li gi u tin n cvShowImage() khng khc vi v d trc. Trong li gi tip, ta cp mt image structure khc. Trc y ta da vo cvCreateFileCapture() cp frame mi cho ta. Tht ra, routine thc s cp ch mt frame v sau vit data mi ln li gi capture c lm (do n thc s c tr cng pointer li lm ta gi n). Trong trng hp ny, tuy nhin, ta mun allocate image structure ring m ta c th vit smoothed image. Argument u tin l mt CvSize structure, m ta c th to thun li bi gi cvGetSize(image); iu ny cho ta size ca structure image hin ti. Argument th hai ni ta kiu ca data type c dng cho mi channel trn mi pixel, v argument

cui nhn bit s cc channel. Do image ny l ba channels (vi 8 bits mi channel) v cng size vi image. Tc v smoothing chnh n ch l mt li gi n OpenCV library: ta ch nh input image, output image, phng php smoothing, v cc parameter smooth. Trong trng hp ny ta yu cu Gaussian smooth trn 3 3 area tm trn mi pixel. N thc s c php cho output l ging nh input image, v iu ny s hiu qu hn trong application hin ti ca ta, nhng ta trnh lm iu ny v n cho ta c hi gii thiu cvCreateImage()! By gi ta c th thy image trong window th hai mi v sau gii phng n: cvReleaseImage() ly mt pointer n IplImage* pointer v sau de-allocates tt c memory gn vi image .

A Not-So-Simple Transformation

l rt tt, v ta ang hc lm nhiu th th v. Trong Example 2-4 ta chn allocate mt IplImage structure mi , v trong structure mi ny ta vit output ca mt transformation n. Nh cp, ta c th p dng transformation theo mt cch m output vit chng ln ci ban u, nhng iu ny khng lun l tng tt. C th, vi operator khng sn cc image vi cng size, depth, v s cc channel nh input image. in hnh, ta mun thc hin mt chui cc operation trn vi image ban u v do sn ra mt chui cc transformed image. Trong cc trng hp ny, thng hu ch gii thiu cc wrapper functions n gin m allocate c output image v thc hin transformation ta thch. Lu , v d, vic gim image bi factor 2 [Rosenfeld80]. Trong OpenCV iu ny c hon thnh bi function cvPyrDown(), m thc hin Gaussian smooth v sau xa mi ng khc t image. iu ny hu ch trong lng ln cc vision algorithm quan trng. Ta c th thc hin function n gin m t trong Example 2-5.
Example 2-5. dng cvPyrDown() to to a new image that is half the widthv height of the input image
IplImage* doPyrDown( IplImage* in, int filter = IPL_GAUSSIAN_5x5 ){ // Best to m bo 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 ); };

Lu rng ta allocate image mi bi c cc parameter cn t image c. Trong OpenCV, tt c cc data type quan trng c thc hin nh cc structure v c chuyn nh cc structure pointer. Khng c cc th nh private data trong OpenCV! By gi hy quan st iu tng t nhng dnh dng nhiu hn lin qua Canny edge detector [Canny86] (xem Example 2-6). Trong trng hp ny, edge detector pht mt image m l size y ca input image nhng cn ch mt channel image n vit.
Example 2-6. The Canny edge detector writes its output to a single channel (grayscale) image
IplImage* doCanny( IplImage* in, double lowThresh, double highThresh, double aperture ){ If(in->nChannels != 1)

iu ny cho php ta ni cng nhau cc operator khc nhau rt d. V d, nu ta mun co image hai ln v thi tm cc ng m c trong image c gim ln hai, ta c th lm nh trong Example 2-7.
Example 2-7. Combining the pyramid down operator (twice) v the Canny subroutine in a n gin image pipeline
IplImage* img1 = doPyrDown( in, IPL_GAUSSIAN_5x5 ); IplImage* img2 = doPyrDown( img1, IPL_GAUSSIAN_5x5 ); IplImage* img3 = doCanny( img2, 10, 100, 3 ); // do whatever withimg3 // cvReleaseImage( &img1 ); cvReleaseImage( &img2 ); cvReleaseImage( &img3 );

};

return(0); //Canny only handles gray scale images IplImage* out = cvCreateImage( cvSize( cvGetSize( in ), IPL_DEPTH_8U, 1 ); cvCanny( in, out, lowThresh, highThresh, aperture ); return( out );

iu quan trng quan st rng chum li gi vi cc bc khc nhau ca ng filtering khng l tng tt, v sau ta s khng c cch gii thng cc image m ta cp cng vi cch ny. Nu ta qu li lm vic dn dp ny, ta c th chn include dng sau trong mi ca cc wrapper: C ch t lm sch ny rt gn, nhng n s c bt li sau: nu ta thc s mun lm g ci ci trong cc image trung gian, ta phi truy cp vo n. gii quyt vn , code trc c th c n gin nh m t trong Example 2-8.
Example 2-8. n gin ng image ca Example 2-7 bi lm cc bc release ring cc memory allocation trung gian
IplImage* out; out = doPyrDown( in, IPL_GAUSSIAN_5x5 ); out = doPyrDown( out, IPL_GAUSSIAN_5x5 ); out = doCanny( out, 10, 100, 3 ); // do whatever without // cvReleaseImage ( &out ); cvReleaseImage( &in );

Mt t cui ca cnh bo ng filter t lm sch: trong OpenCV ta phi lun lun chc rng image (hay structure khc) c de-allocated l ci, tht ra, c cp r rng trc y. Quan st trng hp ca IplImage* pointer returned bi cvCreateFileCapture(). y pointer tr n mt structure c cp nh mt phn ca CvCapture structure, v structure ch c cp pht ch mt khi khi CvCapture c khi to v mt AVI c ti. De-allocating structure ny bng mt li gi n cvRelease Image() s c kt qu trong vi ngc nhin kh chu. Tinh thn ca chuyn ny l, mc d n quan trng chm sc garbage collection trong OpenCV, ta nn ch dn dp rc m ta to.

Input from a Camera

Vision c th lm nhiu th trong th gii ca cc computer. Trong vi trng hp ta phn tch cc frame lin tc ti t u . Trong cc trng hp khc ta phn tch video m c t a. Trong cc trng hp khc na, ta mun lm vic vi data streaming thi gian thc t vi kiu camera device. OpenCVc bit hn, phn HighGUI ca OpenCV librarycung cp cch d handle trng hp ny. Phng php tng t vi cch ta c cc AVI. Thay v gi

cvCreateFileCapture(), ta gi cvCreateCameraCapture(). Routine sau khng nhn mt file

name m l camera ID number nh argument ca n. D nhin, iu ny l quan trng ch khi nhiu cameras sn c. Value mc nh l 1, m c ngha ch ly mt; t nhin, iu ny l rt d khi ch c mt camera ly (xem chng 4 chi tit hn). cvCreateCameraCapture() function tr v cng CvCapture* pointer, m ta c th sau dng chnh xc nh ta lm vi cc frame ly t mt video stream. D nhin, nhiu vic din ra hu trng lm mt chui cc camera image trng nh mt video, nhng ta b ngn vi tt c iu . Ta c th n gin ly cc image t camera bt c khi no ta sn sng cho chng v tin hnh nh nu ta khng bit s khc bit. Cho cc nguyn nhn pht trin, hu ht ng dng m c tp trung lm vic real time s c mt video-in mode, v s ph bin ca CvCapture structure lm iu ny c bit d thc hin. xem Example 2-9.
Example 2-9. sau khi the capture structure is initialized, it no longer matters c hay khng the image is from a camera hay a file
CvCapture* capture; if( argc==1 ) { capture = cvCreateCameraCapture(0); } else { capture = cvCreateFileCapture( argv[1] ); } assert( capture != NULL ); // Rest of program proceeds totally ignorant

Nh bn c th thy, arrangement ny l rt l tng.

Writing to an AVI File

Trong nhiu applications ta s mun record streaming input hay ngay c cc captured image khc hn thnh mt output video stream, v OpenCV cung cp mt method d dng lm iu ny. Ch nh ta c th to mt capture device m cho php ta ly cc frame mt thi im t video stream, ta c th to mt writer device m cho php ta t cc frame tng ci mt vo video file. Routine m cho php ta lm iu ny l cvCreateVideoWriter(). Mt khi li gi ny c lm, ta c th gi lin tip cvWriteFrame(), cho mi frame, v cui cng cvReleaseVideoWriter() khi ta xong. Example 2-10 m t mt program n gin m m mt video file, c ni dung, chuyn chng thnh mt logpolar format (th g mt bn thc s thy, c m t trong chng 6), v vit ra log-polar image vo mt video file mi.
Example 2-10. A complete program to read in a color video v write out the same video in grayscale
// Convert a video to grayscale // argv[1]: input video file // argv[2]: name of new output file // #include cv.h #include highgui.h main( int argc, char* argv[] ) { CvCapture* capture = 0; capture = cvCreateFileCapture( argv[1] ); if(!capture){ return -1; } IplImage *bgr_frame=cvQueryFrame(capture);//Init the video read double fps = cvGetCaptureProperty ( capture, CV_CAP_PROP_FPS ); CvSize size = cvSize( (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),

Nhn ton b program ny l ra cc phn t quen thuc. Ta m mt video; bt u c bng cvQueryFrame(), m cn c cc thc tnh video trn vi system; v sau dng cvGetCaptureProperty() chc chn cc thuc tnh quan trng khc ca video stream. Ta sau m mt video file vit, convert frame thnh logpolar format, v vit cc frame vo file mi ny tng ci n khi khng cn ci no. Sau ta ng li. Li gi n cvCreateVideoWriter() cha vi parameter m ta nn hiu. u tin ch l filename cho file mi. Th hai l video codec m video stream s c nn. C vo s cc codec hin hnh, nhng codec m bn chn phi sn c trn my bn (codecs c ci c lp vi OpenCV). Trong trng hp ca ta ta chn MJPG codec tng i ph bin; this is indicated to OpenCV by dng the macro CV_FOURCC(), m takes four characters as arguments. nhng ci ny characters constitute the four-character code of the codec, v every codec has such a code. The four-character code for motion jpeg is MJPG, so ta ch nh that as CV_FOURCC(M,J,P,G). The next hai arguments are the replay frame rate, v the size of the images ta s be using. In our case, ta set nhng ci ny to the values ta got from the original (color) video.

(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT) ); CvVideoWriter *writer = cvCreateVideoWriter( argv[2], CV_FOURCC(M,J,P,G), fps, size ); IplImage* logpolar_frame = cvCreateImage( size, IPL_DEPTH_8U, 3 ); while( (bgr_frame=cvQueryFrame(capture)) != NULL ) { cvLogPolar( bgr_frame, logpolar_frame, cvPoint2D32f(bgr_frame->width/2, bgr_frame->height/2), 40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS ); cvWriteFrame( writer, logpolar_frame ); } cvReleaseVideoWriter( &writer ); cvReleaseImage( &logpolar_frame ); cvReleaseCapture( &capture ); return(0);

Onward

Trc khi chuyn n chng tip theo, ta nn dnh t thi gian ly stock ni ta v chun b tin ln. Ta thy rng OpenCV API cung cp ta mt lng cc cng c d dng ti still image t files, c video t disk, hay capturing video t cameras. Ta cng thy rng library ny cha cc primitive function thao tc nhng images ny. iu ta cha thy l cc phn t mnh m ca library, m cho php thao tc phc tp hn ca tp ton b cc abstract data types m l quan trng gii quyt vn n vision thc t. Trong vi chng tip ta s o su hn vo c bn v tm hiu chi tit hn c v cc interface-related function v image data types. Ta s xem xt cc primitive image manipulation operator v, sau , vi th cao cp hn nhiu. Sau , ta s sn sng khm ph nhiu services c bit m API cung cp cho cc nhim v khc nh camera calibration, tracking, v recognition. S sng cha? i thi!

Exercises

Download v install OpenCV nu bn hin cha c. Duyt h thng qua directory structure. Lu c th docs directory; ni bn c th load index.htm, m cc link xa hn n documentation chnh ca library. Khm ph xa hn cc vng chnh ca library.

Cvcore cha cc data structures v algorithms c bn, cv cha image processing v vision algorithms, ml bao gm algorithms cho machine learning v clustering, v otherlibs/highgui cha the I/O functions. Check out the _make directory (containing the OpenCV build files) v also cc sample directory, where example code c lu. 1. Go to the /opencv/_make directory. On Windows, open the solution file opencv .sln; on Linux, open the appropriate makefile. Build the library in c the debug and the release versions. This c th take some time, but bn s need kt quing library v dll files. 2. Go to the /opencv/samples/c/ directory. to a project hay make file and then import v build lkdemo.c (this is mt v d motion tracking program). Attach a camera to your system v chy the code. With the display window selected, type r to initialize tracking. Bn c th add points by clicking on video positions with the mouse. Bn c th also switch to watching only the points (and not the image) by typing n. Typing n again s toggle gia night v day views. 3. dng the capture v store code in Example 2-10, cng nhau with the doPyrDown() code of Example 2-5 to to a program that reads from a camera v stores downsampled color images to disk. Modify the code in exercise 3 v combine it with4. the window display code in Example 2-1 to display the frames as they are processed. 5. Modify the program of exercise 4 witha slider control from Example 2-3 sao cho the user c th dynamically vary the pyramid downsampling reduction level by factors of gia 2 v 8. Bn c th skip writing this to disk, but bn should display the results.

You might also like