Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equivalent of c++ std::vector<cv::Vec6f> in java with javacv? #913

Closed
Luca96 opened this issue Feb 13, 2018 · 8 comments
Closed

Equivalent of c++ std::vector<cv::Vec6f> in java with javacv? #913

Luca96 opened this issue Feb 13, 2018 · 8 comments

Comments

@Luca96
Copy link

Luca96 commented Feb 13, 2018

Hi, I'm trying to use the Subdiv2D (from opencv_imgproc) class to perform the Delaunay triangulation.
I need to call the getTriangleList(FloatPointer) method, but it need a FloatPointer object as parameter.
From Opencv documentation, the method getTriangleList need a std::vector<cv::Vec6f> as parameter.
How can i achieve the same in java with javacv?

@saudet
Copy link
Member

saudet commented Feb 13, 2018 via email

@Luca96
Copy link
Author

Luca96 commented Feb 14, 2018

Hi @saudet , here is the code:

cv::Subdiv2D subdiv(rect); //rect is a cv::Rect

// Insert points into subdiv (points is a vector<cv::Point2f>)
for (size_t i = 0; i < points.size(); ++i)
       subdiv.insert(points[i]);

//getting the triangles from subdiv
vector<cv::Vec6f> triangleList;
subdiv.getTriangleList(triangleList);

@saudet
Copy link
Member

saudet commented Feb 14, 2018

Something like this should work then:

FloatPointer triangleList = new FloatPointer(null);
subdiv.getTriangleList(triangleList);
int n = triangleList.limit() / 6;
...

Could you give it a try?

@Luca96
Copy link
Author

Luca96 commented Feb 14, 2018

It seems to work, here is my code:

FloatBuffer buffer = null;
FloatPointer triangleList = new FloatPointer(buffer); 

subdiv2D.getTriangleList(triangleList);
long n = triangleList.limit() * 6;

for (int i = 0; i < n; i++)
      float f = triangleList.get(i); //points coordinate

I only changed few things like:

FloatPointer triangleList = new FloatPointer(null);
//to
FloatPointer triangleList = new FloatPointer(buffer); 
//to avoid: Cannot resolve constructor: FloatPointer(null); ERROR

//and

int n = triangleList.limit() / 6; //number of triangles
//to
long n = triangleList.limit() * 6; //all points coordinate (that is what i need :D )

So, thanks again @saudet for your help!!

@saudet
Copy link
Member

saudet commented Feb 15, 2018

Hum, ok, so the number of points doesn't come out right with that hack. We'll need to do something about that. Thanks for reporting!

@Luca96
Copy link
Author

Luca96 commented Feb 15, 2018

Thank you sir!
But i notice that i can do strange things like this:

 //really strange, upper bound should not exceed triangleList.limit() * 6
long n = triangleList.limit() * 6 * 10; 

//getting coordinate
for (int i = 0; i < n; i += 2) { 
      float x = triangleList.get(i);
      float y = triangleList.get(i + 1);
}

When i >= 0 and i < triangleList.limit() * 6 i get the right coordinate with some strange value like this: (x: -1800.00, y: -1800.00)

But when i >= triangleList.limit() * 6 and i < n , i would expect an error instead i get other values (close to 0) like: (x: 0.000133, y: 0.000145), (x: -0.000192, y: 0.000035)), ... and a lot of zeroes (x: 0.00, y: 0.00)

Note that the right x and y are between 100 and 500.

Is this normal or caused by some memory inconsistencies or some algorithmic bugs of cv::Subdiv2D?

@saudet
Copy link
Member

saudet commented Feb 15, 2018 via email

saudet added a commit to bytedeco/javacpp-presets that referenced this issue Mar 3, 2019
@saudet
Copy link
Member

saudet commented Apr 11, 2019

This has now been fixed with JavaCV 1.5, which provides a nice Vec6fVector wrapper class for this. Thanks for reporting!

@saudet saudet closed this as completed Apr 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants