2015-11-06 65 views
0

得到RGB值我有一個點雲。我想獲得它的RGB值。我怎樣才能做到這一點?
爲了讓我的問題更清楚,請參閱代碼。如何從點雲

// Load the first input file into a PointCloud<T> with an appropriate type : 
     pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud1 (new pcl::PointCloud<pcl::PointXYZRGB>); 
     if (pcl::io::loadPCDFile<pcl::PointXYZRGB> ("../data/station1.pcd", *cloud1) == -1) 
     { 
      std::cout << "Error reading PCD file !!!" << std::endl; 
      exit(-1); 
     } 

我想每個單獨的值

std::cout << " x = " << cloud1->points[11].x << std::endl; 
std::cout << " y = " << cloud1->points[11].y << std::endl; 
std::cout << " z = " << cloud1->points[11].z << std::endl; 
std::cout << " r = " << cloud1->points[11].r << std::endl; 
std::cout << " g = " << cloud1->points[11].g << std::endl; 
std::cout << " b = " << cloud1->points[11].b << std::endl; 

但是結果是我得到類似的東西:

x = 2.33672 
y = 3.8102 
z = 8.86153 
r = � 
g = w 
b = � 

回答

3

From the point cloud docs

一個點結構,代表歐幾里得XYZ座標和RGB顏色。

由於歷史的原因(PCL首先被開發作爲ROS包),則RGB信息被打包到一個整數,並澆鑄到浮子。這是我們希望在不久的將來,除去一些東西,但在此期間,下面的代碼片段應該幫你收拾,並在您PointXYZRGB結構解壓RGB顏色:

// pack r/g/b into rgb 
uint8_t r = 255, g = 0, b = 0; // Example: Red color 
uint32_t rgb = ((uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b); 
p.rgb = *reinterpret_cast<float*>(&rgb); 

要解壓數據轉換成單獨的值,使用:

PointXYZRGB p; 
// unpack rgb into r/g/b 
uint32_t rgb = *reinterpret_cast<int*>(&p.rgb); 
uint8_t r = (rgb >> 16) & 0x0000ff; 
uint8_t g = (rgb >> 8) & 0x0000ff; 
uint8_t b = (rgb)  & 0x0000ff; 

或者,從1.1.0開始,您可以直接使用pr,pg和pb。

在文件point_types.hpp559行定義。

+0

謝謝@Kevin;但我仍然得到同樣的錯誤;這是新的代碼: 'uint32_t的RGB = *的reinterpret_cast (cloud1->點[11] .rgb); uint8_t r =(rgb >> 16)&0x0000ff; uint8_t g =(rgb >> 8)&0x0000ff; uint8_t b =(rgb)&0x0000ff; std :: cout <<「x =」<< cloud1-> points [11] .x << std :: endl; std :: cout <<「y =」<< cloud1-> points [11] .y << std :: endl; std :: cout <<「z =」<< cloud1-> points [11] .z << std :: endl; std :: cout <<「r =」<< r << std :: endl; std :: cout <<「g =」<< g << std :: endl; std :: cout <<「b =」<< b << std :: endl; ' – lotfishtaine

+0

我成功解決了!實際上R,G和B值是unsigned char類型,因此我們需要將它們轉換爲int來查看它們的數值。 我嘗試:'性病::法院<< 「R =」 << INT(cloud1->點[11] .R)<<的std :: ENDL;'和它的作品 – lotfishtaine

+0

作出這樣的(INT)cloud1->點[11] .r雖然;)您現在調用int的構造函數(創建一個新變量並將其返回到輸出與cout),而不是將值轉換爲int類型。 – Kevin