Accessing IplImage element of type IPL_DEPTH_16S in OpenCV
I know that for a single channel byte image you do:
((uchar *)(img->imageData + i*img->widthStep))[j]
and for a single channel float image, you do:
((float*)(img->imageData + i*img->widthStep))[j]
But how about for 16 b开发者_StackOverflow社区it signed images (IPL_DEPTH_16S), I tried:
((short*)(img->imageData + i*img->widthStep))[j]
and
((signed int*)(img->imageData + i*img->widthStep))[j]
to no avail.
Thanks,
Actually, the short works fine if you take widthStep/2...
The answer is :
(((short*)(img->imageData)) + i*img->widthStep)[j]
And the reason is explained by this example:
#include <stdio.h>
int main(){
char * pointer;
printf("%zu \n", sizeof(char));
printf("%zu \n", sizeof(signed short));
printf("%zu \n", sizeof(signed int));
printf("%zu \n", sizeof(float));
printf("%p \n",((char*)(pointer) + 10 * 5));
printf("%p \n",((signed short*)(pointer)) + 10 * 5);
printf("%p\n",(((signed int*)(pointer)) + 10 * 5));
printf("%p\n",((float*)(pointer)) + 10 * 5);
}
1
2
4
4
0x7fff5fc01084
0x7fff5fc010b6
0x7fff5fc0111a
0x7fff5fc0111a
精彩评论