Update documentation of imwrite()

pull/17728/head
Suleyman TURKMEN 5 years ago committed by sturkmen72
parent 5a7045181a
commit 2566d13100
  1. 7
      modules/imgcodecs/include/opencv2/imgcodecs.hpp
  2. 17
      samples/cpp/tutorial_code/snippets/imgcodecs_imwrite.cpp

@ -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 - 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 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). 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 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 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. 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 The sample below shows how to create a BGRA image, how to set custom compression parameters and save it to a PNG file.
compression parameters: It also demonstrates how to save multiple images in a TIFF file:
@include snippets/imgcodecs_imwrite.cpp @include snippets/imgcodecs_imwrite.cpp
@param filename Name of the file. @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 @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, CV_EXPORTS_W bool imwrite( const String& filename, InputArray img,

@ -3,7 +3,7 @@
using namespace cv; using namespace cv;
using namespace std; using namespace std;
static void createAlphaMat(Mat &mat) static void paintAlphaMat(Mat &mat)
{ {
CV_Assert(mat.channels() == 4); CV_Assert(mat.channels() == 4);
for (int i = 0; i < mat.rows; ++i) for (int i = 0; i < mat.rows; ++i)
@ -21,9 +21,9 @@ static void createAlphaMat(Mat &mat)
int main() int main()
{ {
// Create mat with alpha channel Mat mat(480, 640, CV_8UC4); // Create a matrix with alpha channel
Mat mat(480, 640, CV_8UC4); paintAlphaMat(mat);
createAlphaMat(mat);
vector<int> compression_params; vector<int> compression_params;
compression_params.push_back(IMWRITE_PNG_COMPRESSION); compression_params.push_back(IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9); compression_params.push_back(9);
@ -37,9 +37,18 @@ int main()
{ {
fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what()); fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
} }
if (result) if (result)
printf("Saved PNG file with alpha data.\n"); printf("Saved PNG file with alpha data.\n");
else else
printf("ERROR: Can't save PNG file.\n"); printf("ERROR: Can't save PNG file.\n");
vector<Mat> 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; return result ? 0 : 1;
} }

Loading…
Cancel
Save