Ask Your Question
0

I want to draw a line of points

asked 2017-08-31 02:32:08 -0600

rezaee gravatar image

updated 2017-08-31 02:52:05 -0600

I am newbie in OpenCV, so I don't know can I ask this kind of question here or not? I like to draw a sequence of points as a line on OpenCV3.3.0. I think I can collect this sequence by "vector<point>" although I don't know is this the best idea or not? Lets consider I have these points vector:

 vector<Point> vec = { Point(0,0),Point(10,10),Point(20,20), Point(30,30), Point(40,40), Point(50,50) };

And like to draw them in an empty Mat as an 50*50 pixel image. So it would be the images diameter. But I don't know how can I do this? I searched and found the "line" function, but it has only to "starting and end" points. should I use many line functions in sequence? there isn't better solution for this?

I tried to do it by this code but it thrown in an exception:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

void MyLine(Mat img, Point start, Point end);
Mat im = Mat::zeros(100, 100, CV_8UC1);
Mat img = Mat::zeros(100, 100, CV_8UC1);
vector<Point> vec = { Point(0,0),Point(10,10),Point(20,20), Point(30,30), Point(40,40), Point(50,50) };

    int main()
    {

        Point start= Point(0, 0);
        Point end = Point(50, 50);

        MyLine(img, start, end);
        namedWindow("image", WINDOW_AUTOSIZE);
        imshow("image", im);
        waitKey(0);
        return 0;
    }

    void MyLine(Mat img, Point start, Point end)
    {
        int thickness = 2;
        int lineType = 8;
        for (int i = 0; i < 7; i++)
        {
            line(img,
                vec[i],
                vec[i+1],
                Scalar(255, 255, 255),
                thickness,
                lineType);
            im += img;

        }
    }
edit retag flag offensive close merge delete

Comments

Direct initialisation of vectors is only possible in C++11 right? So did you enable that flag during build?

StevenPuttemans gravatar imageStevenPuttemans ( 2017-08-31 03:41:51 -0600 )edit

Also please provide the error thrown :D

StevenPuttemans gravatar imageStevenPuttemans ( 2017-08-31 03:42:19 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2017-08-31 04:04:47 -0600

berak gravatar image

updated 2017-08-31 04:07:29 -0600

your point vec has 6 items, so the largest valid index is 5, and your loop should be:

  for (int i = 0; i < 5; i++)  // 6-1 because of vec[i+1]

also:

 im += img; // this should be removed ! (and use imshow("image", img);)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-08-31 02:32:08 -0600

Seen: 2,523 times

Last updated: Aug 31 '17