I have a point cloud in a txt file where each row is of the form:
x y z r g b
How should I read this into an XYZRGB point cloud in PCL? The ascii reader or the pcdreader in PCL expect the format to be of the form x y z rgb where rgb is one value representing r g b channels together.
Is there some way I could read the point clouds of the format I mentioned above without having to modify the point clouds themselves?
EDIT: Adding my current code and some lines from the point cloud in response to a comment
pcl::ASCIIReader ptsReader;
ptsReader.setSepChars(" ");
ptsReader.read(m_pointCloudFilePath,*m_pointCloudRef);
if m_pointCloudRef
is of type: pcl::PointCloud<pcl::PointXYZRGB>::Ptr
this code doesn't work with a runtime error message: Failed to find match for field 'rgb'.
. The same code works if m_pointCloudRef
is of type pcl::PointCloud<pcl::PointXYZ>::Ptr
(which also means that I am working with an ASCII file where each row is x y z
)
The following are the first few lines of the point cloud that I am using:
0.792 9.978 12.769 234 220 209
0.792 9.978 12.768 242 228 217
0.794 9.978 12.771 241 227 214
0.794 9.978 12.770 247 231 218
0.793 9.979 12.769 234 217 207
0.793 9.979 12.768 238 224 213
0.794 9.979 12.767 239 227 215
0.795 9.978 12.772 230 221 206
0.795 9.978 12.771 243 229 216
0.795 9.979 12.770 242 226 213
0.795 9.979 12.769 235 218 208
0.795 9.979 12.768 235 221 210
0.795 9.979 12.767 240 228 216
0.795 9.979 12.766 240 230 218
0.795 9.979 12.765 240 230 218
0.795 9.978 12.763 244 234 222
If you don't want to change how your clouds are saved on disk and only need to work with a very few cloud types, you can just read them manually.
bool loadAsciCloud(std::string filename, pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud)
{
std::cout << "Begin Loading Model" << std::endl;
FILE* f = fopen(filename.c_str(), "r");
if (NULL == f)
{
std::cout << "ERROR: failed to open file: " << filename << endl;
return false;
}
float x, y, z;
char r, g, b;
while (!feof(f))
{
int n_args = fscanf_s(f, "%f %f %f %c %c %c", &x, &y, &z, &r, &g, &b);
if (n_args != 6)
continue;
pcl::PointXYZRGB point;
point.x = x;
point.y = y;
point.z = z;
point.r = r;
point.g = g;
point.b = b;
cloud->push_back(p);
}
fclose(f);
std::cout << "Loaded cloud with " << cloud->size() << " points." << std::endl;
return cloud->size() > 0;
}