我正在编写一个小程序,将OpenInventor文件转换为PCD文件。为此,我需要输入两个文件,即OpenInventor文件和JPEG图像。纹理坐标是介于0.0和1.0之间的float值。
我使用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).
最佳答案
您的意思是将其作为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;
}