我有一个RGB 16位格式的数据(unsigned char *)R:5 G:6 B:5
如何将此数据设置为IplImage格式?
我可以从:
unsigned char* data = ...data...;
IplImage *img = cvCreateImage(cvSize(800,480), IPL_DEPTH_8U, 3); // Should it be 16?
cvSetData(img,data,800*2); // Here is where I am not sure
答案 0 :(得分:2)
您需要将IPL_DEPTH_16U
(单通道)转换为IPL_DEPTH_8U
(三通道)。
下面是我快速编写的一些代码(这应该可行,但我没有565图像方便用它进行测试,所以我会首先尝试一些测试图像...)
#include <opencv2/core/core.hpp>
#include <iostream>
using namespace std;
using namespace cv;
#define RED_MASK 0xF800
#define GREEN_MASK 0x07E0
#define BLUE_MASK 0x001F
int main(int argc, char* argv[])
{
IplImage *rgb565Image = cvCreateImage(cvSize(800, 480), IPL_DEPTH_16U, 1);
IplImage *rgb888Image = cvCreateImage(cvSize(800, 480), IPL_DEPTH_8U, 3);
unsigned short* rgb565Data = (unsigned short*)rgb565Image->imageData;
int rgb565Step = rgb565Image->widthStep / sizeof(unsigned short);
uchar* rgb888Data = (uchar*)rgb888Image->imageData;
float factor5Bit = 255.0 / 31.0;
float factor6Bit = 255.0 / 63.0;
for(int i = 0; i < rgb565Image->height; i++)
{
for(int j = 0; j < rgb565Image->width; j++)
{
unsigned short rgb565 = rgb565Data[i*rgb565Step + j];
uchar r5 = (rgb565 & RED_MASK) >> 11;
uchar g6 = (rgb565 & GREEN_MASK) >> 5;
uchar b5 = (rgb565 & BLUE_MASK);
// round answer to closest intensity in 8-bit space...
uchar r8 = floor((r5 * factor5Bit) + 0.5);
uchar g8 = floor((g6 * factor6Bit) + 0.5);
uchar b8 = floor((b5 * factor5Bit) + 0.5);
rgb888Data[i*rgb888Image->widthStep + j] = r8;
rgb888Data[i*rgb888Image->widthStep + (j + 1)] = g8;
rgb888Data[i*rgb888Image->widthStep + (j + 2)] = b8;
}
}
return 0;
}
你可以使用查找表来加快转换速度,但我所拥有的应该是有益的指导用途。
另外,请查看this SO帖子,以便进一步讨论此主题。