openCV library for Renesas RZ/A
Dependents: RZ_A2M_Mbed_samples
include/opencv2/imgcodecs.hpp@0:0e0631af0305, 2021-01-29 (annotated)
- Committer:
- RyoheiHagimoto
- Date:
- Fri Jan 29 04:53:38 2021 +0000
- Revision:
- 0:0e0631af0305
copied from https://github.com/d-kato/opencv-lib.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RyoheiHagimoto | 0:0e0631af0305 | 1 | /*M/////////////////////////////////////////////////////////////////////////////////////// |
RyoheiHagimoto | 0:0e0631af0305 | 2 | // |
RyoheiHagimoto | 0:0e0631af0305 | 3 | // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. |
RyoheiHagimoto | 0:0e0631af0305 | 4 | // |
RyoheiHagimoto | 0:0e0631af0305 | 5 | // By downloading, copying, installing or using the software you agree to this license. |
RyoheiHagimoto | 0:0e0631af0305 | 6 | // If you do not agree to this license, do not download, install, |
RyoheiHagimoto | 0:0e0631af0305 | 7 | // copy or use the software. |
RyoheiHagimoto | 0:0e0631af0305 | 8 | // |
RyoheiHagimoto | 0:0e0631af0305 | 9 | // |
RyoheiHagimoto | 0:0e0631af0305 | 10 | // License Agreement |
RyoheiHagimoto | 0:0e0631af0305 | 11 | // For Open Source Computer Vision Library |
RyoheiHagimoto | 0:0e0631af0305 | 12 | // |
RyoheiHagimoto | 0:0e0631af0305 | 13 | // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. |
RyoheiHagimoto | 0:0e0631af0305 | 14 | // Copyright (C) 2009, Willow Garage Inc., all rights reserved. |
RyoheiHagimoto | 0:0e0631af0305 | 15 | // Third party copyrights are property of their respective owners. |
RyoheiHagimoto | 0:0e0631af0305 | 16 | // |
RyoheiHagimoto | 0:0e0631af0305 | 17 | // Redistribution and use in source and binary forms, with or without modification, |
RyoheiHagimoto | 0:0e0631af0305 | 18 | // are permitted provided that the following conditions are met: |
RyoheiHagimoto | 0:0e0631af0305 | 19 | // |
RyoheiHagimoto | 0:0e0631af0305 | 20 | // * Redistribution's of source code must retain the above copyright notice, |
RyoheiHagimoto | 0:0e0631af0305 | 21 | // this list of conditions and the following disclaimer. |
RyoheiHagimoto | 0:0e0631af0305 | 22 | // |
RyoheiHagimoto | 0:0e0631af0305 | 23 | // * Redistribution's in binary form must reproduce the above copyright notice, |
RyoheiHagimoto | 0:0e0631af0305 | 24 | // this list of conditions and the following disclaimer in the documentation |
RyoheiHagimoto | 0:0e0631af0305 | 25 | // and/or other materials provided with the distribution. |
RyoheiHagimoto | 0:0e0631af0305 | 26 | // |
RyoheiHagimoto | 0:0e0631af0305 | 27 | // * The name of the copyright holders may not be used to endorse or promote products |
RyoheiHagimoto | 0:0e0631af0305 | 28 | // derived from this software without specific prior written permission. |
RyoheiHagimoto | 0:0e0631af0305 | 29 | // |
RyoheiHagimoto | 0:0e0631af0305 | 30 | // This software is provided by the copyright holders and contributors "as is" and |
RyoheiHagimoto | 0:0e0631af0305 | 31 | // any express or implied warranties, including, but not limited to, the implied |
RyoheiHagimoto | 0:0e0631af0305 | 32 | // warranties of merchantability and fitness for a particular purpose are disclaimed. |
RyoheiHagimoto | 0:0e0631af0305 | 33 | // In no event shall the Intel Corporation or contributors be liable for any direct, |
RyoheiHagimoto | 0:0e0631af0305 | 34 | // indirect, incidental, special, exemplary, or consequential damages |
RyoheiHagimoto | 0:0e0631af0305 | 35 | // (including, but not limited to, procurement of substitute goods or services; |
RyoheiHagimoto | 0:0e0631af0305 | 36 | // loss of use, data, or profits; or business interruption) however caused |
RyoheiHagimoto | 0:0e0631af0305 | 37 | // and on any theory of liability, whether in contract, strict liability, |
RyoheiHagimoto | 0:0e0631af0305 | 38 | // or tort (including negligence or otherwise) arising in any way out of |
RyoheiHagimoto | 0:0e0631af0305 | 39 | // the use of this software, even if advised of the possibility of such damage. |
RyoheiHagimoto | 0:0e0631af0305 | 40 | // |
RyoheiHagimoto | 0:0e0631af0305 | 41 | //M*/ |
RyoheiHagimoto | 0:0e0631af0305 | 42 | |
RyoheiHagimoto | 0:0e0631af0305 | 43 | #ifndef OPENCV_IMGCODECS_HPP |
RyoheiHagimoto | 0:0e0631af0305 | 44 | #define OPENCV_IMGCODECS_HPP |
RyoheiHagimoto | 0:0e0631af0305 | 45 | |
RyoheiHagimoto | 0:0e0631af0305 | 46 | #include "opencv2/core.hpp" |
RyoheiHagimoto | 0:0e0631af0305 | 47 | |
RyoheiHagimoto | 0:0e0631af0305 | 48 | /** |
RyoheiHagimoto | 0:0e0631af0305 | 49 | @defgroup imgcodecs Image file reading and writing |
RyoheiHagimoto | 0:0e0631af0305 | 50 | @{ |
RyoheiHagimoto | 0:0e0631af0305 | 51 | @defgroup imgcodecs_c C API |
RyoheiHagimoto | 0:0e0631af0305 | 52 | @defgroup imgcodecs_ios iOS glue |
RyoheiHagimoto | 0:0e0631af0305 | 53 | @} |
RyoheiHagimoto | 0:0e0631af0305 | 54 | */ |
RyoheiHagimoto | 0:0e0631af0305 | 55 | |
RyoheiHagimoto | 0:0e0631af0305 | 56 | //////////////////////////////// image codec //////////////////////////////// |
RyoheiHagimoto | 0:0e0631af0305 | 57 | namespace cv |
RyoheiHagimoto | 0:0e0631af0305 | 58 | { |
RyoheiHagimoto | 0:0e0631af0305 | 59 | |
RyoheiHagimoto | 0:0e0631af0305 | 60 | //! @addtogroup imgcodecs |
RyoheiHagimoto | 0:0e0631af0305 | 61 | //! @{ |
RyoheiHagimoto | 0:0e0631af0305 | 62 | |
RyoheiHagimoto | 0:0e0631af0305 | 63 | //! Imread flags |
RyoheiHagimoto | 0:0e0631af0305 | 64 | enum ImreadModes { |
RyoheiHagimoto | 0:0e0631af0305 | 65 | IMREAD_UNCHANGED = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). |
RyoheiHagimoto | 0:0e0631af0305 | 66 | IMREAD_GRAYSCALE = 0, //!< If set, always convert image to the single channel grayscale image. |
RyoheiHagimoto | 0:0e0631af0305 | 67 | IMREAD_COLOR = 1, //!< If set, always convert image to the 3 channel BGR color image. |
RyoheiHagimoto | 0:0e0631af0305 | 68 | IMREAD_ANYDEPTH = 2, //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit. |
RyoheiHagimoto | 0:0e0631af0305 | 69 | IMREAD_ANYCOLOR = 4, //!< If set, the image is read in any possible color format. |
RyoheiHagimoto | 0:0e0631af0305 | 70 | IMREAD_LOAD_GDAL = 8, //!< If set, use the gdal driver for loading the image. |
RyoheiHagimoto | 0:0e0631af0305 | 71 | IMREAD_REDUCED_GRAYSCALE_2 = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2. |
RyoheiHagimoto | 0:0e0631af0305 | 72 | IMREAD_REDUCED_COLOR_2 = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2. |
RyoheiHagimoto | 0:0e0631af0305 | 73 | IMREAD_REDUCED_GRAYSCALE_4 = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4. |
RyoheiHagimoto | 0:0e0631af0305 | 74 | IMREAD_REDUCED_COLOR_4 = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4. |
RyoheiHagimoto | 0:0e0631af0305 | 75 | IMREAD_REDUCED_GRAYSCALE_8 = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8. |
RyoheiHagimoto | 0:0e0631af0305 | 76 | IMREAD_REDUCED_COLOR_8 = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8. |
RyoheiHagimoto | 0:0e0631af0305 | 77 | IMREAD_IGNORE_ORIENTATION = 128 //!< If set, do not rotate the image according to EXIF's orientation flag. |
RyoheiHagimoto | 0:0e0631af0305 | 78 | }; |
RyoheiHagimoto | 0:0e0631af0305 | 79 | |
RyoheiHagimoto | 0:0e0631af0305 | 80 | //! Imwrite flags |
RyoheiHagimoto | 0:0e0631af0305 | 81 | enum ImwriteFlags { |
RyoheiHagimoto | 0:0e0631af0305 | 82 | IMWRITE_JPEG_QUALITY = 1, //!< For JPEG, it can be a quality from 0 to 100 (the higher is the better). Default value is 95. |
RyoheiHagimoto | 0:0e0631af0305 | 83 | IMWRITE_JPEG_PROGRESSIVE = 2, //!< Enable JPEG features, 0 or 1, default is False. |
RyoheiHagimoto | 0:0e0631af0305 | 84 | IMWRITE_JPEG_OPTIMIZE = 3, //!< Enable JPEG features, 0 or 1, default is False. |
RyoheiHagimoto | 0:0e0631af0305 | 85 | IMWRITE_JPEG_RST_INTERVAL = 4, //!< JPEG restart interval, 0 - 65535, default is 0 - no restart. |
RyoheiHagimoto | 0:0e0631af0305 | 86 | IMWRITE_JPEG_LUMA_QUALITY = 5, //!< Separate luma quality level, 0 - 100, default is 0 - don't use. |
RyoheiHagimoto | 0:0e0631af0305 | 87 | IMWRITE_JPEG_CHROMA_QUALITY = 6, //!< Separate chroma quality level, 0 - 100, default is 0 - don't use. |
RyoheiHagimoto | 0:0e0631af0305 | 88 | 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. Also strategy is changed to IMWRITE_PNG_STRATEGY_DEFAULT (Z_DEFAULT_STRATEGY). |
RyoheiHagimoto | 0:0e0631af0305 | 89 | IMWRITE_PNG_STRATEGY = 17, //!< One of cv::ImwritePNGFlags, default is IMWRITE_PNG_STRATEGY_DEFAULT. |
RyoheiHagimoto | 0:0e0631af0305 | 90 | IMWRITE_PNG_BILEVEL = 18, //!< Binary level PNG, 0 or 1, default is 0. |
RyoheiHagimoto | 0:0e0631af0305 | 91 | IMWRITE_PXM_BINARY = 32, //!< For PPM, PGM, or PBM, it can be a binary format flag, 0 or 1. Default value is 1. |
RyoheiHagimoto | 0:0e0631af0305 | 92 | 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. |
RyoheiHagimoto | 0:0e0631af0305 | 93 | IMWRITE_PAM_TUPLETYPE = 128,//!< For PAM, sets the TUPLETYPE field to the corresponding string value that is defined for the format |
RyoheiHagimoto | 0:0e0631af0305 | 94 | }; |
RyoheiHagimoto | 0:0e0631af0305 | 95 | |
RyoheiHagimoto | 0:0e0631af0305 | 96 | //! Imwrite PNG specific flags used to tune the compression algorithm. |
RyoheiHagimoto | 0:0e0631af0305 | 97 | /** These flags will be modify the way of PNG image compression and will be passed to the underlying zlib processing stage. |
RyoheiHagimoto | 0:0e0631af0305 | 98 | |
RyoheiHagimoto | 0:0e0631af0305 | 99 | - The effect of IMWRITE_PNG_STRATEGY_FILTERED is to force more Huffman coding and less string matching; it is somewhat intermediate between IMWRITE_PNG_STRATEGY_DEFAULT and IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY. |
RyoheiHagimoto | 0:0e0631af0305 | 100 | - IMWRITE_PNG_STRATEGY_RLE is designed to be almost as fast as IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY, but give better compression for PNG image data. |
RyoheiHagimoto | 0:0e0631af0305 | 101 | - The strategy parameter only affects the compression ratio but not the correctness of the compressed output even if it is not set appropriately. |
RyoheiHagimoto | 0:0e0631af0305 | 102 | - IMWRITE_PNG_STRATEGY_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler decoder for special applications. |
RyoheiHagimoto | 0:0e0631af0305 | 103 | */ |
RyoheiHagimoto | 0:0e0631af0305 | 104 | enum ImwritePNGFlags { |
RyoheiHagimoto | 0:0e0631af0305 | 105 | IMWRITE_PNG_STRATEGY_DEFAULT = 0, //!< Use this value for normal data. |
RyoheiHagimoto | 0:0e0631af0305 | 106 | IMWRITE_PNG_STRATEGY_FILTERED = 1, //!< Use this value for data produced by a filter (or predictor).Filtered data consists mostly of small values with a somewhat random distribution. In this case, the compression algorithm is tuned to compress them better. |
RyoheiHagimoto | 0:0e0631af0305 | 107 | IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY = 2, //!< Use this value to force Huffman encoding only (no string match). |
RyoheiHagimoto | 0:0e0631af0305 | 108 | IMWRITE_PNG_STRATEGY_RLE = 3, //!< Use this value to limit match distances to one (run-length encoding). |
RyoheiHagimoto | 0:0e0631af0305 | 109 | IMWRITE_PNG_STRATEGY_FIXED = 4 //!< Using this value prevents the use of dynamic Huffman codes, allowing for a simpler decoder for special applications. |
RyoheiHagimoto | 0:0e0631af0305 | 110 | }; |
RyoheiHagimoto | 0:0e0631af0305 | 111 | |
RyoheiHagimoto | 0:0e0631af0305 | 112 | //! Imwrite PAM specific tupletype flags used to define the 'TUPETYPE' field of a PAM file. |
RyoheiHagimoto | 0:0e0631af0305 | 113 | enum ImwritePAMFlags { |
RyoheiHagimoto | 0:0e0631af0305 | 114 | IMWRITE_PAM_FORMAT_NULL = 0, |
RyoheiHagimoto | 0:0e0631af0305 | 115 | IMWRITE_PAM_FORMAT_BLACKANDWHITE = 1, |
RyoheiHagimoto | 0:0e0631af0305 | 116 | IMWRITE_PAM_FORMAT_GRAYSCALE = 2, |
RyoheiHagimoto | 0:0e0631af0305 | 117 | IMWRITE_PAM_FORMAT_GRAYSCALE_ALPHA = 3, |
RyoheiHagimoto | 0:0e0631af0305 | 118 | IMWRITE_PAM_FORMAT_RGB = 4, |
RyoheiHagimoto | 0:0e0631af0305 | 119 | IMWRITE_PAM_FORMAT_RGB_ALPHA = 5, |
RyoheiHagimoto | 0:0e0631af0305 | 120 | }; |
RyoheiHagimoto | 0:0e0631af0305 | 121 | |
RyoheiHagimoto | 0:0e0631af0305 | 122 | /** @brief Loads an image from a file. |
RyoheiHagimoto | 0:0e0631af0305 | 123 | |
RyoheiHagimoto | 0:0e0631af0305 | 124 | @anchor imread |
RyoheiHagimoto | 0:0e0631af0305 | 125 | |
RyoheiHagimoto | 0:0e0631af0305 | 126 | The function imread loads an image from the specified file and returns it. If the image cannot be |
RyoheiHagimoto | 0:0e0631af0305 | 127 | read (because of missing file, improper permissions, unsupported or invalid format), the function |
RyoheiHagimoto | 0:0e0631af0305 | 128 | returns an empty matrix ( Mat::data==NULL ). |
RyoheiHagimoto | 0:0e0631af0305 | 129 | |
RyoheiHagimoto | 0:0e0631af0305 | 130 | Currently, the following file formats are supported: |
RyoheiHagimoto | 0:0e0631af0305 | 131 | |
RyoheiHagimoto | 0:0e0631af0305 | 132 | - Windows bitmaps - \*.bmp, \*.dib (always supported) |
RyoheiHagimoto | 0:0e0631af0305 | 133 | - JPEG files - \*.jpeg, \*.jpg, \*.jpe (see the *Notes* section) |
RyoheiHagimoto | 0:0e0631af0305 | 134 | - JPEG 2000 files - \*.jp2 (see the *Notes* section) |
RyoheiHagimoto | 0:0e0631af0305 | 135 | - Portable Network Graphics - \*.png (see the *Notes* section) |
RyoheiHagimoto | 0:0e0631af0305 | 136 | - WebP - \*.webp (see the *Notes* section) |
RyoheiHagimoto | 0:0e0631af0305 | 137 | - Portable image format - \*.pbm, \*.pgm, \*.ppm \*.pxm, \*.pnm (always supported) |
RyoheiHagimoto | 0:0e0631af0305 | 138 | - Sun rasters - \*.sr, \*.ras (always supported) |
RyoheiHagimoto | 0:0e0631af0305 | 139 | - TIFF files - \*.tiff, \*.tif (see the *Notes* section) |
RyoheiHagimoto | 0:0e0631af0305 | 140 | - OpenEXR Image files - \*.exr (see the *Notes* section) |
RyoheiHagimoto | 0:0e0631af0305 | 141 | - Radiance HDR - \*.hdr, \*.pic (always supported) |
RyoheiHagimoto | 0:0e0631af0305 | 142 | - Raster and Vector geospatial data supported by Gdal (see the *Notes* section) |
RyoheiHagimoto | 0:0e0631af0305 | 143 | |
RyoheiHagimoto | 0:0e0631af0305 | 144 | @note |
RyoheiHagimoto | 0:0e0631af0305 | 145 | |
RyoheiHagimoto | 0:0e0631af0305 | 146 | - The function determines the type of an image by the content, not by the file extension. |
RyoheiHagimoto | 0:0e0631af0305 | 147 | - In the case of color images, the decoded images will have the channels stored in **B G R** order. |
RyoheiHagimoto | 0:0e0631af0305 | 148 | - On Microsoft Windows\* OS and MacOSX\*, the codecs shipped with an OpenCV image (libjpeg, |
RyoheiHagimoto | 0:0e0631af0305 | 149 | libpng, libtiff, and libjasper) are used by default. So, OpenCV can always read JPEGs, PNGs, |
RyoheiHagimoto | 0:0e0631af0305 | 150 | and TIFFs. On MacOSX, there is also an option to use native MacOSX image readers. But beware |
RyoheiHagimoto | 0:0e0631af0305 | 151 | that currently these native image loaders give images with different pixel values because of |
RyoheiHagimoto | 0:0e0631af0305 | 152 | the color management embedded into MacOSX. |
RyoheiHagimoto | 0:0e0631af0305 | 153 | - On Linux\*, BSD flavors and other Unix-like open-source operating systems, OpenCV looks for |
RyoheiHagimoto | 0:0e0631af0305 | 154 | codecs supplied with an OS image. Install the relevant packages (do not forget the development |
RyoheiHagimoto | 0:0e0631af0305 | 155 | files, for example, "libjpeg-dev", in Debian\* and Ubuntu\*) to get the codec support or turn |
RyoheiHagimoto | 0:0e0631af0305 | 156 | on the OPENCV_BUILD_3RDPARTY_LIBS flag in CMake. |
RyoheiHagimoto | 0:0e0631af0305 | 157 | - In the case you set *WITH_GDAL* flag to true in CMake and @ref IMREAD_LOAD_GDAL to load the image, |
RyoheiHagimoto | 0:0e0631af0305 | 158 | then [GDAL](http://www.gdal.org) driver will be used in order to decode the image by supporting |
RyoheiHagimoto | 0:0e0631af0305 | 159 | the following formats: [Raster](http://www.gdal.org/formats_list.html), |
RyoheiHagimoto | 0:0e0631af0305 | 160 | [Vector](http://www.gdal.org/ogr_formats.html). |
RyoheiHagimoto | 0:0e0631af0305 | 161 | - If EXIF information are embedded in the image file, the EXIF orientation will be taken into account |
RyoheiHagimoto | 0:0e0631af0305 | 162 | and thus the image will be rotated accordingly except if the flag @ref IMREAD_IGNORE_ORIENTATION is passed. |
RyoheiHagimoto | 0:0e0631af0305 | 163 | @param filename Name of file to be loaded. |
RyoheiHagimoto | 0:0e0631af0305 | 164 | @param flags Flag that can take values of cv::ImreadModes |
RyoheiHagimoto | 0:0e0631af0305 | 165 | */ |
RyoheiHagimoto | 0:0e0631af0305 | 166 | CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR ); |
RyoheiHagimoto | 0:0e0631af0305 | 167 | |
RyoheiHagimoto | 0:0e0631af0305 | 168 | /** @brief Loads a multi-page image from a file. |
RyoheiHagimoto | 0:0e0631af0305 | 169 | |
RyoheiHagimoto | 0:0e0631af0305 | 170 | The function imreadmulti loads a multi-page image from the specified file into a vector of Mat objects. |
RyoheiHagimoto | 0:0e0631af0305 | 171 | @param filename Name of file to be loaded. |
RyoheiHagimoto | 0:0e0631af0305 | 172 | @param flags Flag that can take values of cv::ImreadModes, default with cv::IMREAD_ANYCOLOR. |
RyoheiHagimoto | 0:0e0631af0305 | 173 | @param mats A vector of Mat objects holding each page, if more than one. |
RyoheiHagimoto | 0:0e0631af0305 | 174 | @sa cv::imread |
RyoheiHagimoto | 0:0e0631af0305 | 175 | */ |
RyoheiHagimoto | 0:0e0631af0305 | 176 | CV_EXPORTS_W bool imreadmulti(const String& filename, std::vector<Mat>& mats, int flags = IMREAD_ANYCOLOR); |
RyoheiHagimoto | 0:0e0631af0305 | 177 | |
RyoheiHagimoto | 0:0e0631af0305 | 178 | /** @brief Saves an image to a specified file. |
RyoheiHagimoto | 0:0e0631af0305 | 179 | |
RyoheiHagimoto | 0:0e0631af0305 | 180 | The function imwrite saves the image to the specified file. The image format is chosen based on the |
RyoheiHagimoto | 0:0e0631af0305 | 181 | filename extension (see cv::imread for the list of extensions). Only 8-bit (or 16-bit unsigned (CV_16U) |
RyoheiHagimoto | 0:0e0631af0305 | 182 | in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with 'BGR' channel order) images |
RyoheiHagimoto | 0:0e0631af0305 | 183 | can be saved using this function. If the format, depth or channel order is different, use |
RyoheiHagimoto | 0:0e0631af0305 | 184 | Mat::convertTo , and cv::cvtColor to convert it before saving. Or, use the universal FileStorage I/O |
RyoheiHagimoto | 0:0e0631af0305 | 185 | functions to save the image to XML or YAML format. |
RyoheiHagimoto | 0:0e0631af0305 | 186 | |
RyoheiHagimoto | 0:0e0631af0305 | 187 | It is possible to store PNG images with an alpha channel using this function. To do this, create |
RyoheiHagimoto | 0:0e0631af0305 | 188 | 8-bit (or 16-bit) 4-channel image BGRA, where the alpha channel goes last. Fully transparent pixels |
RyoheiHagimoto | 0:0e0631af0305 | 189 | should have alpha set to 0, fully opaque pixels should have alpha set to 255/65535. |
RyoheiHagimoto | 0:0e0631af0305 | 190 | |
RyoheiHagimoto | 0:0e0631af0305 | 191 | The sample below shows how to create such a BGRA image and store to PNG file. It also demonstrates how to set custom |
RyoheiHagimoto | 0:0e0631af0305 | 192 | compression parameters : |
RyoheiHagimoto | 0:0e0631af0305 | 193 | @code |
RyoheiHagimoto | 0:0e0631af0305 | 194 | #include <opencv2/opencv.hpp> |
RyoheiHagimoto | 0:0e0631af0305 | 195 | |
RyoheiHagimoto | 0:0e0631af0305 | 196 | using namespace cv; |
RyoheiHagimoto | 0:0e0631af0305 | 197 | using namespace std; |
RyoheiHagimoto | 0:0e0631af0305 | 198 | |
RyoheiHagimoto | 0:0e0631af0305 | 199 | void createAlphaMat(Mat &mat) |
RyoheiHagimoto | 0:0e0631af0305 | 200 | { |
RyoheiHagimoto | 0:0e0631af0305 | 201 | CV_Assert(mat.channels() == 4); |
RyoheiHagimoto | 0:0e0631af0305 | 202 | for (int i = 0; i < mat.rows; ++i) { |
RyoheiHagimoto | 0:0e0631af0305 | 203 | for (int j = 0; j < mat.cols; ++j) { |
RyoheiHagimoto | 0:0e0631af0305 | 204 | Vec4b& bgra = mat.at<Vec4b>(i, j); |
RyoheiHagimoto | 0:0e0631af0305 | 205 | bgra[0] = UCHAR_MAX; // Blue |
RyoheiHagimoto | 0:0e0631af0305 | 206 | bgra[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // Green |
RyoheiHagimoto | 0:0e0631af0305 | 207 | bgra[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // Red |
RyoheiHagimoto | 0:0e0631af0305 | 208 | bgra[3] = saturate_cast<uchar>(0.5 * (bgra[1] + bgra[2])); // Alpha |
RyoheiHagimoto | 0:0e0631af0305 | 209 | } |
RyoheiHagimoto | 0:0e0631af0305 | 210 | } |
RyoheiHagimoto | 0:0e0631af0305 | 211 | } |
RyoheiHagimoto | 0:0e0631af0305 | 212 | |
RyoheiHagimoto | 0:0e0631af0305 | 213 | int main(int argv, char **argc) |
RyoheiHagimoto | 0:0e0631af0305 | 214 | { |
RyoheiHagimoto | 0:0e0631af0305 | 215 | // Create mat with alpha channel |
RyoheiHagimoto | 0:0e0631af0305 | 216 | Mat mat(480, 640, CV_8UC4); |
RyoheiHagimoto | 0:0e0631af0305 | 217 | createAlphaMat(mat); |
RyoheiHagimoto | 0:0e0631af0305 | 218 | |
RyoheiHagimoto | 0:0e0631af0305 | 219 | vector<int> compression_params; |
RyoheiHagimoto | 0:0e0631af0305 | 220 | compression_params.push_back(IMWRITE_PNG_COMPRESSION); |
RyoheiHagimoto | 0:0e0631af0305 | 221 | compression_params.push_back(9); |
RyoheiHagimoto | 0:0e0631af0305 | 222 | |
RyoheiHagimoto | 0:0e0631af0305 | 223 | try { |
RyoheiHagimoto | 0:0e0631af0305 | 224 | imwrite("alpha.png", mat, compression_params); |
RyoheiHagimoto | 0:0e0631af0305 | 225 | } |
RyoheiHagimoto | 0:0e0631af0305 | 226 | catch (cv::Exception& ex) { |
RyoheiHagimoto | 0:0e0631af0305 | 227 | fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what()); |
RyoheiHagimoto | 0:0e0631af0305 | 228 | return 1; |
RyoheiHagimoto | 0:0e0631af0305 | 229 | } |
RyoheiHagimoto | 0:0e0631af0305 | 230 | |
RyoheiHagimoto | 0:0e0631af0305 | 231 | fprintf(stdout, "Saved PNG file with alpha data.\n"); |
RyoheiHagimoto | 0:0e0631af0305 | 232 | return 0; |
RyoheiHagimoto | 0:0e0631af0305 | 233 | } |
RyoheiHagimoto | 0:0e0631af0305 | 234 | @endcode |
RyoheiHagimoto | 0:0e0631af0305 | 235 | @param filename Name of the file. |
RyoheiHagimoto | 0:0e0631af0305 | 236 | @param img Image to be saved. |
RyoheiHagimoto | 0:0e0631af0305 | 237 | @param params Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ... .) see cv::ImwriteFlags |
RyoheiHagimoto | 0:0e0631af0305 | 238 | */ |
RyoheiHagimoto | 0:0e0631af0305 | 239 | CV_EXPORTS_W bool imwrite( const String& filename, InputArray img, |
RyoheiHagimoto | 0:0e0631af0305 | 240 | const std::vector<int>& params = std::vector<int>()); |
RyoheiHagimoto | 0:0e0631af0305 | 241 | |
RyoheiHagimoto | 0:0e0631af0305 | 242 | /** @brief Reads an image from a buffer in memory. |
RyoheiHagimoto | 0:0e0631af0305 | 243 | |
RyoheiHagimoto | 0:0e0631af0305 | 244 | The function imdecode reads an image from the specified buffer in the memory. If the buffer is too short or |
RyoheiHagimoto | 0:0e0631af0305 | 245 | contains invalid data, the function returns an empty matrix ( Mat::data==NULL ). |
RyoheiHagimoto | 0:0e0631af0305 | 246 | |
RyoheiHagimoto | 0:0e0631af0305 | 247 | See cv::imread for the list of supported formats and flags description. |
RyoheiHagimoto | 0:0e0631af0305 | 248 | |
RyoheiHagimoto | 0:0e0631af0305 | 249 | @note In the case of color images, the decoded images will have the channels stored in **B G R** order. |
RyoheiHagimoto | 0:0e0631af0305 | 250 | @param buf Input array or vector of bytes. |
RyoheiHagimoto | 0:0e0631af0305 | 251 | @param flags The same flags as in cv::imread, see cv::ImreadModes. |
RyoheiHagimoto | 0:0e0631af0305 | 252 | */ |
RyoheiHagimoto | 0:0e0631af0305 | 253 | CV_EXPORTS_W Mat imdecode( InputArray buf, int flags ); |
RyoheiHagimoto | 0:0e0631af0305 | 254 | |
RyoheiHagimoto | 0:0e0631af0305 | 255 | /** @overload |
RyoheiHagimoto | 0:0e0631af0305 | 256 | @param buf |
RyoheiHagimoto | 0:0e0631af0305 | 257 | @param flags |
RyoheiHagimoto | 0:0e0631af0305 | 258 | @param dst The optional output placeholder for the decoded matrix. It can save the image |
RyoheiHagimoto | 0:0e0631af0305 | 259 | reallocations when the function is called repeatedly for images of the same size. |
RyoheiHagimoto | 0:0e0631af0305 | 260 | */ |
RyoheiHagimoto | 0:0e0631af0305 | 261 | CV_EXPORTS Mat imdecode( InputArray buf, int flags, Mat* dst); |
RyoheiHagimoto | 0:0e0631af0305 | 262 | |
RyoheiHagimoto | 0:0e0631af0305 | 263 | /** @brief Encodes an image into a memory buffer. |
RyoheiHagimoto | 0:0e0631af0305 | 264 | |
RyoheiHagimoto | 0:0e0631af0305 | 265 | The function imencode compresses the image and stores it in the memory buffer that is resized to fit the |
RyoheiHagimoto | 0:0e0631af0305 | 266 | result. See cv::imwrite for the list of supported formats and flags description. |
RyoheiHagimoto | 0:0e0631af0305 | 267 | |
RyoheiHagimoto | 0:0e0631af0305 | 268 | @param ext File extension that defines the output format. |
RyoheiHagimoto | 0:0e0631af0305 | 269 | @param img Image to be written. |
RyoheiHagimoto | 0:0e0631af0305 | 270 | @param buf Output buffer resized to fit the compressed image. |
RyoheiHagimoto | 0:0e0631af0305 | 271 | @param params Format-specific parameters. See cv::imwrite and cv::ImwriteFlags. |
RyoheiHagimoto | 0:0e0631af0305 | 272 | */ |
RyoheiHagimoto | 0:0e0631af0305 | 273 | CV_EXPORTS_W bool imencode( const String& ext, InputArray img, |
RyoheiHagimoto | 0:0e0631af0305 | 274 | CV_OUT std::vector<uchar>& buf, |
RyoheiHagimoto | 0:0e0631af0305 | 275 | const std::vector<int>& params = std::vector<int>()); |
RyoheiHagimoto | 0:0e0631af0305 | 276 | |
RyoheiHagimoto | 0:0e0631af0305 | 277 | //! @} imgcodecs |
RyoheiHagimoto | 0:0e0631af0305 | 278 | |
RyoheiHagimoto | 0:0e0631af0305 | 279 | } // cv |
RyoheiHagimoto | 0:0e0631af0305 | 280 | |
RyoheiHagimoto | 0:0e0631af0305 | 281 | #endif //OPENCV_IMGCODECS_HPP |