imgcodecs.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #ifndef __OPENCV_IMGCODECS_HPP__
  43. #define __OPENCV_IMGCODECS_HPP__
  44. #include "opencv2/core.hpp"
  45. /**
  46. @defgroup imgcodecs Image file reading and writing
  47. @{
  48. @defgroup imgcodecs_c C API
  49. @defgroup imgcodecs_ios iOS glue
  50. @}
  51. */
  52. //////////////////////////////// image codec ////////////////////////////////
  53. namespace cv
  54. {
  55. //! @addtogroup imgcodecs
  56. //! @{
  57. //! Imread flags
  58. enum ImreadModes {
  59. IMREAD_UNCHANGED = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
  60. IMREAD_GRAYSCALE = 0, //!< If set, always convert image to the single channel grayscale image.
  61. IMREAD_COLOR = 1, //!< If set, always convert image to the 3 channel BGR color image.
  62. IMREAD_ANYDEPTH = 2, //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
  63. IMREAD_ANYCOLOR = 4, //!< If set, the image is read in any possible color format.
  64. IMREAD_LOAD_GDAL = 8 //!< If set, use the gdal driver for loading the image.
  65. };
  66. //! Imwrite flags
  67. enum ImwriteFlags {
  68. IMWRITE_JPEG_QUALITY = 1, //!< For JPEG, it can be a quality from 0 to 100 (the higher is the better). Default value is 95.
  69. IMWRITE_JPEG_PROGRESSIVE = 2, //!< Enable JPEG features, 0 or 1, default is False.
  70. IMWRITE_JPEG_OPTIMIZE = 3, //!< Enable JPEG features, 0 or 1, default is False.
  71. IMWRITE_JPEG_RST_INTERVAL = 4, //!< JPEG restart interval, 0 - 65535, default is 0 - no restart.
  72. IMWRITE_JPEG_LUMA_QUALITY = 5, //!< Separate luma quality level, 0 - 100, default is 0 - don't use.
  73. IMWRITE_JPEG_CHROMA_QUALITY = 6, //!< Separate chroma quality level, 0 - 100, default is 0 - don't use.
  74. IMWRITE_PNG_COMPRESSION = 16, //!< For PNG, it can be the compression level from 0 to 9. A higher value means a smaller size and longer compression time. Default value is 3.
  75. IMWRITE_PNG_STRATEGY = 17, //!< One of cv::ImwritePNGFlags, default is IMWRITE_PNG_STRATEGY_DEFAULT.
  76. IMWRITE_PNG_BILEVEL = 18, //!< Binary level PNG, 0 or 1, default is 0.
  77. IMWRITE_PXM_BINARY = 32, //!< For PPM, PGM, or PBM, it can be a binary format flag, 0 or 1. Default value is 1.
  78. IMWRITE_WEBP_QUALITY = 64 //!< For WEBP, it can be a quality from 1 to 100 (the higher is the better). By default (without any parameter) and for quality above 100 the lossless compression is used.
  79. };
  80. //! Imwrite PNG specific flags
  81. enum ImwritePNGFlags {
  82. IMWRITE_PNG_STRATEGY_DEFAULT = 0,
  83. IMWRITE_PNG_STRATEGY_FILTERED = 1,
  84. IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY = 2,
  85. IMWRITE_PNG_STRATEGY_RLE = 3,
  86. IMWRITE_PNG_STRATEGY_FIXED = 4
  87. };
  88. /** @brief Loads an image from a file.
  89. @anchor imread
  90. @param filename Name of file to be loaded.
  91. @param flags Flag that can take values of @ref cv::ImreadModes
  92. The function imread loads an image from the specified file and returns it. If the image cannot be
  93. read (because of missing file, improper permissions, unsupported or invalid format), the function
  94. returns an empty matrix ( Mat::data==NULL ). Currently, the following file formats are supported:
  95. - Windows bitmaps - \*.bmp, \*.dib (always supported)
  96. - JPEG files - \*.jpeg, \*.jpg, \*.jpe (see the *Notes* section)
  97. - JPEG 2000 files - \*.jp2 (see the *Notes* section)
  98. - Portable Network Graphics - \*.png (see the *Notes* section)
  99. - WebP - \*.webp (see the *Notes* section)
  100. - Portable image format - \*.pbm, \*.pgm, \*.ppm (always supported)
  101. - Sun rasters - \*.sr, \*.ras (always supported)
  102. - TIFF files - \*.tiff, \*.tif (see the *Notes* section)
  103. @note
  104. - The function determines the type of an image by the content, not by the file extension.
  105. - On Microsoft Windows\* OS and MacOSX\*, the codecs shipped with an OpenCV image (libjpeg,
  106. libpng, libtiff, and libjasper) are used by default. So, OpenCV can always read JPEGs, PNGs,
  107. and TIFFs. On MacOSX, there is also an option to use native MacOSX image readers. But beware
  108. that currently these native image loaders give images with different pixel values because of
  109. the color management embedded into MacOSX.
  110. - On Linux\*, BSD flavors and other Unix-like open-source operating systems, OpenCV looks for
  111. codecs supplied with an OS image. Install the relevant packages (do not forget the development
  112. files, for example, "libjpeg-dev", in Debian\* and Ubuntu\*) to get the codec support or turn
  113. on the OPENCV_BUILD_3RDPARTY_LIBS flag in CMake.
  114. @note In the case of color images, the decoded images will have the channels stored in B G R order.
  115. */
  116. CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );
  117. /** @brief Loads a multi-page image from a file. (see imread for details.)
  118. @param filename Name of file to be loaded.
  119. @param flags Flag that can take values of @ref cv::ImreadModes, default with IMREAD_ANYCOLOR.
  120. @param mats A vector of Mat objects holding each page, if more than one.
  121. */
  122. CV_EXPORTS_W bool imreadmulti(const String& filename, std::vector<Mat>& mats, int flags = IMREAD_ANYCOLOR);
  123. /** @brief Saves an image to a specified file.
  124. @param filename Name of the file.
  125. @param img Image to be saved.
  126. @param params Format-specific save parameters encoded as pairs, see @ref cv::ImwriteFlags
  127. paramId_1, paramValue_1, paramId_2, paramValue_2, ... .
  128. The function imwrite saves the image to the specified file. The image format is chosen based on the
  129. filename extension (see imread for the list of extensions). Only 8-bit (or 16-bit unsigned (CV_16U)
  130. in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with 'BGR' channel order) images
  131. can be saved using this function. If the format, depth or channel order is different, use
  132. Mat::convertTo , and cvtColor to convert it before saving. Or, use the universal FileStorage I/O
  133. functions to save the image to XML or YAML format.
  134. It is possible to store PNG images with an alpha channel using this function. To do this, create
  135. 8-bit (or 16-bit) 4-channel image BGRA, where the alpha channel goes last. Fully transparent pixels
  136. should have alpha set to 0, fully opaque pixels should have alpha set to 255/65535. The sample below
  137. shows how to create such a BGRA image and store to PNG file. It also demonstrates how to set custom
  138. compression parameters :
  139. @code
  140. #include <vector>
  141. #include <stdio.h>
  142. #include <opencv2/opencv.hpp>
  143. using namespace cv;
  144. using namespace std;
  145. void createAlphaMat(Mat &mat)
  146. {
  147. CV_Assert(mat.channels() == 4);
  148. for (int i = 0; i < mat.rows; ++i) {
  149. for (int j = 0; j < mat.cols; ++j) {
  150. Vec4b& bgra = mat.at<Vec4b>(i, j);
  151. bgra[0] = UCHAR_MAX; // Blue
  152. bgra[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // Green
  153. bgra[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // Red
  154. bgra[3] = saturate_cast<uchar>(0.5 * (bgra[1] + bgra[2])); // Alpha
  155. }
  156. }
  157. }
  158. int main(int argv, char **argc)
  159. {
  160. // Create mat with alpha channel
  161. Mat mat(480, 640, CV_8UC4);
  162. createAlphaMat(mat);
  163. vector<int> compression_params;
  164. compression_params.push_back(IMWRITE_PNG_COMPRESSION);
  165. compression_params.push_back(9);
  166. try {
  167. imwrite("alpha.png", mat, compression_params);
  168. }
  169. catch (runtime_error& ex) {
  170. fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
  171. return 1;
  172. }
  173. fprintf(stdout, "Saved PNG file with alpha data.\n");
  174. return 0;
  175. }
  176. @endcode
  177. */
  178. CV_EXPORTS_W bool imwrite( const String& filename, InputArray img,
  179. const std::vector<int>& params = std::vector<int>());
  180. /** @overload */
  181. CV_EXPORTS_W Mat imdecode( InputArray buf, int flags );
  182. /** @brief Reads an image from a buffer in memory.
  183. @param buf Input array or vector of bytes.
  184. @param flags The same flags as in imread, see @ref cv::ImreadModes.
  185. @param dst The optional output placeholder for the decoded matrix. It can save the image
  186. reallocations when the function is called repeatedly for images of the same size.
  187. The function reads an image from the specified buffer in the memory. If the buffer is too short or
  188. contains invalid data, the empty matrix/image is returned.
  189. See imread for the list of supported formats and flags description.
  190. @note In the case of color images, the decoded images will have the channels stored in B G R order.
  191. */
  192. CV_EXPORTS Mat imdecode( InputArray buf, int flags, Mat* dst);
  193. /** @brief Encodes an image into a memory buffer.
  194. @param ext File extension that defines the output format.
  195. @param img Image to be written.
  196. @param buf Output buffer resized to fit the compressed image.
  197. @param params Format-specific parameters. See imwrite and @ref cv::ImwriteFlags.
  198. The function compresses the image and stores it in the memory buffer that is resized to fit the
  199. result. See imwrite for the list of supported formats and flags description.
  200. @note cvEncodeImage returns single-row matrix of type CV_8UC1 that contains encoded image as array
  201. of bytes.
  202. */
  203. CV_EXPORTS_W bool imencode( const String& ext, InputArray img,
  204. CV_OUT std::vector<uchar>& buf,
  205. const std::vector<int>& params = std::vector<int>());
  206. //! @} imgcodecs
  207. } // cv
  208. #endif //__OPENCV_IMGCODECS_HPP__