Ask Your Question

How to convert cv::Mat to std::string?

asked 2012-06-18 22:55:40 -0500

I would like to store cv::Mat in std::string and read it back. Is there an easy way to do this using OpenCV?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-06-26 11:09:02 -0500

Kirill Kornyakov gravatar image

Yes, OpenCV 2.4.1 adds such functionality, you can check ChangeLog for the sample code.

BTW, for debugging purposes you can use something like this:

cout << myMatrix << endl;
edit flag offensive delete link more

answered 2013-09-23 02:19:52 -0500

Michael Burdinov gravatar image

updated 2013-09-23 05:47:22 -0500

For creating string from Mat you should use iterators:

string matAsString (src.begin<unsigned char>(), src.end<unsigned char>());

This way you will get all the relevant content of src even if it is not continuous.

edit flag offensive delete link more


@Michael Burdinov Your method works for gray scale images, how can I use this for 3 channel BGR images?

abhijit gravatar imageabhijit ( 2018-03-31 02:50:12 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2012-06-18 22:55:40 -0500

Seen: 8,546 times

Last updated: Sep 23 '13