简体   繁体   English

OpenCV从一组点画一条线

[英]OpenCV Drawing a Line from a set of points

I am trying to draw a line that will link up center points of a bounding box, The points are stored in a vector as the center moves from frame to frame. 我正在尝试画一条线来链接边界框的中心点,随着中心从一帧到另一帧的移动,这些点存储在向量中。

Now I am trying to use a CvLine to linke these points together with a line. 现在,我尝试使用CvLine将这些点与一条线链接在一起。 I am following This Opencv Documentation . 我正在关注此Opencv文档 But CvLine function isynt happy with the parameters I give it. 但是CvLine函数对我给它的参数感到满意。

Here is the code: 这是代码:

vector<Point> Rightarm(20);


vector<Point> Leftarm(20);

    vector<Point>::const_iterator RightIter;
    vector<Point>::const_iterator LeftIter;



   Point center = Point(oko[0].x + (oko[0].width/2), oko[0].y + (oko[0].height/2));
    cout<<"Center Point of Box: 0 is: " <<center<<endl;

    double area = (oko[0].width * oko[0].height);
    cout<<"The Area of Box: 0 is: " <<area<<endl;

    Point center1 = Point(oko[1].x + (oko[1].width/2), oko[1].y + (oko[1].height/2));
    cout<<"Center Point of Box: 1 is: " <<center1<<endl;

    double area1 = (oko[1].width * oko[1].height);
    cout<<"The Area of Box: 1 is: " <<area1<<endl;



Rightarm.push_back(center);
    Leftarm.push_back(center1); 

    if(oko[0].x > oko[1].x)
    {

    }
        else
        {

        }


    for(RightIter = Rightarm.begin(); RightIter != Rightarm.end(); ++RightIter)
    {
        circle(drawing, *RightIter, 3, Scalar(0,0,255), CV_FILLED); 
    }

    if(Rightarm.size() == 20)
        {
            Rightarm.clear();

        }

    for(LeftIter = Leftarm.begin(); LeftIter != Leftarm.end(); ++LeftIter)
    {
        circle(drawing, *LeftIter, 3, Scalar(0,255,0), CV_FILLED);
    }

        if(Rightarm.size() == 20)
        {
            Leftarm.clear();

        }

    cvLine(drawing, center.x, center.y, Scalar(255,255,255),1 ,8 ,CV_AA);

    imshow(window_Input, frame);
    imshow(window_Output, drawing);

Can anyone see where I am going wrong with this...? 谁能看到我在这方面出了错吗?

You are giving wrong arguments and one extra argument to line funciton. 您给线路功能提供了错误的论点和一个额外的论据。 The documentation you pointed to is for Python interface, that too the older one using cv . 您指向的文档是针对Python接口的,也是使用cv的较老版本的文档。 Assuming that you have a recent version of OpenCV it is better if you use the new C++ interface or cv2 interface in Python. 假设您具有最新版本的OpenCV,最好在Python中使用新的C ++接口或cv2接口。

you have to use line function like 你必须使用像

cvLine(
img,       // image to draw on
center,    // starting end point of line segment of type cv::Point
center1,   // other end of line segment
Scalar(0, 255, 0), //green colour
1         // thickness of line
CV_AA     // anti aliased line type
);

documentation is here 文档在这里

Maybe like this: 也许是这样的:

struct centerpoint {
  int x;
  int y;
} center1,center2;

(...) //Define values for centers.

cvLine( drawing, 
        cvPoint(center1.x, center1.y),
        cvPoint(center2.x, center2.y),
        Scalar(255,255,255), 1, 8 , CV_AA);

Don't forget to vote all the answers you like and accept the one that works. 别忘了投票选出您喜欢的所有答案并接受有效的答案。

cvLine在两点之间画线,您应该给它两个cv :: Point,但不能给定center.x nad center.y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM