Can't access data from color video_frame C++
| Required Info | |
|---|---|
| Camera Model | D435i |
| Firmware Version | 2.32.1 |
| Operating System & Version | Linux |
| Kernel Version (Linux Only) | 4.14.168-1-MANJARO |
| Platform | PC |
| SDK Version | 2.0 latest |
| Language | C++ |
| Segment | AR |
Issue Description
I am attempting to get the RGB color out of texture_coordinates from a pointcloud...
Here is what I have with my code so far:
typedef struct RGB {
RGB(uint8_t r, uint8_t g, uint8_t b) {
this->r = r;
this->g = g;
this->b = b;
}
uint8_t r;
uint8_t g;
uint8_t b;
} RGB
void someFunction(rs2::frame depth_frame, rs2::frame color_frame) {
depth_frame.get_data();
rs2::pointcloud pointcloud;
pointcloud.map_to(color_frame);
rs2::points points = pointcloud.calculate(depth_frame);
auto pc_vertices = points.get_vertices();
auto tex_coords = points.get_texture_coordinates();
const int tex_width = color_frame.as<rs2::video_frame>().get_width();
const int tex_height = color_frame.as<rs2::video_frame>().get_height();
auto stride = color_frame.as<rs2::video_frame>().get_stride_in_bytes();
auto tex_ptr = (uint8_t *)color_frame.get_data();
std::vector<float3> vertices;
std::vector<RGB> colors;
for (unsigned int i = 0; i < points.size(); i++) {
if (vertices[i].z) {
vertices.push_back(float3(pc_vertices[i].x, pc_vertices[i].y, pc_vertices[i].z));
// get the corresponding pixel's rgb value to send
int px = rs2::clamp(tex_coords[i].u * tex_width, 0, tex_width);
int py = rs2::clamp(tex_coords[i].v * tex_height, 0, tex_height);
auto r = tex_ptr[3 * (px * stride + py)];
auto g = tex_ptr[3 * (px * stride + py) + 1];
auto b = tex_ptr[3 * (px * stride + py) + 2];
colors.push_back(RGB(r, g, b));
}
}
// do stuff
}
Now getting the vertex data works fine!
It is just the line
colors.push_back(RGB(r,g,b));
that causes a crash!
If I comment it out, no worries.... AND if I change the line to something like
colors.push_back(RGB(255,255,255));
It also works fine... I just have a white point cloud.
So I'm thinking that it has something to do with the way that I'm accessing the texture.
But I really have no idea what I am doing wrong here... I have been racking my brain for the whole day on this one.
Any help would be much appreciated!
Thanks a lot.
Not to worry!
I was able to work it out based on the function here at line 66: https://github.com/IntelRealSense/librealsense/blob/master/wrappers/pcl/pcl-color/rs-pcl-color.cpp
Not sure what I was doing wrong, but this function works!