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

后端 未结 7 1006
广开言路
广开言路 2020-12-29 21:57

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         


        
相关标签:
7条回答
  • 2020-12-29 22:01

    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.

    0 讨论(0)
  • 2020-12-29 22:02

    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.

    0 讨论(0)
  • 2020-12-29 22:05

    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.

    0 讨论(0)
  • 2020-12-29 22:21
    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

    0 讨论(0)
  • 2020-12-29 22:22

    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));
    
    0 讨论(0)
  • 2020-12-29 22:23

    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.

    0 讨论(0)
提交回复
热议问题