如何将 circle/dot 添加到 opencv 视频?

How to add a circle/dot to a opencv video?

我有一个包含 x 和 y 坐标的逗号分隔值文件。我需要在来自摄像机的 opencv 视频上放置一个 dot/circle 并使用来自逗号分隔值文件的 x y 坐标进行跟踪。我有显示相机的代码:

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
    VideoCapture cap(0); // open the video camera no. 0

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video cam" << endl;
        return -1;
    }

    double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
    double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

    cout << "Frame size : " << dWidth << " x " << dHeight << endl;

    namedWindow("CameraDisplay",CV_WINDOW_AUTOSIZE); //create a window called "CameraDisplay"

    while (1)
    {
        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
             cout << "Cannot read a frame from video stream" << endl;
             break;
        }

        imshow("CameraDisplay", frame); //show the frame in "CameraDisplay" window

        if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
        {
            cout << "esc key is pressed by user" << endl;
            break; 
        }
    }
    return 0;    
}

您的网络摄像头捕捉到的帧只不过是一张图像。因此,您可以像处理单张图片一样在网络摄像头视频上画一个圆圈。

以下是openCV在你的图片上放置圆圈的方法:

void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

例如:

circle(frame, Point2i(x-coordinate, y-coordinate), 5, Scalar(0,125,230), 4, 3);