# I want to draw a line of points

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 close merge delete

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

Sort by » oldest newest most voted

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);)

more

Official site

GitHub

Wiki

Documentation