openCV是一个很好很强大的开源计算机视觉库,实现了图像处理和计算机视觉方面的很多通用算法,可谓是图像视频开
发者手中的利器。可是openCV也有个让人有点不爽的地方,对图像的数据格式要求太严了点,大部分函数都要求要同样
数据类型才能操作,且如果有两个8位的图像相乘这样的操作,则必然超出数据范围,如相除操作则会被强制四舍五入
,而且我找遍整个openCV也沒看到有图像数据格式转换的函数,于是自己写两个转换函数吧:
void myCopy8to16(const IplImage*src, IplImage*dest)//把8位的图像转化成16位
{
if(src->width!=dest->width || src->height!=dest->height)
{
return;
}
dest->origin=src->origin;
for(int i=0;i<src->height;i++)
{
for(int j=0;j<src->width;j++)
{
((short *)(dest->imageData+i*dest->widthStep))[j*dest->nChannels+0]=((uchar *)
(src->imageData+i*src->widthStep))[j*src->nChannels+0];
((short *)(dest->imageData+i*dest->widthStep))[j*dest->nChannels+1]=((uchar *)
(src->imageData+i*src->widthStep))[j*src->nChannels+1];
((short *)(dest->imageData+i*dest->widthStep))[j*dest->nChannels+2]=((uchar *)
(src->imageData+i*src->widthStep))[j*src->nChannels+2];
}
}
}
void myCopy16to8(const IplImage*src, IplImage*dest)//把16位的图像转化成8位
{
if(src->width!=dest->width || src->height!=dest->height)
{
return;
}
dest->origin=src->origin;
for(int i=0;i<src->height;i++)
{
for(int j=0;j<src->width;j++)
{
((uchar *)(dest->imageData+i*dest->widthStep))[j*dest->nChannels+0]=((short *)
(src->imageData+i*src->widthStep))[j*src->nChannels+0];
((uchar *)(dest->imageData+i*dest->widthStep))[j*dest->nChannels+1]=((short *)
(src->imageData+i*src->widthStep))[j*src->nChannels+1];
((uchar *)(dest->imageData+i*dest->widthStep))[j*dest->nChannels+2]=((short *)
(src->imageData+i*src->widthStep))[j*src->nChannels+2];
}
}
}
写的比较简单,运算效率也不太高,但总算能用。凑合着用下吧。
沒有写异常捕捉,出错后果自负哈。
PS:谁找到有更好的办法别忘了告诉我下。
PS2:只用于位数转换,并未拉伸数据,而且,转回8位时是直接去掉了高8位。。。。。。。丢失数据,后果自负哈。