You are on page 1of 11

OpenCV Tutorial II: Video Processing

Xuan Mo
iPAL Group Meeting

February 11, 2011

Outline

Reading video Writing video Edge Detection Demo: Laplacian edge detection

2/11/2011

iPAL Group Meeting

Capturing a frame from a video sequence


Initializing capture from a camera: CvCapture capture = cvCaptureF romCAM (0); capture from video device 0 Initializing capture from a le: CvCapture capture = cvCaptureF romAV I(inf ile.avi); Capturing a frame: cvGrabF rame(capture); retrieve the captured frame: img = cvRetrieveF rame(capture); Releasing the capture source: cvReleaseCapture(&capture); Dont forget to release!

2/11/2011

iPAL Group Meeting

Get capture device properties

Get capture device properties: cvQueryF rame(capture); this call is necessary to get correct capture properties cvGetCaptureP roperty(capture, property id, value); property id: CV CAP P ROP F RAM E HEIGHT CV CAP P ROP F RAM E W IDT H CV CAP P ROP F P S CV CAP P ROP F RAM E COU N T Frame count does not seem to be working properly.

2/11/2011

iPAL Group Meeting

Get frame information

Function is the same: cvGetCaptureP roperty(capture, property id, value); property id: CV CAP P ROP P OS M SEC CV CAP P ROP P OS F RAM ES CV CAP P ROP P OS AV I RAT IO(0 1) and so on...

Example: f loat posRatio = cvGetCaptureP roperty(capture, CV CAP P ROP P OS AV I RAT IO)

2/11/2011

iPAL Group Meeting

Set frame information

Set capture device properties: cvSetCaptureP roperty(capture, property id, value); sets the specied property of camera or AVI. property id: The same as getting frame information Example: cvSetCaptureP roperty (capture, CV CAP P ROP P OS AV I RAT IO, 0.9); start capturing from a relative position of 0.9 of a video le

2/11/2011

iPAL Group Meeting

Write/save video
First initializing a video writer: CvV ideoW ritercvCreateV ideoW riter (f ilename, f ourcc, f ps, f rame size, is color = 1) fourcc: four-Character Codes CV F OU RCC( P , I , M , 1 ) : M P EG 1 CV F OU RCC( M , J , P , G ) : motion jpeg CV F OU RCC( M , P , 4 , 2 ) : M P EG 4.2 CV F OU RCC( D , I , V , 3 ) : M P EG 4.3 CV F OU RCC( D , I , V , X ) : M P EG 4 = M P EG 1 CV F OU RCC( U , 2 , 6 , 3 ) : H263 CV F OU RCC( I , 2 , 6 , 3 ) : H263I CV F OU RCC( F , L , V , 1 ) : F LV 1 Example: CvV ideoW riterwriter = 0; writer = cvCreateV ideoW riter(. . . )
2/11/2011 iPAL Group Meeting 7

Write/save video
Then writing the video le: add the frame to the le cvW riteF rame(writer, img); Example: IplImage img = 0; intnF rames = 50; f or(i = 0; i < nF rames; i + +){ cvGrabF rame(capture); img = cvRetrieveF rame(capture); cvW riteF rame(writer, img); } view the captured frames during capture cvShowImage(mainW in, img); key = cvW aitKey(20); wait 20 ms wait 20 ms in order to display properly

2/11/2011

iPAL Group Meeting

Edge detection

search-based

... ...
1 2

Computing a measure of edge strength, usually a rst-order derivative expression such as the gradient magnitude. Searching for local directional maxima of the gradient magnitude using a computed estimate of the local orientation of the edge, usually the gradient direction.

zero-crossing based search for zero crossings in a second-order derivative expression computed from the image in order to nd edges, usually the zero-crossings of the Laplacian or the zero-crossings of a non-linear dierential expression.

2/11/2011

iPAL Group Meeting

Laplacian edge detection

2/11/2011

iPAL Group Meeting

10

Demo: Laplacian edge detection

...
1

Capture the video from WebCam


Function: cvCaptureF romCAM

. . . .

.. Smoothing the frames


2

Function: cvSmooth

.. Split into dierent color spaces


3

Function: cvSplit

.. Add Laplacian lter in each color space 4


Function: cvLaplace
5

...
6

Merge the 3 color spaces


Function: cvM erge

.. Show the frame


Function: cvShowImage

2/11/2011

iPAL Group Meeting

11

You might also like