如何使用 PCL 进行颜色过滤
How to do color filtering with PCL
我正在学习使用pcl.I想过滤掉颜色为红色(rgb 255,0,0)的点云,但不是work.what我应该怎么做?
我使用的 PCL 版本是 1.12.1.
#include <pcl/point_types.h>
#include <pcl/filters/conditional_removal.h>
int main()
{
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_filtered(new
pcl::PointCloud<pcl::PointXYZRGB>);
cloud->width = 5;
cloud->height = 1;
cloud->points.resize((cloud->width) * (cloud->height));
//creat point cloud
for (size_t i = 0; i < cloud->points.size(); ++i)
{
cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cloud->points[i].r = 110;
cloud->points[i].g = 110;
cloud->points[i].b = 110;
}
//set rbg 255,0,0
cloud->points[2].r = 255;cloud->points[2].g = 0;cloud->points[2].b = 0;
pcl::ConditionAnd<pcl::PointXYZRGB>::Ptr range_cond(new pcl::ConditionAnd<pcl::PointXYZRGB>());
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZRGB>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZRGB>("r", pcl::ComparisonOps::EQ, 255)));
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZRGB>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZRGB>("g", pcl::ComparisonOps::EQ, 0)));
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZRGB>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZRGB>("b", pcl::ComparisonOps::EQ, 0)));
pcl::ConditionalRemoval<pcl::PointXYZRGB> condrem;
condrem.setCondition(range_cond);
condrem.setInputCloud(cloud);
condrem.setKeepOrganized(true);
condrem.filter(*cloud_filtered);
std::cerr << "Cloud before filtering: " << std::endl;
for (size_t i = 0; i < cloud->points.size(); ++i)
std::cerr << " " << cloud->points[i].x << " "
<< cloud->points[i].y << " "
<< cloud->points[i].z << " "
<< (int)cloud->points[i].r << " "
<< (int)cloud->points[i].g << " "
<< (int)cloud->points[i].b << std::endl;
std::cerr << "Cloud after filtering: " << std::endl;
for (size_t i = 0; i < cloud_filtered->points.size(); ++i)
std::cerr << " " << cloud_filtered->points[i].x << " "
<< cloud_filtered->points[i].y << " "
<< cloud_filtered->points[i].z << " "
<< (int)cloud_filtered->points[i].r << " "
<< (int)cloud_filtered->points[i].g << " "
<< (int)cloud_filtered->points[i].b << std::endl;
return (0);
}
过滤后的云是None。
结果:
enter image description here
警告消息提示您:“未找到字段!” (三次)。 PointXYZRGB
没有 r
、g
和 b
字段。您可以使用 getFields() 找出点类型具有哪些字段。 PointXYZRGB
有一个可用于过滤的组合字段 rgb
。但是,您可能想考虑使用 PointXYZRGBA
代替(使用字段 rgba
),因为其他点类型使用 float
进行存储(历史原因),我不确定它的效果如何带过滤。
我修改后的代码在这里:
//creat point cloud
for (size_t i = 0; i < cloud->points.size(); ++i)
{
cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cstd::uint32_t rgb = ((std::uint32_t)110 << 16 | (std::uint32_t)110 << 8 | (std::uint32_t)110);
cloud->points[i].rgb = *reinterpret_cast<float*>(&rgb);
}
//set red rbg 255,0,0
std::uint32_t red_rgb = ((std::uint32_t)255 << 16 | (std::uint32_t)0 << 8 | (std::uint32_t)0);
cloud->points[2].rgb = *reinterpret_cast<float*>(&red_rgb);
std::cerr << " FieldsList:" << pcl::getFieldsList(*cloud) <<std::endl;
pcl::ConditionAnd<pcl::PointXYZRGB>::Ptr range_cond(new pcl::ConditionAnd<pcl::PointXYZRGB>());
std::uint32_t rgb_filter = ((std::uint32_t)255 << 16 | (std::uint32_t)0 << 8 | (std::uint32_t)0);
//Add a comparison operator less than (255,0,0) on the rgb field
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZRGB>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZRGB>("rgb", pcl::ComparisonOps::LT, *reinterpret_cast<float*>(&rgb_filter))));
最终结果:
enter image description here
我正在学习使用pcl.I想过滤掉颜色为红色(rgb 255,0,0)的点云,但不是work.what我应该怎么做? 我使用的 PCL 版本是 1.12.1.
#include <pcl/point_types.h>
#include <pcl/filters/conditional_removal.h>
int main()
{
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_filtered(new
pcl::PointCloud<pcl::PointXYZRGB>);
cloud->width = 5;
cloud->height = 1;
cloud->points.resize((cloud->width) * (cloud->height));
//creat point cloud
for (size_t i = 0; i < cloud->points.size(); ++i)
{
cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cloud->points[i].r = 110;
cloud->points[i].g = 110;
cloud->points[i].b = 110;
}
//set rbg 255,0,0
cloud->points[2].r = 255;cloud->points[2].g = 0;cloud->points[2].b = 0;
pcl::ConditionAnd<pcl::PointXYZRGB>::Ptr range_cond(new pcl::ConditionAnd<pcl::PointXYZRGB>());
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZRGB>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZRGB>("r", pcl::ComparisonOps::EQ, 255)));
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZRGB>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZRGB>("g", pcl::ComparisonOps::EQ, 0)));
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZRGB>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZRGB>("b", pcl::ComparisonOps::EQ, 0)));
pcl::ConditionalRemoval<pcl::PointXYZRGB> condrem;
condrem.setCondition(range_cond);
condrem.setInputCloud(cloud);
condrem.setKeepOrganized(true);
condrem.filter(*cloud_filtered);
std::cerr << "Cloud before filtering: " << std::endl;
for (size_t i = 0; i < cloud->points.size(); ++i)
std::cerr << " " << cloud->points[i].x << " "
<< cloud->points[i].y << " "
<< cloud->points[i].z << " "
<< (int)cloud->points[i].r << " "
<< (int)cloud->points[i].g << " "
<< (int)cloud->points[i].b << std::endl;
std::cerr << "Cloud after filtering: " << std::endl;
for (size_t i = 0; i < cloud_filtered->points.size(); ++i)
std::cerr << " " << cloud_filtered->points[i].x << " "
<< cloud_filtered->points[i].y << " "
<< cloud_filtered->points[i].z << " "
<< (int)cloud_filtered->points[i].r << " "
<< (int)cloud_filtered->points[i].g << " "
<< (int)cloud_filtered->points[i].b << std::endl;
return (0);
}
过滤后的云是None。 结果: enter image description here
警告消息提示您:“未找到字段!” (三次)。 PointXYZRGB
没有 r
、g
和 b
字段。您可以使用 getFields() 找出点类型具有哪些字段。 PointXYZRGB
有一个可用于过滤的组合字段 rgb
。但是,您可能想考虑使用 PointXYZRGBA
代替(使用字段 rgba
),因为其他点类型使用 float
进行存储(历史原因),我不确定它的效果如何带过滤。
我修改后的代码在这里:
//creat point cloud
for (size_t i = 0; i < cloud->points.size(); ++i)
{
cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f) / 1000;
cstd::uint32_t rgb = ((std::uint32_t)110 << 16 | (std::uint32_t)110 << 8 | (std::uint32_t)110);
cloud->points[i].rgb = *reinterpret_cast<float*>(&rgb);
}
//set red rbg 255,0,0
std::uint32_t red_rgb = ((std::uint32_t)255 << 16 | (std::uint32_t)0 << 8 | (std::uint32_t)0);
cloud->points[2].rgb = *reinterpret_cast<float*>(&red_rgb);
std::cerr << " FieldsList:" << pcl::getFieldsList(*cloud) <<std::endl;
pcl::ConditionAnd<pcl::PointXYZRGB>::Ptr range_cond(new pcl::ConditionAnd<pcl::PointXYZRGB>());
std::uint32_t rgb_filter = ((std::uint32_t)255 << 16 | (std::uint32_t)0 << 8 | (std::uint32_t)0);
//Add a comparison operator less than (255,0,0) on the rgb field
range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZRGB>::ConstPtr(new pcl::FieldComparison<pcl::PointXYZRGB>("rgb", pcl::ComparisonOps::LT, *reinterpret_cast<float*>(&rgb_filter))));
最终结果: enter image description here