OpenCV casting of vector<Point>.data to Point* resulting in an unexpected behaviour

自闭症网瘾萝莉.ら 提交于 2019-12-13 03:24:20

问题


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.

来源:https://stackoverflow.com/questions/52628920/opencv-casting-of-vectorpoint-data-to-point-resulting-in-an-unexpected-behavi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!