PageRenderTime 75ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/opencv1/py/highgui_reading_and_writing_images_and_video.rst

https://github.com/ethanrublee/libopencv-debian
ReStructuredText | 679 lines | 268 code | 411 blank | 0 comment | 0 complexity | 73f83debe12d3dcf0c1bf794fdfd2926 MD5 | raw file
  1. Reading and Writing Images and Video
  2. ====================================
  3. .. highlight:: python
  4. .. index:: LoadImage
  5. .. _LoadImage:
  6. LoadImage
  7. ---------
  8. .. function:: LoadImage(filename, iscolor=CV_LOAD_IMAGE_COLOR)->None
  9. Loads an image from a file as an IplImage.
  10. :param filename: Name of file to be loaded.
  11. :type filename: str
  12. :param iscolor: Specific color type of the loaded image:
  13. * **CV_LOAD_IMAGE_COLOR** the loaded image is forced to be a 3-channel color image
  14. * **CV_LOAD_IMAGE_GRAYSCALE** the loaded image is forced to be grayscale
  15. * **CV_LOAD_IMAGE_UNCHANGED** the loaded image will be loaded as is.
  16. :type iscolor: int
  17. The function
  18. ``cvLoadImage``
  19. loads an image from the specified file and returns the pointer to the loaded image. Currently the following file formats are supported:
  20. *
  21. Windows bitmaps - BMP, DIB
  22. *
  23. JPEG files - JPEG, JPG, JPE
  24. *
  25. Portable Network Graphics - PNG
  26. *
  27. Portable image format - PBM, PGM, PPM
  28. *
  29. Sun rasters - SR, RAS
  30. *
  31. TIFF files - TIFF, TIF
  32. 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.
  33. .. index:: LoadImageM
  34. .. _LoadImageM:
  35. LoadImageM
  36. ----------
  37. .. function:: LoadImageM(filename, iscolor=CV_LOAD_IMAGE_COLOR)->None
  38. Loads an image from a file as a CvMat.
  39. :param filename: Name of file to be loaded.
  40. :type filename: str
  41. :param iscolor: Specific color type of the loaded image:
  42. * **CV_LOAD_IMAGE_COLOR** the loaded image is forced to be a 3-channel color image
  43. * **CV_LOAD_IMAGE_GRAYSCALE** the loaded image is forced to be grayscale
  44. * **CV_LOAD_IMAGE_UNCHANGED** the loaded image will be loaded as is.
  45. :type iscolor: int
  46. The function
  47. ``cvLoadImageM``
  48. loads an image from the specified file and returns the pointer to the loaded image.
  49. urrently the following file formats are supported:
  50. *
  51. Windows bitmaps - BMP, DIB
  52. *
  53. JPEG files - JPEG, JPG, JPE
  54. *
  55. Portable Network Graphics - PNG
  56. *
  57. Portable image format - PBM, PGM, PPM
  58. *
  59. Sun rasters - SR, RAS
  60. *
  61. TIFF files - TIFF, TIF
  62. 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.
  63. .. index:: SaveImage
  64. .. _SaveImage:
  65. SaveImage
  66. ---------
  67. .. function:: SaveImage(filename,image)-> None
  68. Saves an image to a specified file.
  69. :param filename: Name of the file.
  70. :type filename: str
  71. :param image: Image to be saved.
  72. :type image: :class:`CvArr`
  73. The function
  74. ``cvSaveImage``
  75. saves the image to the specified file. The image format is chosen based on the
  76. ``filename``
  77. extension, see
  78. :ref:`LoadImage`
  79. . 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
  80. ``cvCvtScale``
  81. and
  82. ``cvCvtColor``
  83. to convert it before saving, or use universal
  84. ``cvSave``
  85. to save the image to XML or YAML format.
  86. .. index:: CvCapture
  87. .. _CvCapture:
  88. CvCapture
  89. ---------
  90. .. class:: CvCapture
  91. Video capturing structure.
  92. The structure
  93. ``CvCapture``
  94. does not have a public interface and is used only as a parameter for video capturing functions.
  95. .. index:: CaptureFromCAM
  96. .. _CaptureFromCAM:
  97. CaptureFromCAM
  98. --------------
  99. .. function:: CaptureFromCAM(index) -> CvCapture
  100. Initializes capturing a video from a camera.
  101. :param 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.
  102. :type index: int
  103. The function
  104. ``cvCaptureFromCAM``
  105. 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).
  106. To release the structure, use
  107. :ref:`ReleaseCapture`
  108. .
  109. .. index:: CaptureFromFile
  110. .. _CaptureFromFile:
  111. CaptureFromFile
  112. ---------------
  113. .. function:: CaptureFromFile(filename) -> CvCapture
  114. Initializes capturing a video from a file.
  115. :param filename: Name of the video file.
  116. :type filename: str
  117. The function
  118. ``cvCaptureFromFile``
  119. 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.
  120. After the allocated structure is not used any more it should be released by the
  121. :ref:`ReleaseCapture`
  122. function.
  123. .. index:: GetCaptureProperty
  124. .. _GetCaptureProperty:
  125. GetCaptureProperty
  126. ------------------
  127. .. function:: GetCaptureProperty(capture, property_id)->double
  128. Gets video capturing properties.
  129. :param capture: video capturing structure.
  130. :type capture: :class:`CvCapture`
  131. :param property_id: Property identifier. Can be one of the following:
  132. :type property_id: int
  133. * **CV_CAP_PROP_POS_MSEC** Film current position in milliseconds or video capture timestamp
  134. * **CV_CAP_PROP_POS_FRAMES** 0-based index of the frame to be decoded/captured next
  135. * **CV_CAP_PROP_POS_AVI_RATIO** Relative position of the video file (0 - start of the film, 1 - end of the film)
  136. * **CV_CAP_PROP_FRAME_WIDTH** Width of the frames in the video stream
  137. * **CV_CAP_PROP_FRAME_HEIGHT** Height of the frames in the video stream
  138. * **CV_CAP_PROP_FPS** Frame rate
  139. * **CV_CAP_PROP_FOURCC** 4-character code of codec
  140. * **CV_CAP_PROP_FRAME_COUNT** Number of frames in the video file
  141. * **CV_CAP_PROP_FORMAT** The format of the Mat objects returned by retrieve()
  142. * **CV_CAP_PROP_MODE** A backend-specific value indicating the current capture mode
  143. * **CV_CAP_PROP_BRIGHTNESS** Brightness of the image (only for cameras)
  144. * **CV_CAP_PROP_CONTRAST** Contrast of the image (only for cameras)
  145. * **CV_CAP_PROP_SATURATION** Saturation of the image (only for cameras)
  146. * **CV_CAP_PROP_HUE** Hue of the image (only for cameras)
  147. * **CV_CAP_PROP_GAIN** Gain of the image (only for cameras)
  148. * **CV_CAP_PROP_EXPOSURE** Exposure (only for cameras)
  149. * **CV_CAP_PROP_CONVERT_RGB** Boolean flags indicating whether images should be converted to RGB
  150. * **CV_CAP_PROP_WHITE_BALANCE** Currently unsupported
  151. * **CV_CAP_PROP_RECTIFICATION** TOWRITE (note: only supported by DC1394 v 2.x backend currently)
  152. The function
  153. ``cvGetCaptureProperty``
  154. retrieves the specified property of the camera or video file.
  155. .. index:: GrabFrame
  156. .. _GrabFrame:
  157. GrabFrame
  158. ---------
  159. .. function:: GrabFrame(capture) -> int
  160. Grabs the frame from a camera or file.
  161. :param capture: video capturing structure.
  162. :type capture: :class:`CvCapture`
  163. The function
  164. ``cvGrabFrame``
  165. grabs the frame from a camera or file. The grabbed frame is stored internally. The purpose of this function is to grab the frame
  166. *quickly*
  167. 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,
  168. :ref:`RetrieveFrame`
  169. should be used.
  170. .. index:: QueryFrame
  171. .. _QueryFrame:
  172. QueryFrame
  173. ----------
  174. .. function:: QueryFrame(capture) -> iplimage
  175. Grabs and returns a frame from a camera or file.
  176. :param capture: video capturing structure.
  177. :type capture: :class:`CvCapture`
  178. The function
  179. ``cvQueryFrame``
  180. grabs a frame from a camera or video file, decompresses it and returns it. This function is just a combination of
  181. :ref:`GrabFrame`
  182. and
  183. :ref:`RetrieveFrame`
  184. , 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.
  185. .. index:: RetrieveFrame
  186. .. _RetrieveFrame:
  187. RetrieveFrame
  188. -------------
  189. .. function:: RetrieveFrame(capture) -> iplimage
  190. Gets the image grabbed with cvGrabFrame.
  191. :param capture: video capturing structure.
  192. :type capture: :class:`CvCapture`
  193. The function
  194. ``cvRetrieveFrame``
  195. returns the pointer to the image grabbed with the
  196. :ref:`GrabFrame`
  197. 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.
  198. .. index:: SetCaptureProperty
  199. .. _SetCaptureProperty:
  200. SetCaptureProperty
  201. ------------------
  202. .. function:: SetCaptureProperty(capture, property_id,value)->None
  203. Sets video capturing properties.
  204. :param capture: video capturing structure.
  205. :type capture: :class:`CvCapture`
  206. :param property_id: property identifier. Can be one of the following:
  207. :type property_id: int
  208. * **CV_CAP_PROP_POS_MSEC** Film current position in milliseconds or video capture timestamp
  209. * **CV_CAP_PROP_POS_FRAMES** 0-based index of the frame to be decoded/captured next
  210. * **CV_CAP_PROP_POS_AVI_RATIO** Relative position of the video file (0 - start of the film, 1 - end of the film)
  211. * **CV_CAP_PROP_FRAME_WIDTH** Width of the frames in the video stream
  212. * **CV_CAP_PROP_FRAME_HEIGHT** Height of the frames in the video stream
  213. * **CV_CAP_PROP_FPS** Frame rate
  214. * **CV_CAP_PROP_FOURCC** 4-character code of codec
  215. * **CV_CAP_PROP_FRAME_COUNT** Number of frames in the video file
  216. * **CV_CAP_PROP_FORMAT** The format of the Mat objects returned by retrieve()
  217. * **CV_CAP_PROP_MODE** A backend-specific value indicating the current capture mode
  218. * **CV_CAP_PROP_BRIGHTNESS** Brightness of the image (only for cameras)
  219. * **CV_CAP_PROP_CONTRAST** Contrast of the image (only for cameras)
  220. * **CV_CAP_PROP_SATURATION** Saturation of the image (only for cameras)
  221. * **CV_CAP_PROP_HUE** Hue of the image (only for cameras)
  222. * **CV_CAP_PROP_GAIN** Gain of the image (only for cameras)
  223. * **CV_CAP_PROP_EXPOSURE** Exposure (only for cameras)
  224. * **CV_CAP_PROP_CONVERT_RGB** Boolean flags indicating whether images should be converted to RGB
  225. * **CV_CAP_PROP_WHITE_BALANCE** Currently unsupported
  226. * **CV_CAP_PROP_RECTIFICATION** TOWRITE (note: only supported by DC1394 v 2.x backend currently)
  227. :param value: value of the property.
  228. :type value: float
  229. The function
  230. ``cvSetCaptureProperty``
  231. sets the specified property of video capturing. Currently the function supports only video files:
  232. ``CV_CAP_PROP_POS_MSEC, CV_CAP_PROP_POS_FRAMES, CV_CAP_PROP_POS_AVI_RATIO``
  233. .
  234. 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).
  235. .. index:: CreateVideoWriter
  236. .. _CreateVideoWriter:
  237. CreateVideoWriter
  238. -----------------
  239. .. function:: CreateVideoWriter(filename, fourcc, fps, frame_size, is_color) -> CvVideoWriter
  240. Creates the video file writer.
  241. :param filename: Name of the output video file.
  242. :type filename: str
  243. :param fourcc: 4-character code of codec used to compress the frames. For example, ``CV_FOURCC('P','I','M,'1')`` is a MPEG-1 codec, ``CV_FOURCC('M','J','P','G')`` is a motion-jpeg codec etc.
  244. 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.
  245. :type fourcc: int
  246. :param fps: Framerate of the created video stream.
  247. :type fps: float
  248. :param frame_size: Size of the video frames.
  249. :type frame_size: :class:`CvSize`
  250. :param 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).
  251. :type is_color: int
  252. The function
  253. ``cvCreateVideoWriter``
  254. creates the video writer structure.
  255. 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.
  256. .. index:: WriteFrame
  257. .. _WriteFrame:
  258. WriteFrame
  259. ----------
  260. .. function:: WriteFrame(writer, image)->int
  261. Writes a frame to a video file.
  262. :param writer: Video writer structure
  263. :type writer: :class:`CvVideoWriter`
  264. :param image: The written frame
  265. :type image: :class:`IplImage`
  266. The function
  267. ``cvWriteFrame``
  268. writes/appends one frame to a video file.