4

Why can't I pass

    Point src[1][4] = {
        {
            Point(border,border), Point(border,h-border), Point(w-border,h-border), Point(w-border,h-border)
        }
    };

as

polylines(frame, src, ns, 1, true, CV_RGB(255,255,255), 2); 

where

polylines has prototype

void polylines(Mat& img, const Point** pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

UPDATE

Okay, okay, I remembered this stuff :)

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385

4 Answers4

4

Point[1][4] can't be converted to Point** this way.
Have a look at this question: Converting multidimensional arrays to pointers in c++

The actual solution here is to use STL container instead of C-style array, e.g. std::vector:

typedef std::vector<Point> Shape;
std::vector<Shape> myShapes;
...

and pass it by const reference instead of const pointer:

void polylines(Mat& img, const std::vector<Shape>& shapes, ...)

Also note that src, ns, pts, etc. aren't very lucky names for your variables. Try to choose more meaningful names... if not for your own sake, then do it for the sake of future readers of that code :)

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
2

Because they're not the same thing. polylines asks for a pointer to pointers to Point, but you're giving it a pointer to array[4] of Point.

Rephrased: When you use src as an expression like this, it decays from an array into a pointer. In this case it will decay from Point[1][4] into Point(*)[4], i.e. a pointer to an array of size 4 of type Point. This is the memory location of the 4 Point objects. polylines expects the memory location of a pointer to a Point object.

Per Johansson
  • 6,697
  • 27
  • 34
1

Array-of-arrays and pointer-to-pointers do not imply the same memory layout. An array-of-array contains all of its elements contiguously, whereas a pointer-to-pointers requires an array of contiguous pointers which point to (possibly non-contiguous) arrays of contiguous elements.

To do that conversion, you would need to allocate an array of pointers and have them point to the correct entries in your array-of-arrays. In your case, since you have a single set of values, it's even simpler:

Point * pointer = &src[0][0];
int npts = 4;
polylines(img, &pointer, &npts, 1, ...);

Assuming ncontours is the number of pointers to which the pointer-to-pointer points and npts is the number of points to which the individual pointers point.

Trillian
  • 6,207
  • 1
  • 26
  • 36
0

Please check this question: casting char[][] to char** causes segfault?

Short answer: You should never do that

Community
  • 1
  • 1
Manu343726
  • 13,969
  • 4
  • 40
  • 75