1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
While OpenCV was designed for use in full-scale
applications and can be used within functionally rich UI frameworks (such as Qt, WinForms or Cocoa) or without any UI at all, sometimes there is a need to try some functionality quickly and visualize the results. This is what the HighGUI module has been designed for.
It provides easy interface to:
\begin{itemize}
\item create and manipulate windows that can display images and "remember" their content (no need to handle repaint events from OS)
\item add trackbars to the windows, handle simple mouse events as well as keyboard commmands
\item read and write images to/from disk or memory.
\item read video from camera or file and write video to a file.
\end{itemize}
\ifCPy
\section{User Interface}
\ifC
\cvCPyFunc{ConvertImage} % XXX:TBD
Converts one image to another with an optional vertical flip.
\cvdefC{void cvConvertImage( const CvArr* src, CvArr* dst, int flags=0 );}
\begin{description}
\cvarg{src}{Source image.}
\cvarg{dst}{Destination image. Must be single-channel or 3-channel 8-bit image.}
\cvarg{flags}{The operation flags:
\begin{description}
\cvarg{CV\_CVTIMG\_FLIP}{Flips the image vertically}
\cvarg{CV\_CVTIMG\_SWAP\_RB}{Swaps the red and blue channels. In OpenCV color images have \texttt{BGR} channel order, however on some systems the order needs to be reversed before displaying the image (\cross{ShowImage} does this automatically).}
\end{description}}
\end{description}
The function \texttt{cvConvertImage} converts one image to another and flips the result vertically if desired. The function is used by \cross{ShowImage}.
\fi
\cvCPyFunc{CreateTrackbar}
Creates a trackbar and attaches it to the specified window
\cvdefC{
int cvCreateTrackbar( \par const char* trackbarName, \par const char* windowName,
\par int* value, \par int count, \par CvTrackbarCallback onChange );
}
\cvdefPy{CreateTrackbar(trackbarName, windowName, value, count, onChange) -> None}
\begin{description}
\cvarg{trackbarName}{Name of the created trackbar.}
\cvarg{windowName}{Name of the window which will be used as a parent for created trackbar.}
\ifPy
\cvarg{value}{Initial value for the slider position, between 0 and \texttt{count}.}
\else
\cvarg{value}{Pointer to an integer variable, whose value will reflect the position of the slider. Upon creation, the slider position is defined by this variable.}
\fi
\cvarg{count}{Maximal position of the slider. Minimal position is always 0.}
\ifPy
\cvarg{onChange}{
OpenCV calls \texttt{onChange} every time the slider changes position.
OpenCV will call it as \texttt{func(x)} where \texttt{x} is the new position of the slider.}
\else
\cvarg{onChange}{
Pointer to the function to be called every time the slider changes position.
This function should be prototyped as \texttt{void Foo(int);} Can be NULL if callback is not required.}
\fi
\end{description}
The function \texttt{cvCreateTrackbar} creates a trackbar (a.k.a. slider or range control) with the specified name and range, assigns a variable to be syncronized with trackbar position and specifies a callback function to be called on trackbar position change. The created trackbar is displayed on the top of the given window.\\\\
\textbf{[Qt Backend Only]} qt-specific details:
\begin{description}
\cvarg{windowName}{Name of the window which will be used as a parent for created trackbar. Can be NULL if the trackbar should be attached to the control panel.}
\end{description}
The created trackbar is displayed at the bottom of the given window if \emph{windowName} is correctly provided, or displayed on the control panel if \emph{windowName} is NULL.
By clicking on the label of each trackbar, it is possible to edit the trackbar's value manually for a more accurate control of it.
\ifC
\begin{lstlisting}
CV_EXTERN_C_FUNCPTR( void (*CvTrackbarCallback)(int pos) );
\end{lstlisting}
\fi
\cvCPyFunc{DestroyAllWindows}
Destroys all of the HighGUI windows.
\cvdefC{void cvDestroyAllWindows(void);}
\cvdefPy{DestroyAllWindows()-> None}
The function \texttt{cvDestroyAllWindows} destroys all of the opened HighGUI windows.
\cvCPyFunc{DestroyWindow}
Destroys a window.
\cvdefC{void cvDestroyWindow( const char* name );}
\cvdefPy{DestroyWindow(name)-> None}
\begin{description}
\cvarg{name}{Name of the window to be destroyed.}
\end{description}
The function \texttt{cvDestroyWindow} destroys the window with the given name.
\cvCPyFunc{GetTrackbarPos}
Returns the trackbar position.
\cvdefC{int cvGetTrackbarPos( \par const char* trackbarName, \par const char* windowName );}
\cvdefPy{GetTrackbarPos(trackbarName,windowName)-> None}
\begin{description}
\cvarg{trackbarName}{Name of the trackbar.}
\cvarg{windowName}{Name of the window which is the parent of the trackbar.}
\end{description}
The function \texttt{cvGetTrackbarPos} returns the current position of the specified trackbar.\\\\
\textbf{[Qt Backend Only]} qt-specific details:
\begin{description}
\cvarg{windowName}{Name of the window which is the parent of the trackbar. Can be NULL if the trackbar is attached to the control panel.}
\end{description}
\ifC
\cvCPyFunc{GetWindowHandle}
Gets the window's handle by its name.
\cvdefC{void* cvGetWindowHandle( const char* name );}
\begin{description}
\cvarg{name}{Name of the window}.
\end{description}
The function \texttt{cvGetWindowHandle} returns the native window handle (HWND in case of Win32 and GtkWidget in case of GTK+).\\\\
\textbf{[Qt Backend Only]} qt-specific details:
The function \texttt{cvGetWindowHandle} returns the native window handle inheriting from the Qt class QWidget.
\cvCPyFunc{GetWindowName}
Gets the window's name by its handle.
\cvdefC{const char* cvGetWindowName( void* windowHandle );}
\begin{description}
\cvarg{windowHandle}{Handle of the window.}
\end{description}
The function \texttt{cvGetWindowName} returns the name of the window given its native handle (HWND in case of Win32 and GtkWidget in case of GTK+).\\\\
\textbf{[Qt Backend Only]} qt-specific details:
The function \texttt{cvGetWindowName} returns the name of the window given its native handle (QWidget).
\cvCPyFunc{InitSystem}
Initializes HighGUI.
\cvdefC{int cvInitSystem( int argc, char** argv );}
\begin{description}
\cvarg{argc}{Number of command line arguments}
\cvarg{argv}{Array of command line arguments}
\end{description}
The function \texttt{cvInitSystem} initializes HighGUI. If it wasn't
called explicitly by the user before the first window was created, it is
called implicitly then with \texttt{argc=0}, \texttt{argv=NULL}. Under
Win32 there is no need to call it explicitly. Under X Window the arguments
may be used to customize a look of HighGUI windows and controls.\\\\
\textbf{[Qt Backend Only]} qt-specific details:
The function \texttt{cvInitSystem} is automatically called at the first cvNameWindow call.
\fi
\cvCPyFunc{MoveWindow}
Sets the position of the window.
\cvdefC{void cvMoveWindow( const char* name, int x, int y );}
\cvdefPy{MoveWindow(name,x,y)-> None}
\begin{description}
\cvarg{name}{Name of the window to be moved.}
\cvarg{x}{New x coordinate of the top-left corner}
\cvarg{y}{New y coordinate of the top-left corner}
\end{description}
The function \texttt{cvMoveWindow} changes the position of the window.
\cvCPyFunc{NamedWindow}
Creates a window.
\cvdefC{int cvNamedWindow( const char* name, int flags );}
\cvdefPy{NamedWindow(name,flags=CV\_WINDOW\_AUTOSIZE)-> None}
\begin{description}
\cvarg{name}{Name of the window in the window caption that may be used as a window identifier.}
\cvarg{flags}{Flags of the window. Currently the only supported flag is \texttt{CV\_WINDOW\_AUTOSIZE}. If this is set, window size is automatically adjusted to fit the displayed image (see \cross{ShowImage}), and the user can not change the window size manually.}
\end{description}
The function \texttt{cvNamedWindow} creates a window which can be used as a placeholder for images and trackbars. Created windows are referred to by their names.
If a window with the same name already exists, the function does nothing.\\\\
\textbf{[Qt Backend Only]} qt-specific details:
\begin{description}
\cvarg{flags}{Flags of the window. Currently the supported flags are:
\begin{description}
\cvarg{CV\_WINDOW\_NORMAL or CV\_WINDOW\_AUTOSIZE:}
{ \texttt{CV\_WINDOW\_NORMAL} let the user resize the window, whereas \texttt{CV\_WINDOW\_AUTOSIZE} adjusts automatically the window's size to fit the displayed image (see \cross{ShowImage}), and the user can not change the window size manually.}
\cvarg{CV\_WINDOW\_FREERATIO or CV\_WINDOW\_KEEPRATIO:}
{\texttt{CV\_WINDOW\_FREERATIO} adjust the image without respect the its ration, whereas \texttt{CV\_WINDOW\_KEEPRATIO} keep the image's ratio.}
\cvarg{CV\_GUI\_NORMAL or CV\_GUI\_EXPANDED:}
{ \texttt{CV\_GUI\_NORMAL} is the old way to draw the window without statusbar and toolbar, whereas \texttt{CV\_GUI\_EXPANDED} is the new enhance GUI.}
\end{description}
This parameter is optional. The default flags set for a new window are \texttt{CV\_WINDOW\_AUTOSIZE}, \texttt{CV\_WINDOW\_KEEPRATIO}, and \texttt{CV\_GUI\_EXPANDED}.
However, if you want to modify the flags, you can combine them using OR operator, ie:
\begin{lstlisting}
cvNamedWindow( ``myWindow'', \texttt{CV\_WINDOW\_NORMAL} \textbar \texttt{CV\_GUI\_NORMAL});}
\end{lstlisting}
\end{description}
\cvCPyFunc{ResizeWindow}
Sets the window size.
\cvdefC{void cvResizeWindow( const char* name, int width, int height );}
\cvdefPy{ResizeWindow(name,width,height)-> None}
\begin{description}
\cvarg{name}{Name of the window to be resized.}
\cvarg{width}{New width}
\cvarg{height}{New height}
\end{description}
The function \texttt{cvResizeWindow} changes the size of the window.
\cvCPyFunc{SetMouseCallback}
Assigns callback for mouse events.
\cvdefC{void cvSetMouseCallback( const char* windowName, CvMouseCallback onMouse, void* param=NULL );}
\cvdefPy{SetMouseCallback(windowName, onMouse, param) -> None}
\begin{description}
\cvarg{windowName}{Name of the window.}
\ifC % {
\cvarg{onMouse}{Pointer to the function to be called every time a mouse event occurs in the specified window. This function should be prototyped as
\texttt{void Foo(int event, int x, int y, int flags, void* param);}
where \texttt{event} is one of \texttt{CV\_EVENT\_*}, \texttt{x} and \texttt{y} are the coordinates of the mouse pointer in image coordinates (not window coordinates), \texttt{flags} is a combination of \texttt{CV\_EVENT\_FLAG\_*}, and \texttt{param} is a user-defined parameter passed to the \texttt{cvSetMouseCallback} function call.}
\else % }{
\cvarg{onMouse}{Callable to be called every time a mouse event occurs in the specified window. This callable should have signature
\texttt{ Foo(event, x, y, flags, param)-> None }
where \texttt{event} is one of \texttt{CV\_EVENT\_*}, \texttt{x} and \texttt{y} are the coordinates of the mouse pointer in image coordinates (not window coordinates), \texttt{flags} is a combination of \texttt{CV\_EVENT\_FLAG\_*}, and \texttt{param} is a user-defined parameter passed to the \texttt{cvSetMouseCallback} function call.}
\fi % }
\cvarg{param}{User-defined parameter to be passed to the callback function.}
\end{description}
The function \texttt{cvSetMouseCallback} sets the callback function for mouse events occuring within the specified window.
The \texttt{event} parameter is one of:
\begin{description}
\cvarg{CV\_EVENT\_MOUSEMOVE}{Mouse movement}
\cvarg{CV\_EVENT\_LBUTTONDOWN}{Left button down}
\cvarg{CV\_EVENT\_RBUTTONDOWN}{Right button down}
\cvarg{CV\_EVENT\_MBUTTONDOWN}{Middle button down}
\cvarg{CV\_EVENT\_LBUTTONUP}{Left button up}
\cvarg{CV\_EVENT\_RBUTTONUP}{Right button up}
\cvarg{CV\_EVENT\_MBUTTONUP}{Middle button up}
\cvarg{CV\_EVENT\_LBUTTONDBLCLK}{Left button double click}
\cvarg{CV\_EVENT\_RBUTTONDBLCLK}{Right button double click}
\cvarg{CV\_EVENT\_MBUTTONDBLCLK}{Middle button double click}
\end{description}
The \texttt{flags} parameter is a combination of :
\begin{description}
\cvarg{CV\_EVENT\_FLAG\_LBUTTON}{Left button pressed}
\cvarg{CV\_EVENT\_FLAG\_RBUTTON}{Right button pressed}
\cvarg{CV\_EVENT\_FLAG\_MBUTTON}{Middle button pressed}
\cvarg{CV\_EVENT\_FLAG\_CTRLKEY}{Control key pressed}
\cvarg{CV\_EVENT\_FLAG\_SHIFTKEY}{Shift key pressed}
\cvarg{CV\_EVENT\_FLAG\_ALTKEY}{Alt key pressed}
\end{description}
\cvCPyFunc{SetTrackbarPos}
Sets the trackbar position.
\cvdefC{void cvSetTrackbarPos( \par const char* trackbarName, \par const char* windowName, \par int pos );}
\cvdefPy{SetTrackbarPos(trackbarName,windowName,pos)-> None}
\begin{description}
\cvarg{trackbarName}{Name of the trackbar.}
\cvarg{windowName}{Name of the window which is the parent of trackbar.}
\cvarg{pos}{New position.}
\end{description}
The function \texttt{cvSetTrackbarPos} sets the position of the specified trackbar.\\\\
\textbf{[Qt Backend Only]} qt-specific details:
\begin{description}
\cvarg{windowName}{Name of the window which is the parent of trackbar. Can be NULL if the trackbar is attached to the control panel.}
\end{description}
\cvCPyFunc{ShowImage}
Displays the image in the specified window
\cvdefC{void cvShowImage( const char* name, const CvArr* image );}
\cvdefPy{ShowImage(name,image)-> None}
\begin{description}
\cvarg{name}{Name of the window.}
\cvarg{image}{Image to be shown.}
\end{description}
The function \texttt{cvShowImage} displays the image in the specified window. If the window was created with the \texttt{CV\_WINDOW\_AUTOSIZE} flag then the image is shown with its original size, otherwise the image is scaled to fit in the window. The function may scale the image, depending on its depth:
\begin{itemize}
\item If the image is 8-bit unsigned, it is displayed as is.
\item If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255*256] is mapped to [0,255].
\item If the image is 32-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].
\end{itemize}
\cvCPyFunc{WaitKey}
Waits for a pressed key.
\cvdefC{int cvWaitKey( int delay=0 );}
\cvdefPy{WaitKey(delay=0)-> int}
\begin{description}
\cvarg{delay}{Delay in milliseconds.}
\end{description}
The function \texttt{cvWaitKey} waits for key event infinitely ($ \texttt{delay} <= 0$) or for \texttt{delay} milliseconds. Returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.
\textbf{Note:} This function is the only method in HighGUI that can fetch and handle events, so it needs to be called periodically for normal event processing, unless HighGUI is used within some environment that takes care of event processing.\\\\
\textbf{[Qt Backend Only]} qt-specific details:
With this current Qt implementation, this is the only way to process event such as repaint for the windows, and so on \ldots
\section{Reading and Writing Images and Video}
\cvCPyFunc{LoadImage}
Loads an image from a file as an IplImage.
\cvdefC{
IplImage* cvLoadImage( \par const char* filename, \par int iscolor=CV\_LOAD\_IMAGE\_COLOR );}
\cvdefPy{LoadImage(filename, iscolor=CV\_LOAD\_IMAGE\_COLOR)->None}
\begin{description}
\cvarg{filename}{Name of file to be loaded.}
\cvarg{iscolor}{Specific color type of the loaded image:
\begin{description}
\cvarg{CV\_LOAD\_IMAGE\_COLOR}{the loaded image is forced to be a 3-channel color image}
\cvarg{CV\_LOAD\_IMAGE\_GRAYSCALE}{the loaded image is forced to be grayscale}
\cvarg{CV\_LOAD\_IMAGE\_UNCHANGED}{the loaded image will be loaded as is.}
\end{description}
}
\end{description}
The function \texttt{cvLoadImage} loads an image from the specified file and returns the pointer to the loaded image. Currently the following file formats are supported:
\begin{itemize}
\item Windows bitmaps - BMP, DIB
\item JPEG files - JPEG, JPG, JPE
\item Portable Network Graphics - PNG
\item Portable image format - PBM, PGM, PPM
\item Sun rasters - SR, RAS
\item TIFF files - TIFF, TIF
\end{itemize}
Note that in the current implementation the alpha channel, if any, is stripped from the output image, e.g. 4-channel RGBA image will be loaded as RGB.
\cvCPyFunc{LoadImageM}
Loads an image from a file as a CvMat.
\cvdefC{
CvMat* cvLoadImageM( \par const char* filename, \par int iscolor=CV\_LOAD\_IMAGE\_COLOR );}
\cvdefPy{LoadImageM(filename, iscolor=CV\_LOAD\_IMAGE\_COLOR)->None}
\begin{description}
\cvarg{filename}{Name of file to be loaded.}
\cvarg{iscolor}{Specific color type of the loaded image:
\begin{description}
\cvarg{CV\_LOAD\_IMAGE\_COLOR}{the loaded image is forced to be a 3-channel color image}
\cvarg{CV\_LOAD\_IMAGE\_GRAYSCALE}{the loaded image is forced to be grayscale}
\cvarg{CV\_LOAD\_IMAGE\_UNCHANGED}{the loaded image will be loaded as is.}
\end{description}
}
\end{description}
The function \texttt{cvLoadImageM} loads an image from the specified file and returns the pointer to the loaded image.
urrently the following file formats are supported:
\begin{itemize}
\item Windows bitmaps - BMP, DIB
\item JPEG files - JPEG, JPG, JPE
\item Portable Network Graphics - PNG
\item Portable image format - PBM, PGM, PPM
\item Sun rasters - SR, RAS
\item TIFF files - TIFF, TIF
\end{itemize}
Note that in the current implementation the alpha channel, if any, is stripped from the output image, e.g. 4-channel RGBA image will be loaded as RGB.
\cvCPyFunc{SaveImage}
Saves an image to a specified file.
\cvdefC{int cvSaveImage( const char* filename, const CvArr* image );}
\cvdefPy{SaveImage(filename,image)-> None}
\begin{description}
\cvarg{filename}{Name of the file.}
\cvarg{image}{Image to be saved.}
\end{description}
The function \texttt{cvSaveImage} saves the image to the specified file. The image format is chosen based on the \texttt{filename} extension, see \cross{LoadImage}. Only 8-bit single-channel or 3-channel (with 'BGR' channel order) images can be saved using this function. If the format, depth or channel order is different, use \texttt{cvCvtScale} and \texttt{cvCvtColor} to convert it before saving, or use universal \texttt{cvSave} to save the image to XML or YAML format.
\cvclass{CvCapture}\label{CvCapture}
Video capturing structure.
\cvdefC{typedef struct CvCapture CvCapture;}
The structure \texttt{CvCapture} does not have a public interface and is used only as a parameter for video capturing functions.
\cvCPyFunc{CaptureFromCAM} % XXX:Called cvCreateCameraCapture in manual
Initializes capturing a video from a camera.
\cvdefC{CvCapture* cvCaptureFromCAM( int index );}
\cvdefPy{CaptureFromCAM(index) -> CvCapture}
\begin{description}
\cvarg{index}{Index of the camera to be used. If there is only one camera or it does not matter what camera is used -1 may be passed.}
\end{description}
The function \texttt{cvCaptureFromCAM} allocates and initializes the CvCapture structure for reading a video stream from the camera. Currently two camera interfaces can be used on Windows: Video for Windows (VFW) and Matrox Imaging Library (MIL); and two on Linux: V4L and FireWire (IEEE1394).
To release the structure, use \cross{ReleaseCapture}.
\cvCPyFunc{CaptureFromFile} % XXX:Called cvCreateFileCapture in manual
Initializes capturing a video from a file.
\cvdefC{CvCapture* cvCaptureFromFile( const char* filename );}
\cvdefPy{CaptureFromFile(filename) -> CvCapture}
\begin{description}
\cvarg{filename}{Name of the video file.}
\end{description}
The function \texttt{cvCaptureFromFile} allocates and initializes the CvCapture structure for reading the video stream from the specified file. Which codecs and file formats are supported depends on the back end library. On Windows HighGui uses Video for Windows (VfW), on Linux ffmpeg is used and on Mac OS X the back end is QuickTime. See VideoCodecs for some discussion on what to expect and how to prepare your video files.
After the allocated structure is not used any more it should be released by the \cross{ReleaseCapture} function.
\cvCPyFunc{GetCaptureProperty}
Gets video capturing properties.
\cvdefC{double cvGetCaptureProperty( CvCapture* capture, int property\_id );}
\cvdefPy{GetCaptureProperty(capture, property\_id)->double}
% when updating the list below, remember to update it also in the cvSetCaptureProperty, cv::VideoCapture::get/set() functions docs
\begin{description}
\cvarg{capture}{video capturing structure.}
\cvarg{property\_id}{Property identifier. Can be one of the following:}
\begin{description}
\cvarg{CV\_CAP\_PROP\_POS\_MSEC}{Film current position in milliseconds or video capture timestamp}
\cvarg{CV\_CAP\_PROP\_POS\_FRAMES}{0-based index of the frame to be decoded/captured next}
\cvarg{CV\_CAP\_PROP\_POS\_AVI\_RATIO}{Relative position of the video file (0 - start of the film, 1 - end of the film)}
\cvarg{CV\_CAP\_PROP\_FRAME\_WIDTH}{Width of the frames in the video stream}
\cvarg{CV\_CAP\_PROP\_FRAME\_HEIGHT}{Height of the frames in the video stream}
\cvarg{CV\_CAP\_PROP\_FPS}{Frame rate}
\cvarg{CV\_CAP\_PROP\_FOURCC}{4-character code of codec}
\cvarg{CV\_CAP\_PROP\_FRAME\_COUNT}{Number of frames in the video file}
\cvarg{CV\_CAP\_PROP\_FORMAT}{The format of the Mat objects returned by retrieve()}
\cvarg{CV\_CAP\_PROP\_MODE}{A backend-specific value indicating the current capture mode}
\cvarg{CV\_CAP\_PROP\_BRIGHTNESS}{Brightness of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_CONTRAST}{Contrast of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_SATURATION}{Saturation of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_HUE}{Hue of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_GAIN}{Gain of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_EXPOSURE}{Exposure (only for cameras)}
\cvarg{CV\_CAP\_PROP\_CONVERT\_RGB}{Boolean flags indicating whether images should be converted to RGB}
\cvarg{CV\_CAP\_PROP\_WHITE\_BALANCE}{Currently unsupported}
\cvarg{CV\_CAP\_PROP\_RECTIFICATION}{TOWRITE (note: only supported by DC1394 v 2.x backend currently)}
\end{description}
\end{description}
The function \texttt{cvGetCaptureProperty} retrieves the specified property of the camera or video file.
\cvCPyFunc{GrabFrame}
Grabs the frame from a camera or file.
\cvdefC{int cvGrabFrame( CvCapture* capture );}
\cvdefPy{GrabFrame(capture) -> int}
\begin{description}
\cvarg{capture}{video capturing structure.}
\end{description}
The function \texttt{cvGrabFrame} grabs the frame from a camera or file. The grabbed frame is stored internally. The purpose of this function is to grab the frame \emph{quickly} so that syncronization can occur if it has to read from several cameras simultaneously. The grabbed frames are not exposed because they may be stored in a compressed format (as defined by the camera/driver). To retrieve the grabbed frame, \cross{RetrieveFrame} should be used.
\cvCPyFunc{QueryFrame}
Grabs and returns a frame from a camera or file.
\cvdefC{IplImage* cvQueryFrame( CvCapture* capture );}
\cvdefPy{QueryFrame(capture) -> iplimage}
\begin{description}
\cvarg{capture}{video capturing structure.}
\end{description}
The function \texttt{cvQueryFrame} grabs a frame from a camera or video file, decompresses it and returns it. This function is just a combination of \cross{GrabFrame} and \cross{RetrieveFrame}, but in one call. The returned image should not be released or modified by the user. In the event of an error, the return value may be NULL.
\ifC
\cvCPyFunc{ReleaseCapture}
Releases the CvCapture structure.
\cvdefC{void cvReleaseCapture( CvCapture** capture );}
\begin{description}
\cvarg{capture}{Pointer to video the capturing structure.}
\end{description}
The function \texttt{cvReleaseCapture} releases the CvCapture structure allocated by \cross{CaptureFromFile} or \cross{CaptureFromCAM}.
\fi
\cvCPyFunc{RetrieveFrame} % XXX:Different than manual
Gets the image grabbed with cvGrabFrame.
\cvdefC{IplImage* cvRetrieveFrame( CvCapture* capture );}
\cvdefPy{RetrieveFrame(capture) -> iplimage}
\begin{description}
\cvarg{capture}{video capturing structure.}
\end{description}
The function \texttt{cvRetrieveFrame} returns the pointer to the image grabbed with the \cross{GrabFrame} function. The returned image should not be released or modified by the user. In the event of an error, the return value may be NULL.
\cvCPyFunc{SetCaptureProperty}
Sets video capturing properties.
\cvdefC{int cvSetCaptureProperty( \par CvCapture* capture, \par int property\_id, \par double value );}
\cvdefPy{SetCaptureProperty(capture, property\_id,value)->None}
% when updating the list below, remember to update it also in the cvGetCaptureProperty, cv::VideoCapture::get/set() functions docs
\begin{description}
\cvarg{capture}{video capturing structure.}
\cvarg{property\_id}{property identifier. Can be one of the following:}
\begin{description}
\cvarg{CV\_CAP\_PROP\_POS\_MSEC}{Film current position in milliseconds or video capture timestamp}
\cvarg{CV\_CAP\_PROP\_POS\_FRAMES}{0-based index of the frame to be decoded/captured next}
\cvarg{CV\_CAP\_PROP\_POS\_AVI\_RATIO}{Relative position of the video file (0 - start of the film, 1 - end of the film)}
\cvarg{CV\_CAP\_PROP\_FRAME\_WIDTH}{Width of the frames in the video stream}
\cvarg{CV\_CAP\_PROP\_FRAME\_HEIGHT}{Height of the frames in the video stream}
\cvarg{CV\_CAP\_PROP\_FPS}{Frame rate}
\cvarg{CV\_CAP\_PROP\_FOURCC}{4-character code of codec}
\cvarg{CV\_CAP\_PROP\_FRAME\_COUNT}{Number of frames in the video file}
\cvarg{CV\_CAP\_PROP\_FORMAT}{The format of the Mat objects returned by retrieve()}
\cvarg{CV\_CAP\_PROP\_MODE}{A backend-specific value indicating the current capture mode}
\cvarg{CV\_CAP\_PROP\_BRIGHTNESS}{Brightness of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_CONTRAST}{Contrast of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_SATURATION}{Saturation of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_HUE}{Hue of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_GAIN}{Gain of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_EXPOSURE}{Exposure (only for cameras)}
\cvarg{CV\_CAP\_PROP\_CONVERT\_RGB}{Boolean flags indicating whether images should be converted to RGB}
\cvarg{CV\_CAP\_PROP\_WHITE\_BALANCE}{Currently unsupported}
\cvarg{CV\_CAP\_PROP\_RECTIFICATION}{TOWRITE (note: only supported by DC1394 v 2.x backend currently)}
\end{description}
\cvarg{value}{value of the property.}
\end{description}
The function \texttt{cvSetCaptureProperty} sets the specified property of video capturing. Currently the function supports only video files: \texttt{CV\_CAP\_PROP\_POS\_MSEC, CV\_CAP\_PROP\_POS\_FRAMES, CV\_CAP\_PROP\_POS\_AVI\_RATIO}.
NB This function currently does nothing when using the latest CVS download on linux with FFMPEG (the function contents are hidden if 0 is used and returned).
\cvCPyFunc{CreateVideoWriter} % XXX Different than manual
Creates the video file writer.
\cvdefC{
typedef struct CvVideoWriter CvVideoWriter;
CvVideoWriter* cvCreateVideoWriter( \par const char* filename, \par int fourcc, \par double fps, \par CvSize frame\_size, \par int is\_color=1 );
}\cvdefPy{CreateVideoWriter(filename, fourcc, fps, frame\_size, is\_color) -> CvVideoWriter}
\begin{description}
\cvarg{filename}{Name of the output video file.}
\cvarg{fourcc}{4-character code of codec used to compress the frames. For example,
\texttt{CV\_FOURCC('P','I','M,'1')} is a MPEG-1 codec,
\texttt{CV\_FOURCC('M','J','P','G')} is a motion-jpeg codec etc.
Under Win32 it is possible to pass -1 in order to choose compression method and additional compression parameters from dialog. Under Win32 if 0 is passed while using an avi filename it will create a video writer that creates an uncompressed avi file.}
\cvarg{fps}{Framerate of the created video stream.}
\cvarg{frame\_size}{Size of the video frames.}
\cvarg{is\_color}{If it is not zero, the encoder will expect and encode color frames, otherwise it will work with grayscale frames (the flag is currently supported on Windows only).}
\end{description}
The function \texttt{cvCreateVideoWriter} creates the video writer structure.
Which codecs and file formats are supported depends on the back end library. On Windows HighGui uses Video for Windows (VfW), on Linux ffmpeg is used and on Mac OS X the back end is QuickTime. See VideoCodecs for some discussion on what to expect.
\ifC
\cvCPyFunc{ReleaseVideoWriter}
Releases the AVI writer.
\cvdefC{void cvReleaseVideoWriter( CvVideoWriter** writer );}
\begin{description}
\cvarg{writer}{Pointer to the video file writer structure.}
\end{description}
The function \texttt{cvReleaseVideoWriter} finishes writing to the video file and releases the structure.
\fi
\cvCPyFunc{WriteFrame}
Writes a frame to a video file.
\cvdefC{int cvWriteFrame( CvVideoWriter* writer, const IplImage* image );}
\cvdefPy{WriteFrame(writer, image)->int}
\begin{description}
\cvarg{writer}{Video writer structure}
\cvarg{image}{The written frame}
\end{description}
The function \texttt{cvWriteFrame} writes/appends one frame to a video file.
\fi
\ifCpp
\section{User Interface}
\cvCppFunc{createTrackbar}
Creates a trackbar and attaches it to the specified window
\cvdefCpp{int createTrackbar( const string\& trackbarname,\par
const string\& winname,\par
int* value, int count,\par
TrackbarCallback onChange CV\_DEFAULT(0),\par
void* userdata CV\_DEFAULT(0));}
\begin{description}
\cvarg{trackbarname}{Name of the created trackbar.}
\cvarg{winname}{Name of the window which will be used as a parent of the created trackbar.}
\cvarg{value}{The optional pointer to an integer variable, whose value will reflect the position of the slider. Upon creation, the slider position is defined by this variable.}
\cvarg{count}{The maximal position of the slider. The minimal position is always 0.}
\cvarg{onChange}{Pointer to the function to be called every time the slider changes position. This function should be prototyped as \texttt{void Foo(int,void*);}, where the first parameter is the trackbar position and the second parameter is the user data (see the next parameter). If the callback is NULL pointer, then no callbacks is called, but only \texttt{value} is updated}
\cvarg{userdata}{The user data that is passed as-is to the callback; it can be used to handle trackbar events without using global variables}
\end{description}
The function \texttt{createTrackbar} creates a trackbar (a.k.a. slider or range control) with the specified name and range, assigns a variable \texttt{value} to be syncronized with trackbar position and specifies a callback function \texttt{onChange} to be called on the trackbar position change. The created trackbar is displayed on the top of the given window.\\\\
\textbf{[Qt Backend Only]} qt-specific details:
\begin{description}
\cvarg{winname}{Name of the window which will be used as a parent for created trackbar. Can be NULL if the trackbar should be attached to the control panel.}
\end{description}
The created trackbar is displayed at the bottom of the given window if \emph{winname} is correctly provided, or displayed on the control panel if \emph{winname} is NULL.
By clicking on the label of each trackbar, it is possible to edit the trackbar's value manually for a more accurate control of it.
\cvCppFunc{getTrackbarPos}
Returns the trackbar position.
\cvdefCpp{int getTrackbarPos( const string\& trackbarname, \par const string\& winname );}
\begin{description}
\cvarg{trackbarname}{Name of the trackbar.}
\cvarg{winname}{Name of the window which is the parent of the trackbar.}
\end{description}
The function returns the current position of the specified trackbar.\\\\
\textbf{[Qt Backend Only]} qt-specific details:
\begin{description}
\cvarg{winname}{Name of the window which is the parent of the trackbar. Can be NULL if the trackbar is attached to the control panel.}
\end{description}
\cvCppFunc{imshow}
Displays the image in the specified window
\cvdefCpp{void imshow( const string\& winname, \par const Mat\& image );}
\begin{description}
\cvarg{winname}{Name of the window.}
\cvarg{image}{Image to be shown.}
\end{description}
The function \texttt{imshow} displays the image in the specified window. If the window was created with the \texttt{CV\_WINDOW\_AUTOSIZE} flag then the image is shown with its original size, otherwise the image is scaled to fit in the window. The function may scale the image, depending on its depth:
\begin{itemize}
\item If the image is 8-bit unsigned, it is displayed as is.
\item If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255*256] is mapped to [0,255].
\item If the image is 32-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].
\end{itemize}
\cvCppFunc{namedWindow}
Creates a window.
\cvdefCpp{void namedWindow( const string\& winname, \par int flags );}
\begin{description}
\cvarg{name}{Name of the window in the window caption that may be used as a window identifier.}
\cvarg{flags}{Flags of the window. Currently the only supported flag is \texttt{CV\_WINDOW\_AUTOSIZE}. If this is set, the window size is automatically adjusted to fit the displayed image (see \cross{imshow}), and the user can not change the window size manually.}
\end{description}
The function \texttt{namedWindow} creates a window which can be used as a placeholder for images and trackbars. Created windows are referred to by their names.
If a window with the same name already exists, the function does nothing.\\\\
\textbf{[Qt Backend Only]} qt-specific details:
\begin{description}
\cvarg{flags}{Flags of the window. Currently the supported flags are:
\begin{description}
\cvarg{CV\_WINDOW\_NORMAL or CV\_WINDOW\_AUTOSIZE:}
{ \texttt{CV\_WINDOW\_NORMAL} let the user resize the window, whereas \texttt{CV\_WINDOW\_AUTOSIZE} adjusts automatically the window's size to fit the displayed image (see \cross{ShowImage}), and the user can not change the window size manually.}
\cvarg{CV\_WINDOW\_FREERATIO or CV\_WINDOW\_KEEPRATIO:}
{\texttt{CV\_WINDOW\_FREERATIO} adjust the image without respect the its ration, whereas \texttt{CV\_WINDOW\_KEEPRATIO} keep the image's ratio.}
\cvarg{CV\_GUI\_NORMAL or CV\_GUI\_EXPANDED:}
{ \texttt{CV\_GUI\_NORMAL} is the old way to draw the window without statusbar and toolbar, whereas \texttt{CV\_GUI\_EXPANDED} is the new enhance GUI.}
\end{description}
This parameter is optional. The default flags set for a new window are \texttt{CV\_WINDOW\_AUTOSIZE}, \texttt{CV\_WINDOW\_KEEPRATIO}, and \texttt{CV\_GUI\_EXPANDED}.
However, if you want to modify the flags, you can combine them using OR operator, ie:
\begin{lstlisting}
namedWindow( ``myWindow'', \texttt{CV\_WINDOW\_NORMAL} \textbar \texttt{CV\_GUI\_NORMAL});}
\end{lstlisting}
\end{description}
\cvCppFunc{setTrackbarPos}
Sets the trackbar position.
\cvdefCpp{void setTrackbarPos( const string\& trackbarname, \par const string\& winname, int pos );}
\begin{description}
\cvarg{trackbarname}{Name of the trackbar.}
\cvarg{winname}{Name of the window which is the parent of trackbar.}
\cvarg{pos}{The new position.}
\end{description}
The function sets the position of the specified trackbar in the specified window.\\\\
\textbf{[Qt Backend Only]} qt-specific details:
\begin{description}
\cvarg{winname}{Name of the window which is the parent of trackbar. Can be NULL if the trackbar is attached to the control panel.}
\end{description}
\cvCppFunc{waitKey}
Waits for a pressed key.
\cvdefCpp{int waitKey(int delay=0);}
\begin{description}
\cvarg{delay}{Delay in milliseconds. 0 is the special value that means "forever"}
\end{description}
The function \texttt{waitKey} waits for key event infinitely (when $\texttt{delay}\leq 0$) or for \texttt{delay} milliseconds, when it's positive. Returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.
\textbf{Note:} This function is the only method in HighGUI that can fetch and handle events, so it needs to be called periodically for normal event processing, unless HighGUI is used within some environment that takes care of event processing.
\textbf{Note 2:} The function only works if there is at least one HighGUI window created and the window is active. If there are several HighGUI windows, any of them can be active.
\section{Reading and Writing Images and Video}
\cvCppFunc{imdecode}
Reads an image from a buffer in memory.
\cvdefCpp{Mat imdecode( const Mat\& buf, \par int flags );}
\begin{description}
\cvarg{buf}{The input array of vector of bytes}
\cvarg{flags}{The same flags as in \cross{imread}}
\end{description}
The function reads image from the specified buffer in memory.
If the buffer is too short or contains invalid data, the empty matrix will be returned.
See \cross{imread} for the list of supported formats and the flags description.
\cvCppFunc{imencode}
Encode an image into a memory buffer.
\cvdefCpp{bool imencode( const string\& ext,\par
const Mat\& img,\par
vector<uchar>\& buf,\par
const vector<int>\& params=vector<int>());}
\begin{description}
\cvarg{ext}{The file extension that defines the output format}
\cvarg{img}{The image to be written}
\cvarg{buf}{The output buffer; resized to fit the compressed image}
\cvarg{params}{The format-specific parameters; see \cross{imwrite}}
\end{description}
The function compresses the image and stores it in the memory buffer, which is resized to fit the result.
See \cross{imwrite} for the list of supported formats and the flags description.
\cvCppFunc{imread}
Loads an image from a file.
\cvdefCpp{Mat imread( const string\& filename, \par int flags=1 );}
\begin{description}
\cvarg{filename}{Name of file to be loaded.}
\cvarg{flags}{Specifies color type of the loaded image:}
\begin{description}
\cvarg{>0}{the loaded image is forced to be a 3-channel color image}
\cvarg{=0}{the loaded image is forced to be grayscale}
\cvarg{<0}{the loaded image will be loaded as-is (note that in the current implementation the alpha channel, if any, is stripped from the output image, e.g. 4-channel RGBA image will be loaded as RGB if $flags\ge0$).}
\end{description}
\end{description}
The function \texttt{imread} loads an image from the specified file and returns it. If the image can not be read (because of missing file, improper permissions, unsupported or invalid format), the function returns empty matrix (\texttt{Mat::data==NULL}).Currently, the following file formats are supported:
\begin{itemize}
\item Windows bitmaps - \texttt{*.bmp, *.dib} (always supported)
\item JPEG files - \texttt{*.jpeg, *.jpg, *.jpe} (see \textbf{Note2})
\item JPEG 2000 files - \texttt{*.jp2} (see \textbf{Note2})
\item Portable Network Graphics - \texttt{*.png} (see \textbf{Note2})
\item Portable image format - \texttt{*.pbm, *.pgm, *.ppm} (always supported)
\item Sun rasters - \texttt{*.sr, *.ras} (always supported)
\item TIFF files - \texttt{*.tiff, *.tif} (see \textbf{Note2})
\end{itemize}
\textbf{Note1}: The function determines type of the image by the content, not by the file extension.
\textbf{Note2}: On Windows and MacOSX the shipped with OpenCV image codecs (libjpeg, libpng, libtiff and libjasper) are used by default; so OpenCV can always read JPEGs, PNGs and TIFFs. On MacOSX there is also the option to use native MacOSX image readers. But beware that currently these native image loaders give images with somewhat different pixel values, because of the embedded into MacOSX color management.
On Linux, BSD flavors and other Unix-like open-source operating systems OpenCV looks for the supplied with OS image codecs. Please, install the relevant packages (do not forget the development files, e.g. "libjpeg-dev" etc. in Debian and Ubuntu) in order to get the codec support, or turn on \texttt{OPENCV\_BUILD\_3RDPARTY\_LIBS} flag in CMake.
\cvCppFunc{imwrite}
Saves an image to a specified file.
\cvdefCpp{bool imwrite( const string\& filename, \par const Mat\& img,\par
const vector<int>\& params=vector<int>());}
\begin{description}
\cvarg{filename}{Name of the file.}
\cvarg{img}{The image to be saved.}
\cvarg{params}{The format-specific save parameters, encoded as pairs \texttt{paramId\_1, paramValue\_1, paramId\_2, paramValue\_2, ...}. The following parameters are currently supported:
\begin{itemize}
\item In the case of JPEG it can be a quality (\texttt{CV\_IMWRITE\_JPEG\_QUALITY}), from 0 to 100 (the higher is the better), 95 by default.
\item In the case of PNG it can be the compression level (\texttt{CV\_IMWRITE\_PNG\_COMPRESSION}), from 0 to 9 (the higher value means smaller size and longer compression time), 3 by default.
\item In the case of PPM, PGM or PBM it can a binary format flag (\texttt{CV\_IMWRITE\_PXM\_BINARY}), 0 or 1, 1 by default.
\end{itemize}
}
\end{description}
The function \texttt{imwrite} saves the image to the specified file. The image format is chosen based on the \texttt{filename} extension, see \cross{imread} for the list of extensions. Only 8-bit (or 16-bit in the case of PNG, JPEG 2000 and TIFF) single-channel or 3-channel (with 'BGR' channel order) images can be saved using this function. If the format, depth or channel order is different, use \cross{Mat::convertTo}, and \cross{cvtColor} to convert it before saving, or use the universal XML I/O functions to save the image to XML or YAML format.
\cvclass{VideoCapture}
Class for video capturing from video files or cameras
\begin{lstlisting}
class VideoCapture
{
public:
// the default constructor
VideoCapture();
// the constructor that opens video file
VideoCapture(const string& filename);
// the constructor that starts streaming from the camera
VideoCapture(int device);
// the destructor
virtual ~VideoCapture();
// opens the specified video file
virtual bool open(const string& filename);
// starts streaming from the specified camera by its id
virtual bool open(int device);
// returns true if the file was open successfully or if the camera
// has been initialized succesfully
virtual bool isOpened() const;
// closes the camera stream or the video file
// (automatically called by the destructor)
virtual void release();
// grab the next frame or a set of frames from a multi-head camera;
// returns false if there are no more frames
virtual bool grab();
// reads the frame from the specified video stream
// (non-zero channel is only valid for multi-head camera live streams)
virtual bool retrieve(Mat& image, int channel=0);
// equivalent to grab() + retrieve(image, 0);
virtual VideoCapture& operator >> (Mat& image);
// sets the specified property propId to the specified value
virtual bool set(int propId, double value);
// retrieves value of the specified property
virtual double get(int propId);
protected:
...
};
\end{lstlisting}
The class provides C++ video capturing API. Here is how the class can be used:
\begin{lstlisting}
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
\end{lstlisting}
\cvCppFunc{VideoCapture::VideoCapture}
\cvdefCpp{
VideoCapture::VideoCapture();\newline
VideoCapture::VideoCapture(const string\& filename);\newline
VideoCapture::VideoCapture(int device);
}
\begin{description}
\cvarg{filename}{TOWRITE}
\cvarg{device}{TOWRITE}
\end{description}
VideoCapture constructors.
\cvCppFunc{VideoCapture::get}
\cvdefCpp{double VideoCapture::get(int property\_id);}
% when updating the list below, remember to update it also in the cvGet/SetCaptureProperty, cv::VideoCapture::set() functions docs
\begin{description}
\cvarg{property\_id}{Property identifier. Can be one of the following:}
\begin{description}
\cvarg{CV\_CAP\_PROP\_POS\_MSEC}{Film current position in milliseconds or video capture timestamp}
\cvarg{CV\_CAP\_PROP\_POS\_FRAMES}{0-based index of the frame to be decoded/captured next}
\cvarg{CV\_CAP\_PROP\_POS\_AVI\_RATIO}{Relative position of the video file (0 - start of the film, 1 - end of the film)}
\cvarg{CV\_CAP\_PROP\_FRAME\_WIDTH}{Width of the frames in the video stream}
\cvarg{CV\_CAP\_PROP\_FRAME\_HEIGHT}{Height of the frames in the video stream}
\cvarg{CV\_CAP\_PROP\_FPS}{Frame rate}
\cvarg{CV\_CAP\_PROP\_FOURCC}{4-character code of codec}
\cvarg{CV\_CAP\_PROP\_FRAME\_COUNT}{Number of frames in the video file}
\cvarg{CV\_CAP\_PROP\_FORMAT}{The format of the Mat objects returned by retrieve()}
\cvarg{CV\_CAP\_PROP\_MODE}{A backend-specific value indicating the current capture mode}
\cvarg{CV\_CAP\_PROP\_BRIGHTNESS}{Brightness of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_CONTRAST}{Contrast of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_SATURATION}{Saturation of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_HUE}{Hue of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_GAIN}{Gain of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_EXPOSURE}{Exposure (only for cameras)}
\cvarg{CV\_CAP\_PROP\_CONVERT\_RGB}{Boolean flags indicating whether images should be converted to RGB}
\cvarg{CV\_CAP\_PROP\_WHITE\_BALANCE}{Currently unsupported}
\cvarg{CV\_CAP\_PROP\_RECTIFICATION}{TOWRITE (note: only supported by DC1394 v 2.x backend currently)}
\end{description}
\end{description}
Note that when querying a property which is unsupported by the backend used by the VideoCapture class, the value 0 is returned.
\cvCppFunc{VideoCapture::set}
\cvdefCpp{bool VideoCapture::set(int property\_id, double value);}
% when updating the list below, remember to update it also in the cvGet/SetCaptureProperty, cv::VideoCapture::get() functions docs
\begin{description}
\cvarg{property\_id}{Property identifier. Can be one of the following:}
\begin{description}
\cvarg{CV\_CAP\_PROP\_POS\_MSEC}{Film current position in milliseconds or video capture timestamp}
\cvarg{CV\_CAP\_PROP\_POS\_FRAMES}{0-based index of the frame to be decoded/captured next}
\cvarg{CV\_CAP\_PROP\_POS\_AVI\_RATIO}{Relative position of the video file (0 - start of the film, 1 - end of the film)}
\cvarg{CV\_CAP\_PROP\_FRAME\_WIDTH}{Width of the frames in the video stream}
\cvarg{CV\_CAP\_PROP\_FRAME\_HEIGHT}{Height of the frames in the video stream}
\cvarg{CV\_CAP\_PROP\_FPS}{Frame rate}
\cvarg{CV\_CAP\_PROP\_FOURCC}{4-character code of codec}
\cvarg{CV\_CAP\_PROP\_FRAME\_COUNT}{Number of frames in the video file}
\cvarg{CV\_CAP\_PROP\_FORMAT}{The format of the Mat objects returned by retrieve()}
\cvarg{CV\_CAP\_PROP\_MODE}{A backend-specific value indicating the current capture mode}
\cvarg{CV\_CAP\_PROP\_BRIGHTNESS}{Brightness of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_CONTRAST}{Contrast of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_SATURATION}{Saturation of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_HUE}{Hue of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_GAIN}{Gain of the image (only for cameras)}
\cvarg{CV\_CAP\_PROP\_EXPOSURE}{Exposure (only for cameras)}
\cvarg{CV\_CAP\_PROP\_CONVERT\_RGB}{Boolean flags indicating whether images should be converted to RGB}
\cvarg{CV\_CAP\_PROP\_WHITE\_BALANCE}{Currently unsupported}
\cvarg{CV\_CAP\_PROP\_RECTIFICATION}{TOWRITE (note: only supported by DC1394 v 2.x backend currently)}
\end{description}
\cvarg{value}{value of the property.}
\end{description}
Sets a property in the VideoCapture backend.
\cvclass{VideoWriter}
Video writer class
\begin{lstlisting}
class VideoWriter
{
public:
// default constructor
VideoWriter();
// constructor that calls open
VideoWriter(const string& filename, int fourcc,
double fps, Size frameSize, bool isColor=true);
// the destructor
virtual ~VideoWriter();
// opens the file and initializes the video writer.
// filename - the output file name.
// fourcc - the codec
// fps - the number of frames per second
// frameSize - the video frame size
// isColor - specifies whether the video stream is color or grayscale
virtual bool open(const string& filename, int fourcc,
double fps, Size frameSize, bool isColor=true);
// returns true if the writer has been initialized successfully
virtual bool isOpened() const;
// writes the next video frame to the stream
virtual VideoWriter& operator << (const Mat& image);
protected:
...
};
\end{lstlisting}
\fi