Skip to content

Commit 3f13339

Browse files
committed
Merge pull request opencv#17728 from sturkmen72:patch-4
2 parents eb6678e + 2566d13 commit 3f13339

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

modules/imgcodecs/include/opencv2/imgcodecs.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,17 @@ can be saved using this function, with these exceptions:
204204
- PNG images with an alpha channel can be saved using this function. To do this, create
205205
8-bit (or 16-bit) 4-channel image BGRA, where the alpha channel goes last. Fully transparent pixels
206206
should have alpha set to 0, fully opaque pixels should have alpha set to 255/65535 (see the code sample below).
207+
- Multiple images (vector of Mat) can be saved in TIFF format (see the code sample below).
207208
208209
If the format, depth or channel order is different, use
209210
Mat::convertTo and cv::cvtColor to convert it before saving. Or, use the universal FileStorage I/O
210211
functions to save the image to XML or YAML format.
211212
212-
The sample below shows how to create a BGRA image and save it to a PNG file. It also demonstrates how to set custom
213-
compression parameters:
213+
The sample below shows how to create a BGRA image, how to set custom compression parameters and save it to a PNG file.
214+
It also demonstrates how to save multiple images in a TIFF file:
214215
@include snippets/imgcodecs_imwrite.cpp
215216
@param filename Name of the file.
216-
@param img Image to be saved.
217+
@param img (Mat or vector of Mat) Image or Images to be saved.
217218
@param params Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ... .) see cv::ImwriteFlags
218219
*/
219220
CV_EXPORTS_W bool imwrite( const String& filename, InputArray img,

samples/cpp/tutorial_code/snippets/imgcodecs_imwrite.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using namespace cv;
44
using namespace std;
55

6-
static void createAlphaMat(Mat &mat)
6+
static void paintAlphaMat(Mat &mat)
77
{
88
CV_Assert(mat.channels() == 4);
99
for (int i = 0; i < mat.rows; ++i)
@@ -21,9 +21,9 @@ static void createAlphaMat(Mat &mat)
2121

2222
int main()
2323
{
24-
// Create mat with alpha channel
25-
Mat mat(480, 640, CV_8UC4);
26-
createAlphaMat(mat);
24+
Mat mat(480, 640, CV_8UC4); // Create a matrix with alpha channel
25+
paintAlphaMat(mat);
26+
2727
vector<int> compression_params;
2828
compression_params.push_back(IMWRITE_PNG_COMPRESSION);
2929
compression_params.push_back(9);
@@ -37,9 +37,18 @@ int main()
3737
{
3838
fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
3939
}
40+
4041
if (result)
4142
printf("Saved PNG file with alpha data.\n");
4243
else
4344
printf("ERROR: Can't save PNG file.\n");
45+
46+
vector<Mat> imgs;
47+
imgs.push_back(mat);
48+
imgs.push_back(~mat);
49+
imgs.push_back(mat(Rect(0, 0, mat.cols / 2, mat.rows / 2)));
50+
imwrite("test.tiff", imgs);
51+
printf("Multiple files saved in test.tiff\n");
52+
4453
return result ? 0 : 1;
4554
}

0 commit comments

Comments
 (0)