How to write a vector<cv::Mat> to a single file? [closed]

asked 2019-11-05 11:50:58 -0500

Soumya gravatar image

Hello, I found a pull request https://github.com/opencv/opencv/pull.... Which states that "imwrite for multipage images implemented"

But I am unable to find the function that does so.

I used cv::imreadmulti() to read a stacked tiff file (one file with multiple images). Then did some processing on them. Now, I have a vector<cv::mat> where each Mat is a Tiff image.

How do I write them to one file which would be similar to the file I read using imreadmulti()?

Thank you

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2019-11-05 16:56:06.079552

Comments

berak gravatar imageberak ( 2019-11-05 12:05:53 -0500 )edit

Hi, I tried imwrite but could not get it to work. Could you please give me an example

Soumya gravatar imageSoumya ( 2019-11-05 13:40:56 -0500 )edit

This is what I tried:

std::vector<cv::mat> xy_stack; cv::imreadmulti(input_file, xy_stack);
cv::imwrite("file.tiff", xy_stack);

It does not work

Error:

OpenCV Error: Assertion failed (0 <= i && i < (int)v.size()) in cv::_InputArray::getMat_, file C:\build\master_winpack-build-win64-vc14\opencv\modules\core\src\matrix.cpp, line 1253

Soumya gravatar imageSoumya ( 2019-11-05 13:45:59 -0500 )edit

i tested like below and see no problem with your code

vector<Mat> imgs;
Mat img = imread(argv[1]);

split(img, imgs);
imwrite("test.tiff", imgs);

std::vector<cv::Mat> xy_stack;
cv::imreadmulti("test.tiff", xy_stack);
cv::imwrite("file.tiff", xy_stack);
return 0;
sturkmen gravatar imagesturkmen ( 2019-11-05 14:56:33 -0500 )edit

My images are: cv::Mat polar(iPolarRows, iPolarColumns, CV_8UC1)

Does the type have anything to do with the problem? I tried 16U/16S/8S/8U all give me same error OpenCV Error: Assertion failed (0 <= i && i < (int)v.size()) in cv::_InputArray::getMat_, file C:\build\master_winpack-build-win64-vc14\opencv\modules\core\src\matrix.cpp, line 1253

Soumya gravatar imageSoumya ( 2019-11-05 15:02:18 -0500 )edit

Hi Strukmen

I tried running your code using a tif file i had

int main(int argc, char *argv[]) {

// Change path to input pullback here
const char* input_file = "D:\\CALCIUM DATA\\Pullbacks\\DMS2-40-PRE.oct\\50.tif";
std::cout << "Input File Path = " << input_file << std::endl;

std::vector<Mat> imgs;
Mat img = imread(input_file);

split(img, imgs);
imwrite("test.tiff", imgs); // Error here

std::vector<cv::Mat> xy_stack;
cv::imreadmulti("test.tiff", xy_stack);
cv::imwrite("file.tiff", xy_stack);
return 0;

}

And I get the same error at the first imwrite

OpenCV Error: Assertion failed (0 <= i && i < (int)v.size()) in cv::_InputArray::getMat_, file C:\build\master_winpack-build-win64-vc14\opencv\modules\core\src\matrix.cpp, line 1253

Soumya gravatar imageSoumya ( 2019-11-05 15:07:32 -0500 )edit

Emailed 50.tif, kindly check

Soumya gravatar imageSoumya ( 2019-11-05 15:13:43 -0500 )edit

works flawlessly

sturkmen gravatar imagesturkmen ( 2019-11-05 15:21:15 -0500 )edit

what about when you try to show images

// Change path to input pullback here
const char* input_file = "D:\\CALCIUM DATA\\Pullbacks\\DMS2-40-PRE.oct\\50.tif";
std::cout << "Input File Path = " << input_file << std::endl;

std::vector<Mat> imgs;
Mat img = imread(input_file);

split(img, imgs);
imshow("test0", imgs[0]);
imshow("test1", imgs[1]);
imshow("test2", imgs[2]);
    waitKey();
imwrite("test.tiff", imgs); // Error here

std::vector<cv::Mat> xy_stack;
cv::imreadmulti("test.tiff", xy_stack);
cv::imwrite("file.tiff", xy_stack);
return 0;
sturkmen gravatar imagesturkmen ( 2019-11-05 15:25:20 -0500 )edit

What could be the issue then?

Soumya gravatar imageSoumya ( 2019-11-05 15:26:21 -0500 )edit