最近在封装opencv中一些标定,视觉方面的函数,接口定义为byte*,需要将byte*与iplImage*相互转换。
其中遇到几个小问题,1)当byte*转成IplImage*时setData()接受图像头,故需要createImageHeader().在上篇cvSetData()中已经说明。
2)在IplImage*转成Byte*时,其实就是取IplImage*中的imageData数据区指针,但是要注意几点:
A:IplImage*指针存放图像有两种方式,由originar变量控制,0-left up为起点,1-left down 为起点。所以要根据origial 处理图像情况。看是否需要对图像进行倒置下。
B:IplImage*存放图像根据读人的格式不同,channel不同。即便是8位图像可能存放成3个通道,所以在转化时获取数据需要注意。
代码贴出来:
//////////////////////////////////////////////////////////////////////////
// Name: byte2IplImg
// Function: convert byte* to IplImage format.
// Author: liyy
// Data:2011.05.23
//////////////////////////////////////////////////////////////////////////
IplImage * CconvertImgStruct::Byte2IplImg( byte *pImg,int width,int height,int bitCount )
{
if (!pImg )
{
return NULL;
}
int nchannel = 3; //create IplImage in 3channel.
IplImage *pIplImgHeader = cvCreateImageHeader(cvSize(width,height),IPL_DEPTH_8U,nchannel);
long lWidthByte = (width * bitCount+31)/32 *4;
pIplImgHeader->origin = 1; //from left-down
cvSetData(pIplImgHeader,pImg,lWidthByte*nchannel);
//return IplImg
return pIplImgHeader;
}
//////////////////////////////////////////////////////////////////////////
//Name: IplImage2Byte
//Function: convert IplImage to byte*
//Author:liyy
//Date: 2011.05.23
//////////////////////////////////////////////////////////////////////////
byte * CconvertImgStruct::IplImage2Byte( IplImage *IplImg )
{
if (!IplImg)
{
return NULL;
}
int nHeight = IplImg->height;
int nWidth =IplImg->width;
int bitCount = IplImg->depth;
int nChanel = IplImg->nChannels;
byte *pImg = new byte[nHeight*nWidth*nChanel*sizeof(byte)];
if (IplImg->origin == 0)
{ //from left-down
for (int i = 0; i< nHeight;i++)
{
for (int j = 0; j< nWidth;j++)
{
for (int k =0; k < nChanel;k++)
{
//*(pImg+i*nWidth+j*nChanel+k) = *(IplImg->imageData+(nHeight-i-1)*nWidth+j*nChanel+k);
pImg[i*nWidth*nChanel+j*nChanel+k] = IplImg->imageData[(nHeight-i-1)*nWidth*nChanel+j*nChanel+k];
}
}
}
}
else
{ //from left-up
memcpy(pImg,IplImg->imageData,nHeight*nWidth*nChanel*sizeof(byte));
}
//pImg = (byte *)IplImg->imageData;
return pImg;
}
}
//////////////////////////////////////////////////////////////////////////
//Name: IplImage2Byte
//Function: convert IplImage to byte*
//Author:liyy
//Date: 2011.05.23
//////////////////////////////////////////////////////////////////////////
byte * CconvertImgStruct::IplImage2Byte( IplImage *IplImg )
{
if (!IplImg)
{
return NULL;
}
int nHeight = IplImg->height;
int nWidth =IplImg->width;
int bitCount = IplImg->depth;
int nChanel = IplImg->nChannels;
byte *pImg = new byte[nHeight*nWidth*nChanel*sizeof(byte)];
if (IplImg->origin == 0)
{ //from left-down
for (int i = 0; i< nHeight;i++)
{
for (int j = 0; j< nWidth;j++)
{
for (int k =0; k < nChanel;k++)
{
//*(pImg+i*nWidth+j*nChanel+k) = *(IplImg->imageData+(nHeight-i-1)*nWidth+j*nChanel+k);
pImg[i*nWidth*nChanel+j*nChanel+k] = IplImg->imageData[(nHeight-i-1)*nWidth*nChanel+j*nChanel+k];
}
}
}
}
else
{ //from left-up
memcpy(pImg,IplImg->imageData,nHeight*nWidth*nChanel*sizeof(byte));
}
//pImg = (byte *)IplImg->imageData;
return pImg;
}