两个openCV补充函数。用于不同位数图像之间转换的。。

 

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位。。。。。。。丢失数据,后果自负哈。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值