Ask Your Question
0

Why the function point to Point2f don't work noramlly

asked 2017-12-28 11:16:03 -0600

yode gravatar image

updated 2017-12-28 11:22:13 -0600

Cross post here


This is my custom function:

#include<opencv.hpp>
using namespace std;
using namespace cv;

Point2f* fun() {
    vector<Point2f> pts = { Point2f(1,6),Point2f(5,5.1),Point2f(0.2,6),Point2f(4.4,0.6) };
    RotatedRect rote_rect = minAreaRect(pts);
    Point2f fourpts[4];
    rote_rect.points(fourpts);
    cout << "real points:" << endl;
    for (int i = 0; i < 4; i++)
    {
        cout << fourpts[i] << endl;
    }
    return fourpts;
}

int main() {

    Point2f *p = fun();
    cout << endl;
    cout << "result points:" << endl;
    for (int i = 0; i < 4; i++)
        cout << p[i] << endl;
        return 0;
}

But why I will get an incongruous output?

real points:
[2.75462, 7.98692]
[0.2, 6]
[4.4, 0.599999]
[6.95462, 2.58692]

result points:
[2.27281e-29, 5.50583e-39]
[5.50838e-39, 5.50838e-39]
[2.24849e-29, 0]
[2.22333e-29, 2.09709e-38]

As you see, the result points is not same to the real points. Is it a bug of Opencv or I have missed something?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2017-12-28 11:23:20 -0600

Tetragramm gravatar image

You need to study your C++ a bit more.

Point2f fourpts[4];//declares a local array.  Will be destroyed when it goes out of scope.
return fourpts;//Returns a pointer to the local array.  When the array is destroyed, the pointer now points to bad memory.
edit flag offensive delete link more

Comments

Thanks very much.. I know the reason and fixed it now.

yode gravatar imageyode ( 2017-12-28 11:31:22 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-12-28 11:16:03 -0600

Seen: 216 times

Last updated: Dec 28 '17