我一直尝试在 C 中使用 OpenCV 中的 printf 将像素值打印到屏幕,但我只是得到垃圾值。如何将 IPL_DEPTH_8U(8 位无符号整数)图像打印到屏幕并将 IPL_DEPTH_32F(32 位浮点)图像打印到屏幕?下面是我正在使用的代码。是不是错了?它可能是正确的,但我只是得到了我没有预料到的像素值:
顺便说一句,这些是单通道图像:
for (y = 0; y < img->height; y++){
uchar *ptr = (uchar*)(img->imageData + y * img->widthStep);
for (x = 0; x < img->width; x++){
printf("%u, ", ptr[x]);
}
printf("\n");
}
这是针对 IPL_DEPTH_8U 图像的,我怀疑它工作正常。对于 32 个浮点图像,我将指针更改为 float * 并将 printf 中的说明符更改为“%f”,但这似乎不起作用。怎么了?
这些是正确的指针和说明符吗?另外,16 位有符号整数和 32 位有符号浮点数的指针和说明符应该是什么?
谢谢。我刚刚开始使用这个网站,你们太棒了!
I keep trying to print values of pixels to screen using printf in OpenCV in C but I just get garbage values. How do I print IPL_DEPTH_8U (8 bit unsigned integer) images to screen and IPL_DEPTH_32F (32 bit floating point) images to screen? Bellow is the code I am using. Is it wrong? It could be correct, but I just get pixel values I do not anticipate:
these are single channel images btw:
for (y = 0; y < img->height; y++){
uchar *ptr = (uchar*)(img->imageData + y * img->widthStep);
for (x = 0; x < img->width; x++){
printf("%u, ", ptr[x]);
}
printf("\n");
}
That is for IPL_DEPTH_8U images, and I suspect that is working fine. for 32 float images I change the pointer to float * and the specifier in printf to '%f', and that doesn't seem to work. What is wrong?
Are these the correct pointers and specifiers? Also, what are the pointers and specifiers supposed to be for 16 bit signed integers and 32 bit signed floats too?
Thanks. I just started using this website and you guys are awesome!
发布评论
评论(2)
您可以使用
cvGet2D()
操作:您必须从
CvScalar
中取出要打印的值。由于它基本上是double[4]
,所以我认为这不会成为问题。You can use
cvGet2D()
doing:You have to pull out the values you want to print from
CvScalar
. Since it is basicallydouble[4]
, I don't think it will be a problem.查看此 OpenCV 常见问题解答。特别是,请查看“如何访问矩阵元素?”部分。
这是我为正确打印浮点
IplImage
结构而编写的一个小示例:还有
CV_MAT_ELEM(matrix, elemtype, row, col )
宏,仅适用于单 -通道图像,但它本质上是所有指针算术的简写。Check out this OpenCV FAQ. In particular, look at the "How to access matrix elements?" section.
Here is a small sample I wrote for properly printing float
IplImage
structures:There is also the
CV_MAT_ELEM( matrix, elemtype, row, col )
macro that will work only for single-channel images, but it essentially is a short-hand for all the pointer arithmetic.