Ask Your Question
3

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

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

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
7

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

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
2

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

Michael Burdinov gravatar image

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

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

Comments

@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 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 15,480 times

Last updated: Sep 23 '13