我正在写一个小程序,将一个OpenInventor文件转换为PCD文件。为此,我在输入中输入了两个文件,即OpenInventor文件和JPEG图像。纹理坐标是介于0.0和1.0之间的浮点值。
我使用OpenCV提取RGB值并以十进制格式返回它,但是下面的函数似乎不能正常工作……
float get_color(cv::Mat img, float x, float y) {
int i = x*img.cols;
int j = y*img.rows;
unsigned char R = img.ptr<unsigned char>(j)[3*i];
unsigned char G = img.ptr<unsigned char>(j)[3*i+1];
unsigned char B = img.ptr<unsigned char>(j)[3*i+2];
return R*pow(16,4) +
G*pow(16,2) +
B;
}我用以下命令加载图像
cv::imread("filename.jpg", CV_LOAD_IMAGE_COLOR).发布于 2012-07-23 20:53:37
我在PCL头"point_types.hpp“(PCL版本:1.5.1)中的评论中找到了我自己问题的答案:
由于历史原因(PCL最初是作为ROS包开发的),
信息被打包成一个整数并转换为一个浮点数。这是我们希望在不久的将来删除的东西,但同时,以下代码片段应该可以帮助您在PointXYZRGB结构中打包和解包RGB颜色:
uint8_t r= 255,g= 0,b= 0;uint32_t rgb = ((uint32_t)r << 16 | (uint32_t)g << 8| (uint32_t)b);p.rgb = *reinterpret_cast(&rgb);
经过重构和一些其他错误修复后,该函数变成了:
float get_color(cv::Mat img, float s, float t){
int j = (1.0 - s)*(float)img.cols;
int i = (1.0 - t)*(float)img.rows;
uint8_t R = img.at<cv::Vec3b>(i,j)[2];
uint8_t G = img.at<cv::Vec3b>(i,j)[1];
uint8_t B = img.at<cv::Vec3b>(i,j)[0];
uint32_t rgb_32 = ((uint32_t)R << 16 | (uint32_t)G << 8 | (uint32_t)B);
return *reinterpret_cast<float*>(&rgb_32);
}发布于 2012-07-23 03:09:22
是否要将其作为32位整数返回?
unsigned int get_color(cv::Mat img, float x, float y)
{
int i = x*img.cols;
int j = y*img.rows;
unsigned char R = img.ptr<unsigned char>(j)[3*i];
unsigned char G = img.ptr<unsigned char>(j)[3*i+1];
unsigned char B = img.ptr<unsigned char>(j)[3*i+2];
return (R << 16) |
(G << 8) |
B;
}或者,您可能希望将其作为浮点数返回,在这种情况下,您需要执行以下操作
struct FloatColour
{
float r;
float g;
float b;
};
float get_color(cv::Mat img, float x, float y)
{
int i = x*img.cols;
int j = y*img.rows;
unsigned char R = img.ptr<unsigned char>(j)[3*i];
unsigned char G = img.ptr<unsigned char>(j)[3*i+1];
unsigned char B = img.ptr<unsigned char>(j)[3*i+2];
FloatColour retCol;
retCol.r = R / 255.0f;
retCol.g = G / 255.0f;
retCol.b = B / 255.0f;
return retCol;
}https://stackoverflow.com/questions/11603091
复制相似问题