使用putText在视频源中显示计数器值

时间:2015-11-12 18:18:40

标签: c++ opencv

我正试图在相机的视频输入上显示实时计数器值,我使用了putText功能,但它无法正常工作

这是我的代码,我想在视频源上显示blinks_number

while (1)
{

    int taw1 = taw2;
    Mat frame, ycbcr;

    cap >> frame; // get a new frame

    taw2 = calc_taw(frame);

    int delta_taw = taw2 - taw1;

    if (delta_taw >= 5)
    {

        taw_values.push_back(delta_taw);

        blinks_number++;

        cout << "blink number                   " << blinks_number << endl;
        cout << "taw_values   " << taw_values.size() << endl;
        putText(frame, blinks_number, Point(50, 100), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 200, 200), 4);
        imshow("Image", image);


        CvPlot::plot("Delta taw", &taw_values[0], taw_values.size(), 5, 0, 0, 255);
        //showIntGraph("Rotation Angle", &taw_values[0], taw_values.size());
    }


    waitKey(40);

}
return 0;

}

1 个答案:

答案 0 :(得分:0)

putText中的文字必须是字符串。您可以使用std::to_stringstd::stringstream从数字中获取字符串。

这是一个有效的例子:

#include <opencv2/opencv.hpp>
#include <string>
#include <sstream>
using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if (!cap.isOpened())  // check if we succeeded
        return -1;

    int blinks_number = 1;

    for (;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera

        string disp = "Frame # " + std::to_string(blinks_number);

        // Or using stringstream
        //
        //std::stringstream ss;
        //ss << "Frame # " << blinks_number;
        //string disp = ss.str();

        putText(frame, disp, Point(50, 100), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 200, 200), 4);
        ++blinks_number;

        imshow("Frame", frame);
        if (waitKey(1) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}