OpenCV: Image file reading and writing

您所在的位置:网站首页 imagefunction OpenCV: Image file reading and writing

OpenCV: Image file reading and writing

#OpenCV: Image file reading and writing| 来源: 网络整理| 查看: 265

#include

Saves an image to a specified file.

The function imwrite saves the image to the specified file. The image format is chosen based on the filename extension (see cv::imread for the list of extensions). In general, only 8-bit unsigned (CV_8U) single-channel or 3-channel (with 'BGR' channel order) images can be saved using this function, with these exceptions:

With OpenEXR encoder, only 32-bit float (CV_32F) images can be saved. 8-bit unsigned (CV_8U) images are not supported. With Radiance HDR encoder, non 64-bit float (CV_64F) images can be saved. All images will be converted to 32-bit float (CV_32F). With JPEG 2000 encoder, 8-bit unsigned (CV_8U) and 16-bit unsigned (CV_16U) images can be saved. With PAM encoder, 8-bit unsigned (CV_8U) and 16-bit unsigned (CV_16U) images can be saved. With PNG encoder, 8-bit unsigned (CV_8U) and 16-bit unsigned (CV_16U) images can be saved. PNG images with an alpha channel can be saved using this function. To do this, create 8-bit (or 16-bit) 4-channel image BGRA, where the alpha channel goes last. Fully transparent pixels should have alpha set to 0, fully opaque pixels should have alpha set to 255/65535 (see the code sample below). With PGM/PPM encoder, 8-bit unsigned (CV_8U) and 16-bit unsigned (CV_16U) images can be saved. With TIFF encoder, 8-bit unsigned (CV_8U), 16-bit unsigned (CV_16U), 32-bit float (CV_32F) and 64-bit float (CV_64F) images can be saved. Multiple images (vector of Mat) can be saved in TIFF format (see the code sample below). 32-bit float 3-channel (CV_32FC3) TIFF images will be saved using the LogLuv high dynamic range encoding (4 bytes per pixel)

If the image format is not supported, the image will be converted to 8-bit unsigned (CV_8U) and saved that way.

If the format, depth or channel order is different, use Mat::convertTo and cv::cvtColor to convert it before saving. Or, use the universal FileStorage I/O functions to save the image to XML or YAML format.

The sample below shows how to create a BGRA image, how to set custom compression parameters and save it to a PNG file. It also demonstrates how to save multiple images in a TIFF file:

#include using namespace cv;using namespace std;static void paintAlphaMat(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(i, j); bgra[0] = UCHAR_MAX; // Blue bgra[1] = saturate_cast((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // Green bgra[2] = saturate_cast((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // Red bgra[3] = saturate_cast(0.5 * (bgra[1] + bgra[2])); // Alpha } }}int main(){ Mat mat(480, 640, CV_8UC4); // Create a matrix with alpha channel paintAlphaMat(mat); vector compression_params; compression_params.push_back(IMWRITE_PNG_COMPRESSION); compression_params.push_back(9); bool result = false; try { result = imwrite("alpha.png", mat, compression_params); } catch (const cv::Exception& ex) { fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what()); } if (result) printf("Saved PNG file with alpha data.\n"); else printf("ERROR: Can't save PNG file.\n"); vector imgs; imgs.push_back(mat); imgs.push_back(~mat); imgs.push_back(mat(Rect(0, 0, mat.cols / 2, mat.rows / 2))); imwrite("test.tiff", imgs); printf("Multiple files saved in test.tiff\n"); return result ? 0 : 1;} Parameters filenameName of the file. img(Mat or vector of Mat) Image or Images to be saved. paramsFormat-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ... .) see cv::ImwriteFlags Examples: samples/cpp/image_alignment.cpp, samples/cpp/stitching.cpp, samples/cpp/stitching_detailed.cpp, samples/cpp/tutorial_code/photo/seamless_cloning/cloning_demo.cpp, samples/tapi/hog.cpp, and samples/tapi/squares.cpp.


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3