OpenCV casting of vector.data to Point* res

2019-08-23 09:00发布

In a OpenCV C++ program I have a function with this body.

  for (int ii=0; ii< static_cast<int>(parks.size()); ii++)
  {
     PolygonVertices temp = parks.at(ii).getPoly();
     const Point *pts = (const cv::Point*) Mat(parks.at(ii).getPoly()).data;
     int npts = Mat(parks.at(ii).getPoly()).rows;
     for (int jj=0; jj<npts; jj++)
     {
         cout<<"----"<<jj<<"----"<<endl;
         cout<<"x: "<<pts[jj].x<<", y: "<<pts[jj].y<<endl;
         cout<<"x: "<<temp[jj].x<<", y: "<<temp[jj].y<<endl;
         cout<<"--------"<<endl;
     }
  }
}

Polygonvertices is a type defined as typedef std::vector<cv::Point> and the method getPoly() returns such a structure.

I am casting the Polygonvertices (which is a vector of Point) data to a pointer of Point type.

The points are selected with the mouse.

The problem is that printing the x and y coordinates of both the element of the pointer and the original element, for the first element (index 0) data coming from the pointer are shifted.

For example this is the output:

----0----
x: -1871319456, y: 21975
x: 286, y: 304
--------
----1----
x: 325, y: 218
x: 325, y: 218
--------
----2----
x: 375, y: 280
x: 375, y: 280

As you can see the first element is totally shifted away?

What is happening? Is the casting wrong?

This is happening using OpenCV4 under Ubuntu 18.04.

0条回答
登录 后发表回答