如何在opencv视频中添加圆圈/点

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

本文关键字:添加 opencv 视频      更新时间:2023-10-16

我有一个带有x和y坐标的逗号分隔值文件。我需要在来自相机的opencv视频上放置一个点/圆圈,并使用逗号分隔值文件中的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);