I am using opencv 2.1. In my code I have a few images stored as Mat objects initialized like this:
Mat img1 = imread(\"img/stuff.pgm\", CV_LOAD_IMAGE_GRAYSCA
For C++ basic use:
cv::putText(yourImageMat,
"Here is some text",
cv::Point(5,5), // Coordinates
cv::FONT_HERSHEY_COMPLEX_SMALL, // Font
1.0, // Scale. 2.0 = 2x bigger
cv::Scalar(255,255,255), // BGR Color
1, // Line Thickness (Optional)
cv::CV_AA); // Anti-alias (Optional)
See putText() in OpenCV docs.
If you are using OpenCV 3.x or 4.0, you have to replace CV_AA
by LINE_AA
. So, the complete call would be:
cv::putText(yourImageMat,
"Text to add",
cv::Point(5,5), // Coordinates
cv::FONT_HERSHEY_COMPLEX_SMALL, // Font
1.0, // Scale. 2.0 = 2x bigger
cv::Scalar(255,255,255), // BGR Color
1, // Line Thickness (Optional)
cv::LINE_AA); // Anti-alias (Optional)
This was posted in this forum.
I was looking at the wrong place. I found the answer in the newer OpenCV documentation for cpp. There is a new function called putText() that accepts cv::Mat objects. So I tried this line and it works:
putText(result, "Differencing the two images.", cvPoint(30,30),
FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(200,200,250), 1, CV_AA);
Hope this helps someone.
putText(result, "Differencing the two images.", cvPoint(30,30),
FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(200,200,250), 1, CV_AA);
In the above line "result" should be a cvArr* or an IplImage*. but from the code provided here, I guess you are passing a cv::Mat object. So, you either need to convert it using cvarrToMat() or pass &result instead of result.
Hope it helps
You can also do the following to print text and variables.
std::ostringstream str;
str << "Here is some text:" << myVariable;
cv::putText(image, cv::Point(10,10), str.str(), CV_FONT_HERSHEY_PLAIN, CV_RGB(0,0,250));
putText(img1, "TextString123", cvPoint(50,200), FONT_HERSHEY_SCRIPT_SIMPLEX, 2.5, cvScalar(255,0,0,255), 3, CV_AA);
You can find more information here: http://docs.opencv.org/2.4.9/modules/core/doc/drawing_functions.html
The main diference between this answer and the answers from above is the value of the 7-th parameter, the thickness level. With thickness==1 this function have not worked for me.