How to overlay text on image when working with cv::Mat type

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.


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.


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