Calculation formula of image span stride in Opencv (EmguCV)

In EmguCV, if there is a 3-channel image Image<Bgr,byte>, the width of the image is Width=21, and the height is Height=10. According to the calculation method found on the Internet:

Formula 1:

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

        It is found that the stride is 64, then the total number of bytes should be stride*Height =640; but the actual size of the Bytes of the image is 720, which is inconsistent! Then the calculation method of stride may be:

Formula 2:

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

        In this way, the stride is 72, and the Bytes calculation is exactly 720. This is inconsistent with the stride calculation method mentioned on the Internet. What is the reason? I don't understand!

        For some images, the calculation of formula 1 happens to be correct. For example, when the width is 11 and the height is 10, the actual Bytes size of the image in opencv is 360, which is consistent with the result of formula 1. Formula 2 is also 360.

     So, what is the value of Bytes in opencv?

     After research, it is found that the stride of Bgr in opencv is in units of 12 bits, so the calculation should follow
if (stride % 12 != 0) stride += 12 - stride % 12, which is consistent with the above formula 2 of. At present, my solution is not to use bytes, but to use bytes to intptr, and use scan0 to build images, so there is no problem.


 

Guess you like

Origin blog.csdn.net/whf227/article/details/121752409