Ask Your Question
1

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

asked 2016-06-01 03:44:23 -0600

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);

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-06-01 04:03:01 -0600

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);
edit flag offensive delete link more

Comments

thanks it works!

choco gravatar imagechoco ( 2016-06-01 04:11:38 -0600 )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 ( 2016-06-01 04:21:34 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-01 03:44:23 -0600

Seen: 7,056 times

Last updated: Jun 01 '16