From 2566d131005091446a206436332791e350ec85a5 Mon Sep 17 00:00:00 2001 From: Suleyman TURKMEN Date: Wed, 1 Jul 2020 23:50:09 +0300 Subject: [PATCH] Update documentation of imwrite() --- modules/imgcodecs/include/opencv2/imgcodecs.hpp | 7 ++++--- .../snippets/imgcodecs_imwrite.cpp | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/modules/imgcodecs/include/opencv2/imgcodecs.hpp b/modules/imgcodecs/include/opencv2/imgcodecs.hpp index 319f6cbd97..e1f8208e0b 100644 --- a/modules/imgcodecs/include/opencv2/imgcodecs.hpp +++ b/modules/imgcodecs/include/opencv2/imgcodecs.hpp @@ -204,16 +204,17 @@ can be saved using this function, with these exceptions: - 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). +- Multiple images (vector of Mat) can be saved in TIFF format (see the code sample below). 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 and save it to a PNG file. It also demonstrates how to set custom -compression parameters: +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 snippets/imgcodecs_imwrite.cpp @param filename Name of the file. -@param img Image to be saved. +@param img (Mat or vector of Mat) Image or Images to be saved. @param params Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ... .) see cv::ImwriteFlags */ CV_EXPORTS_W bool imwrite( const String& filename, InputArray img, diff --git a/samples/cpp/tutorial_code/snippets/imgcodecs_imwrite.cpp b/samples/cpp/tutorial_code/snippets/imgcodecs_imwrite.cpp index 10f4151694..ad9ee6d6b2 100644 --- a/samples/cpp/tutorial_code/snippets/imgcodecs_imwrite.cpp +++ b/samples/cpp/tutorial_code/snippets/imgcodecs_imwrite.cpp @@ -3,7 +3,7 @@ using namespace cv; using namespace std; -static void createAlphaMat(Mat &mat) +static void paintAlphaMat(Mat &mat) { CV_Assert(mat.channels() == 4); for (int i = 0; i < mat.rows; ++i) @@ -21,9 +21,9 @@ static void createAlphaMat(Mat &mat) int main() { - // Create mat with alpha channel - Mat mat(480, 640, CV_8UC4); - createAlphaMat(mat); + 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); @@ -37,9 +37,18 @@ int main() { 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; }