@brief Loads an image from a file.
@anchor imread
The function imread loads an image from the specified file and returns it. If the image cannot be
read (because of missing file, improper permissions, unsupported or invalid format), the function
returns an empty matrix ( Mat::data==NULL ).函数imread从指定的文件中加载一个图像并返回它。如果图像不能被读取(因为文件丢失、权限不当、不支持或无效格式),函数返回一个空矩阵(Mat::data==NULL)。
目前支持的格式:
Currently, the following file formats are supported:
- Windows bitmaps - \*.bmp, \*.dib (always supported)
- JPEG files - \*.jpeg, \*.jpg, \*.jpe (see the *Note* section)
- JPEG 2000 files - \*.jp2 (see the *Note* section)
- Portable Network Graphics - \*.png (see the *Note* section)
- WebP - \*.webp (see the *Note* section)
- Portable image format - \*.pbm, \*.pgm, \*.ppm \*.pxm, \*.pnm (always supported)
- PFM files - \*.pfm (see the *Note* section)
- Sun rasters - \*.sr, \*.ras (always supported)
- TIFF files - \*.tiff, \*.tif (see the *Note* section)
- OpenEXR Image files - \*.exr (see the *Note* section)
- Radiance HDR - \*.hdr, \*.pic (always supported)
- Raster and Vector geospatial data supported by GDAL (see the *Note* section)
@note注意事项
- The function determines the type of an image by the content, not by the file extension.该函数根据内容而不是文件扩展名确定图像的类型。
- In the case of color images, the decoded images will have the channels stored in **B G R** order.在彩色图像的情况下,解码后的图像将具有按** B G R **顺序存储的通道。
- When using IMREAD_GRAYSCALE, the codec's internal grayscale conversion will be used, if available.Results may differ to the output of cvtColor()。使用IMREAD_GRAYSCALE时,将使用编解码器的内部灰度转换(如果可用)。结果可能与cvtColor()的输出有所不同- On Microsoft WindowsOS and MacOSX, the codecs shipped with an OpenCV image (libjpeg,
libpng, libtiff, and libjasper) are used by default. So, OpenCV can always read JPEGs, PNGs,
and TIFFs. On MacOSX, there is also an option to use native MacOSX image readers. But beware
that currently these native image loaders give images with different pixel values because of
the color management embedded into MacOSX.
- On Linux\*, BSD flavors and other Unix-like open-source operating systems, OpenCV looks for
codecs supplied with an OS image. Install the relevant packages (do not forget the development
files, for example, "libjpeg-dev", in Debian\* and Ubuntu\*) to get the codec support or turn
on the OPENCV_BUILD_3RDPARTY_LIBS flag in CMake.
- In the case you set *WITH_GDAL* flag to true in CMake and @ref IMREAD_LOAD_GDAL to load the image,
then the [GDAL](http://www.gdal.org) driver will be used in order to decode the image, supporting
the following formats: [Raster](http://www.gdal.org/formats_list.html),
[Vector](http://www.gdal.org/ogr_formats.html).
- If EXIF information is embedded in the image file, the EXIF orientation will be taken into account
and thus the image will be rotated accordingly except if the flags @ref IMREAD_IGNORE_ORIENTATION
or @ref IMREAD_UNCHANGED are passed.
- Use the IMREAD_UNCHANGED flag to keep the floating point values from PFM image.
- By default number of pixels must be less than 2^30. Limit can be set using system
variable OPENCV_IO_MAX_IMAGE_PIXELS@param filename Name of file to be loaded.
@param flags Flag that can take values of cv::ImreadModes
*/
CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );
- 返回值,Mat 类型, 即返回读取的图像,读取图像失败时返回一个空的矩阵对象(Mat::data == NULL)
- 参数1 filename, 读取的图片文件名,可以使用相对路径或者绝对路径,但必须带完整的文件扩展名(图片格式后缀)
- 参数2 flags, 一个读取标记,用于选择读取图片的方式,默认值为IMREAD_COLOR,flag值的设定与用什么颜色格式读取图片有关,传0是灰度图。
enum ImreadModes {
IMREAD_UNCHANGED = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). Ignore EXIF orientation.
IMREAD_GRAYSCALE = 0, //!< If set, always convert image to the single channel grayscale image (codec internal conversion).
IMREAD_COLOR = 1, //!< If set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTH = 2, //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLOR = 4, //!< If set, the image is read in any possible color format.
IMREAD_LOAD_GDAL = 8, //!< If set, use the gdal driver for loading the image.
IMREAD_REDUCED_GRAYSCALE_2 = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
IMREAD_REDUCED_COLOR_2 = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
IMREAD_REDUCED_GRAYSCALE_4 = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
IMREAD_REDUCED_COLOR_4 = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
IMREAD_REDUCED_GRAYSCALE_8 = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
IMREAD_REDUCED_COLOR_8 = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
IMREAD_IGNORE_ORIENTATION = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
};
c++定义 | python定义 | 说明 |
---|---|---|
IMREAD_UNCHANGED | Python: cv.IMREAD_UNCHANGED | 如果设置,则按原样返回加载的图像(使用Alpha通道,否则会被裁剪) |
IMREAD_GRAYSCALE | Python: cv.IMREAD_GRAYSCALE | 如果设置,则始终将图像转换为单通道灰度图像(编解码器内部转换)。 |
IMREAD_COLOR | Python: cv.IMREAD_COLOR | 如果设置,请始终将图像转换为3通道BGR彩色图像。 |
IMREAD_ANYDEPTH | Python: cv.IMREAD_ANYDEPTH | 如果设置,则在输入具有相应深度时返回16位/ 32位图像,否则将其转换为8位。 |
IMREAD_ANYCOLOR | Python: cv.IMREAD_ANYCOLOR | 如果设置,则以任何可能的颜色格式读取图像。 |
IMREAD_LOAD_GDAL | Python: cv.IMREAD_LOAD_GDAL | 如果设置,使用gdal驱动程序加载图像 |
IMREAD_REDUCED_GRAYSCALE_2 | Python: cv.IMREAD_REDUCED_GRAYSCALE_2 | 如果设置,则始终将图像转换为单通道灰度图像,图像尺寸减小1/2。 |
IMREAD_REDUCED_COLOR_2 | Python: cv.IMREAD_REDUCED_COLOR_2 | 如果设置,则始终将图像转换为3通道BGR彩色图像,图像尺寸减小1/2。 |
IMREAD_REDUCED_GRAYSCALE_4 | Python: cv.IMREAD_REDUCED_GRAYSCALE_4 | 如果设置,则始终将图像转换为单通道灰度图像,图像尺寸减小1/4 |
IMREAD_REDUCED_COLOR_4 | Python: cv.IMREAD_REDUCED_COLOR_4 | 如果设置,则始终将图像转换为3通道BGR彩色图像,图像尺寸减小1/4 |
IMREAD_REDUCED_GRAYSCALE_8 | Python: cv.IMREAD_REDUCED_GRAYSCALE_8 | 如果设置,则始终将图像转换为单通道灰度图像,图像尺寸减小1/8。 |
IMREAD_REDUCED_COLOR_8 | Python: cv.IMREAD_REDUCED_COLOR_8 | 如果设置,则始终将图像转换为3通道BGR彩色图像,图像尺寸减小1/8。 |
IMREAD_IGNORE_ORIENTATION | Python: cv.IMREAD_IGNORE_ORIENTATION | 如果设置,请不要根据EXIF的方向标志旋转图像。 |
函数原型
copyTo()有重载,
inline Mat Mat::clone() const
{
Mat m;
copyTo(m);
return m;
}
opencv矩阵赋值函数copyTo、clone、重载元算赋‘=’之间实现的功能相似均是给不同的矩阵赋值功能。
copyTo和clone函数基本相同,被赋值的矩阵和赋值矩阵之间空间独立,不共享同一空间(深拷贝)。
但是重载元算赋‘=’,被赋值的矩阵和赋值矩阵之间空间共享,改变任一个矩阵的值,会同时影响到另一个矩阵(浅拷贝)。当矩阵作为函数的返回值时其功能和重载元算赋‘=’相同,赋值运算赋会给矩阵空间增加一次计数,所以函数变量返回后函数内部申请的变量空间并不会被撤销,在主函数中仍可以正常使用传递后的参数。