Ask Your Question
0

How do I cast Point2f to Point2d?

asked 2015-08-09 16:48:14 -0600

saihv gravatar image

I am trying to copy a vector of Point2f's into a vector of Point2d's. I am unable to directly cast the Point2f elements to Point2d, as in:

  for (int i = 0 ; i < (int)vector1.size() ; i++)
  {
    vector2.push_back((cv::Point2d)vector1[i]);
  }

The usual std way does not work either.

Example: std::vector<cv::Point2d> vector2(vector1.begin(), vector1.end());
edit retag flag offensive close merge delete

Comments

I don't undestand your problem. Ihave try your example it works for me. What is your configuration opencv, pc ide..?

LBerger gravatar imageLBerger ( 2015-08-10 02:16:53 -0600 )edit

That's strange. I am using 2.4.11 on linux with cmake. It says "Call of overloaded Point_(cv::Point_<float>&) is ambiguous."

Either way, treating it as a Mat worked for me: cv::Mat(vector1).convertTo(vector2, cv::Mat(vector2).type())

saihv gravatar imagesaihv ( 2015-08-10 03:11:19 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-08-10 02:25:56 -0600

If that code does not work, which actually should work like @LBerger pointed out, you could try to break it down a bit more, like the following code.

vector<Point2f> vector1;
vector<Point2d> vector2;
for (size_t i=0 ; i<vector1.size(); i++)
{
    vector2.push_back( cv::Point2d( (double)vector1[i].x, (double)vector1[i].y  ) );
}

Which works just fine over here also.

edit flag offensive delete link more

Comments

Thanks: I will give that a try just to check what's going on. Treating the vector as a Mat worked for me, I wasn't able to answer my own question though.

 cv::Mat(vector1).convertTo(vector2, cv::Mat(vector2).type())
saihv gravatar imagesaihv ( 2015-08-10 03:11:58 -0600 )edit

Why would you want to do this? This is a giant overhead of parsing each vector into a new mat format? ...

StevenPuttemans gravatar imageStevenPuttemans ( 2015-08-10 03:44:40 -0600 )edit
1

Just checked: your version does work! Agreed: the Mat way is terribly inefficient. Still not sure why considering the whole Point2f doesn't work but casting individual elements works.

saihv gravatar imagesaihv ( 2015-08-10 03:49:29 -0600 )edit

It is possible that casting for OpenCV specific types is not enabled ... @LBerger, you did nothing special or extra to get that to work? Should dig deeper in this.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-08-10 04:04:33 -0600 )edit

My results : [10, 0; 5, 1; 3.333333333333334, 2] [10, 0; 5, 1; 3.3333333, 2] [10, 0; 5, 1; 3.3333333, 2]

with this program so

    vector<Point2d> vd;
    vector<Point2f> vf;
    for (int i=0;i<3;i++)
        vd.push_back(Point2d(10.0/(i+1),i));
    for (int i = 0; i < vd.size(); i++)
    {
        Point2f p=vd[i];
        vf.push_back(vd[i]);
    }
       cout<<vd<<"\n";
       cout<<vf<<"\n";
std::vector<cv::Point2f> vector2(vd.begin(), vd.end());
cout << vector2 << "\n";

May be I miss something (VS2013 windows 10 64bits)

LBerger gravatar imageLBerger ( 2015-08-10 05:12:02 -0600 )edit

Oh thats implicit type conversion, by forcing one type into the other. That is different than explicit typecasting like he suggested, which does not work --> (cv::Point2d)vector1[i])

StevenPuttemans gravatar imageStevenPuttemans ( 2015-08-10 06:09:10 -0600 )edit

I have made this test too and it's OK

for (int i = 0; i < vd.size(); i++)
{
    Point2f p=vd[i];
    vf.push_back((cv::Point2f)vd[i]);
}

in debug mode I can see this call opencv2\core\types.hpp line 1089 and for this test too :

for (int i = 0; i < vd.size(); i++)
{
    Point2f p=vd[i];
    vf.push_back(vd[i]);
}
LBerger gravatar imageLBerger ( 2015-08-10 06:17:53 -0600 )edit

That only works because you have the line Point2f p=vd[i];. Basically OpenCV uses smart pointers and there you redefine the pointer interface to the item in question. Then ofcourse the pushing will work. Could you test if

for (int i = 0; i < vd.size(); i++)
{
    vf.push_back((Point2f)vd[i]);
}

works. I am guessing it does not.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-08-10 06:23:42 -0600 )edit

My test is now

vector<Point2d> vd;
vector<Point2f> vf;
for (int i=0;i<3;i++)
    vd.push_back(Point2d(10.0/(i+1),i));
for (int i = 0; i < vd.size(); i++)
{
    vf.push_back(vd[i]);
}
cout<<"vd ="<<vd<<"\n";
cout<<"vf = "<<vf<<"\n";
std::vector<cv::Point2f> vector2(vd.begin(), vd.end());
cout <<"vector 2="<< vector2 << "\n";

Results :

vd =[10, 0;
 5, 1;
 3.333333333333334, 2]
vf = [10, 0;
 5, 1;
 3.3333333, 2]
vector 2=[10, 0;
 5, 1;
 3.3333333, 2]
LBerger gravatar imageLBerger ( 2015-08-10 06:29:04 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-08-09 16:48:14 -0600

Seen: 19,587 times

Last updated: Aug 10 '15