Ask Your Question

Type of lines in LineSegmentDetector

asked 2017-12-05 07:09:17 -0500

Grillteller gravatar image


I am using the LineSegmentDetector ( to find lines in my images. It works really well but I am little bit confused by the documentation. There it says the lines I detect are

"A vector of Vec4i or Vec4f elements specifying the beginning and ending point of a line. Where Vec4i/Vec4f is (x1, y1, x2, y2), point 1 is the start, point 2 - end. Returned lines are strictly oriented depending on the gradient."

But when I use the definition Vec4f lines; I get the error:

Assertion failed (channels() == CV_MAT_CN(dtype)) in cv::Mat::copyTo, file C:\opencv\opencv-master\modules\core\src\copy.cpp, line 259

When I use Mat lines instead it works. Here is my code:

int main(int argc, char** argv)

     Mat img_1,
     img_1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);

      if (! || ! {
          cout << "Error reading image" << endl;
           return EXIT_FAILURE;

      Ptr<LineSegmentDetector> det = createLineSegmentDetector();
      //Vec4f lines;  // Error
      Mat lines // no Error
      det->detect(img_1, lines);

      return 0;

My question is: Why do I get this error and how can I extract the beginning and end of the line out of my Mat? Are every 4 values one line?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-12-05 07:27:34 -0500

berak gravatar image

it detects many lines, so you need a vector<Vec4f> , like:

  Ptr<LineSegmentDetector> det = createLineSegmentDetector();
  vector<Vec4i> lines;
  det->detect(img_1, lines);
  for (size_t i=0; i<lines.size(); i++) {
      Vec4i v = lines[i];
      Point from(v[0], v[1]);
      Point to(v[2], v[3]);
edit flag offensive delete link more


Oh, what a dumb question. Mat is also a vector of vectors. I should have just checked it. Now I see that even in the documentation it says a vector of Vec4f. Thanks anyways :).

Grillteller gravatar imageGrillteller ( 2017-12-05 07:42:04 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-12-05 07:09:17 -0500

Seen: 320 times

Last updated: Dec 05 '17