Ask Your Question
1

possible to convert value of a Point to string? opencv c++

asked Jun 1 '16

choco gravatar image

i want to convert a value of a Point to string, so that i could print the location of that Point on an image.

i am suppose to use this

putText(image, to_string("this is string"), maxLoc,FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(255, 255, 204), 1, CV_AA);

i tried to replace it, and make it to below, and it failed. It work if i just to_string a integer variable

putText(image, to_string(point_variable), maxLoc,FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(255, 255, 204), 1, CV_AA);

Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Jun 1 '16

berak gravatar image

use cv::format

Point p(5,5);
putText(image, format("(%d,%d)", p.x, p.y), maxLoc,FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(255, 255, 204), 1, CV_AA);
Preview: (hide)

Comments

thanks it works!

choco gravatar imagechoco (Jun 1 '16)edit
1

Just want to add a standard way of converting everything except a string into a string in C++, is done by using the stringstream operator. A small example in this case:

Point p(5,5);
stringstream temp;
temp << "(" << p.x << "," << p.y << ")";
putText(image, temp.str(), maxLoc,FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(255, 255, 204), 1, CV_AA);

This workaround of manually writing each element to the stringstream is not necessary for every OpenCV object out there. Some of them, like mats, can be directly written using the << operator. It all depends if it is implemented for that object.

StevenPuttemans gravatar imageStevenPuttemans (Jun 1 '16)edit

Question Tools

1 follower

Stats

Asked: Jun 1 '16

Seen: 7,157 times

Last updated: Jun 01 '16