翻译自OpenCV 3.1.0 Docs - Image file reading and writing
Enumerations
Imread flags
1 | enum cv::ImreadModes |
Imwrite flags
1 | enum cv::ImwriteFlags |
Imwrite PNG specific flags
这些flags
会修改PNG
图片压缩的方式,并被传递到接下来zlib
处理阶段。
IMWRITE_PNG_STRATEGY_FILTERED
的效果force more Huffman coding and less string matching,处于IMWRITE_PNG_STRATEGY_DEFAULT
和IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY
之间。IMWRITE_PNG_STRATEGY_RLE
的速度几乎和IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY
一样快,但对于PNG
图片有更好的压缩效果。- 策略参数(strategy parameter)只影响压缩率,不影响压缩后数据的正确性。
IMWRITE_PNG_STRATEGY_FIXED
避免dynamic Huffman codes,对特殊的应用允许简单的解码器。
1 | enum cv::ImwritePNGFlags |
Functions
imdecode & imencode
函数cv::imdecode
从内存中的缓存读取图片,如果缓存太短或包含不合法的数据,则返回一个空矩阵,即Mat::data==NULL
。
注意对于彩色图片,颜色通道安装B G R的顺序存储。1
2
3
4Mat cv::imdecode(
InputArray buf, // input array or vector of bytes.
int flags // The same flags as in cv::imread
)
下面是为了方便定义的一个重载函数,区别主要是接收的参数。1
2
3
4
5
6
7Mat cv::imdecode(
InputArray buf,
int flags,
Mat* dst // The optional output placeholder for the decoded matrix.
// It can save the image reallocations when the function is
// called repeatedly for images of the same size.
)
函数cv::imencode
压缩图片并将其存储在合适大小的内存缓冲区,支持的图片格式和flags
描述见函数cv::imwrite
。1
2
3
4
5
6bool cv::imencode(
const String& ext, // File extension that defines the output format.
InputArray img, // Image to be written.
std::vector<uchar>& buf, // Output buffer resized to fit the compressed image.
const std::vector<int>& params = std::vector<int>() // Format-specific parameters. See cv::imwrite and cv::ImwriteFlags.
)
imread & imwrite
函数cv::imread
从给定的文件中读取并返回图片,如果图片不能被正确读取(由于文件丢失,没有读取权限,不支持或不合法的格式等等),函数返回一个空矩阵,即Mat::data==NULL
。1
2
3
4Mat cv::imread(
const String& filename, // Name of file to be loaded.
int flags = IMREAD_COLOR // Flag that can take values of cv::ImreadModes
)
目前支持的格式包括:
- Windows bitmaps - .bmp, .dib (always supported)
- JPEG files - .jpeg, .jpg, *.jpe (see the Notes section)
- JPEG 2000 files - *.jp2 (see the Notes section)
- Portable Network Graphics - *.png (see the Notes section)
- WebP - *.webp (see the Notes section)
- Portable image format - .pbm, .pgm, .ppm .pxm, *.pnm (always supported)
- Sun rasters - .sr, .ras (always supported)
- TIFF files - .tiff, .tif (see the Notes section)
- OpenEXR Image files - *.exr (see the Notes section)
- Radiance HDR - .hdr, .pic (always supported)
- Raster and Vector geospatial data supported by Gdal (see the Notes section)
注意:
- 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.
- On Microsoft Windows OS 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 IMREAD_LOAD_GDAL to load the image, then GDAL driver will be used in order to decode the image by supporting the following formats: Raster, Vector.
函数cv::imreadmulti
从给定的文件读入multi-page image
并存储在一个包含Mat
的std::vector
对象中。1
2
3
4
5bool cv::imreadmulti(
const String & filename, // Name of file to be loaded.
std::vector<Mat> & mats, // Flag that can take values of cv::ImreadModes, default with cv::IMREAD_ANYCOLOR.
int flags = IMREAD_ANYCOLOR // A vector of Mat objects holding each page, if more than one.
)
函数cv::imwrite
将图片保存到给定的文件中,图片格式根据文件的扩展名确定,只有8-bit (or 16-bit unsigned (CV_16U) in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with 'BGR' channel order) images
能够使用这个函数保存。如果格式format
、深度depth
或通道顺序channel order
不是这些,需要在保存前使用Mat::convertTo
和cv::cvtColor
函数将图片转化,或者使用更通用的FileStorage I/O
函数将图片保存为XML
或YAML
格式。
1 | bool cv::imwrite( |
使用这个函数在存储PNG
图片时能够同时保存透明度alpha channel
,要这样做,创建一个8-bit (or 16-bit) 4-channel image BGRA
,完全透明(transparent)的像素的alpha
值为0
,完全不透明(opaque)的像素的alpha
值为255/65535
。
下面的例子展示了怎样来创建这样一个BGRA
图片,并存储为PNG
文件,例子同时说明了怎样设置一个压缩参数。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
using namespace cv;
using namespace std;
void createAlphaMat(Mat &mat)
{
CV_Assert(mat.channels() == 4);
for (int i = 0; i < mat.rows; ++i) {
for (int j = 0; j < mat.cols; ++j) {
Vec4b& bgra = mat.at<Vec4b>(i, j);
bgra[0] = UCHAR_MAX; // Blue
bgra[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // Green
bgra[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // Red
bgra[3] = saturate_cast<uchar>(0.5 * (bgra[1] + bgra[2])); // Alpha
}
}
}
int main(int argv, char **argc)
{
// Create mat with alpha channel
Mat mat(480, 640, CV_8UC4);
createAlphaMat(mat);
vector<int> compression_params;
compression_params.push_back(IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
try {
imwrite("alpha.png", mat, compression_params);
}
catch (cv::Exception& ex) {
fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
return 1;
}
fprintf(stdout, "Saved PNG file with alpha data.\n");
return 0;
}