ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

The problem is this line here:

float pt_3D_data = (float) pt_3D.data;

pt_3D.data is a pointer to the start of the data your casting this to a float storing it in a float and then trying to use it as a pointer. This is all over the place and I'm surprised it compiles tbh.

It should be:

float *pt_3D_data = pt_3D.data;

The problem is this line here:

float pt_3D_data = (float) pt_3D.data;

pt_3D.data is a pointer to the start of the data your casting this to a float storing it in a float and then trying to use it as a pointer. This is all over the place and I'm surprised it compiles tbh.

It should be:

float *pt_3D_data = pt_3D.data;

EDIT: Okay that's updated now.

I really recommend you use the cv::Mat::at methods to access and update the elements of the cv::Mat object this is exactly what they're for, accessing the data directly and casting pointers all over the place is going to cause you a world of pain.

The data property of the Mat object has a type of uchar* so when you index it with an lvalue using [2] you're adding two bytes to it not (2 * sizeof(float)), you're then casting a non-aligned pointer to a float* which if nothing else if going to give you garbage values. All in all stick to the at methods unless performance is really important to you.

The problem is this line here:

float pt_3D_data = (float) pt_3D.data;

pt_3D.data is a pointer to the start of the data your casting this to a float storing it in a float and then trying to use it as a pointer. This is all over the place and I'm surprised it compiles tbh.

It should be:

float *pt_3D_data = pt_3D.data;

EDIT: Okay that's updated now.

I really recommend you use the cv::Mat::at methods to access and update the elements of the cv::Mat object this is exactly what they're for, accessing the data directly and casting pointers all over the place is going to cause you a world of pain.

The data property of the Mat object has a type of uchar* so when you index it with an lvalue using [2] you're adding two bytes to it not (2 * sizeof(float)), you're then casting a non-aligned pointer to a float* which if nothing else if going to give you garbage values. All in all stick to the at methods unless performance is really important to you.

UPDATE: If you really need to access the data directly fair enough, but get the code working correctly with at methods first then optimise the code from this point where you know it's working.

Having said that. The code (float)pt_2D.data[2] seems to be attempting to get the 3rd element of a 2 element vector! Which you would expect to crash you code, you seem to have copy and pasted the code to print out the 3D vector without removing the last element.