1

I am working on opencv with c++
I got a reference of code written in python (reference)
I just want to know how to write this line in c++

sys.stdout.write( frame.tostring() )

Please specify the header if needed


HELP

I got the conversion of python code to c++ BUT
But Its popping out me an error saying
I am using Opencv2.3

error: ‘struct _IplImage’ has no member named ‘tostring’
Community
  • 1
  • 1
Wazy
  • 8,822
  • 10
  • 53
  • 98
  • I suggest you read more about the basics of C++ before trying something more complicated like OpenCV. For example, you might want to check out the `std::cout` object... – Some programmer dude Jan 27 '12 at 06:23
  • @JoachimPileborg Thanks for the suggestion...I appreciate that but I am already working on opencv for last 3 months.....I made a good progress but had only explored c++ up to the extend i needed it in opencv... Its very urgent though....Please help me out with a way bro.... – Wazy Jan 27 '12 at 06:26
  • About your second problem, maybe it's called `ToString()` or `toString()`. Use your IDE's autocomplete function to find out the `_IplImage` method that converts it to string. – Hossein Jan 27 '12 at 08:40
  • I had checked the documentation for both Opencv 2.3 and Opencv 2.1....It was there present in Opencv 2.1 as tostring but not in Opencv 2.3...How do I write my own.... http://opencv.willowgarage.com/documentation/python/core_basic_structures.html#iplimage http://opencv.itseez.com/modules/core/doc/old_basic_structures.html#iplimage – Wazy Jan 27 '12 at 08:49

1 Answers1

2

Writing to the standard output (which is what sys.stdout.write does in Python) is generally done via the std::cout stream and the << operator:

std::cout << frame.tostring();

Include the <iostream> header. The cout object is a descendant of std::ostream, so you can do all the things with it that the documentation says you can do with an ostream.


You can also use C-style I/O. The printf and puts functions (in the <cstdio> header) will write a string to standard output:

std::printf("%s", frame.tostring().c_str());
std::puts(frame.tostring().c_str()); // also writes '\n' afterward

Here I'm assuming your tostring function returns a std::string; the C-style functions only print null-terminated char pointers, not string, so I call c_str. The << operator, on the other hand, is overloaded to handle most of the common C++ types, so you can print them without calling extra functions.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • Thank you very much....Great response...But when I am using it in Opencv I am getting such error....`error: request for member ‘tostring’ in ‘frame’, which is of non-class type ‘IplImage*’` – Wazy Jan 27 '12 at 06:47
  • I know nothing about OpenCV. I answered the question title, which was about how to read Python. I assumed since you've been using OpenCV for three months already that you'd know how to do that part. Apparently, you shouldn't be using `IplImage*` directly. [Instead, use `cv::Mat`.](http://stackoverflow.com/a/5192612/33732) Then you can [use the `<<` syntax](http://stackoverflow.com/a/7971568/33732) like I described. – Rob Kennedy Jan 27 '12 at 07:50