Opencv(EmguCV)中图像跨度stride的计算公式

在EmguCV中假如有一3通道图像Image<Bgr,byte> image宽为Width=21 ,高为Height=10,根据网上的查到的计算方法:

公式1:

int stride = width * 3;  
if (stride % 4 != 0)
    stride += 4 - stride % 4; 

        得出stride为64,那么总字节数应为stride*Height =640;但是image的Bytes实际大小为720,不一致!那么stride的计算方法可能是:

公式2:

int stride = Width * 3;
if (stride% 4 != 0)
    stride += (4 - Width % 4) * 3;

        这样stride为72,Bytes计算正好是720,这一点与网上说的stride计算方法不一致,原因是什么?搞不懂!

        对于有些图像,公式1的计算碰巧又是对的,比如当宽度为11,高度为10时,opencv中image实际Bytes大小为360,与公式1的结果一致。公式2也为360.

     所以,opencv中的Bytes到底是什么值?

     经过研究发现,opencv中的Bgr的话,其stride是以12位一个单位的,所以计算应该按照
if (stride % 12 != 0)   stride += 12 - stride % 12,这和上面的公式2是一致的。目前我的解决办法是不用bytes,而改用bytes转为intptr,通过scan0来构建图像,这样也没有问题。


 

猜你喜欢

转载自blog.csdn.net/whf227/article/details/121752409
今日推荐