简体   繁体   English

从OpenCV图像打印像素值到屏幕

[英]Printing pixel values to screen from an opencv image

I keep trying to print values of pixels to screen using printf in OpenCV in C but I just get garbage values. 我一直尝试在C的OpenCV中使用printf将像素值打印到屏幕上,但我只是得到垃圾值。 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? 如何将IPL_DEPTH_8U(8位无符号整数)图像打印到屏幕上,如何将IPL_DEPTH_32F(32位浮点数)图像打印到屏幕上? 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. 那是为IPL_DEPTH_8U图像,我怀疑它工作正常。 for 32 float images I change the pointer to float * and the specifier in printf to '%f', and that doesn't seem to work. 对于32个浮动图像,我将指针更改为float *,并将printf中的说明符更改为'%f',这似乎不起作用。 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? 另外,用于16位带符号整数和32位带符号浮点数的指针和说明符也应该是什么?

Thanks. 谢谢。 I just started using this website and you guys are awesome! 我刚刚开始使用此网站,你们真棒!

You can use cvGet2D() doing: 您可以使用cvGet2D()执行以下操作:

CvScalar val = cvGet2D(img, y, x);

You have to pull out the values you want to print from CvScalar . 您必须从CvScalar要打印的值。 Since it is basically double[4] , I don't think it will be a problem. 由于它基本上是double[4] ,所以我认为这不是问题。

Check out this OpenCV FAQ. 查看 OpenCV常见问题解答。 In particular, look at the "How to access matrix elements?" 特别是,请看“如何访问矩阵元素?”。 section. 部分。

Here is a small sample I wrote for properly printing float IplImage structures: 这是我写的一个小样本,用于正确打印浮动IplImage结构:

void fillIplImage(IplImage* srcdst)
{
    for(int row = 0; row < srcdst->height; row++)
    {
        for(int col = 0; col < srcdst->width; col++)
        {
            ((float*)(srcdst->imageData + srcdst->widthStep*row))[col] = (float)col;
        }
    }
}

void printIplImage(const IplImage* src)
{
    for(int row = 0; row < src->height; row++)
    {
        for(int col = 0; col < src->width; col++)
        {
            printf("%f, ", ((float*)(src->imageData + src->widthStep*row))[col]);
        }
        printf("\n");
    }
}

int main(int argc, char** argv)
{
    CvSize sz = {10, 10};
    IplImage* test = cvCreateImage(sz, IPL_DEPTH_32F, 1);

    fillIplImage(test);
    printIplImage(test);

    cvReleaseImage(&test);
    return 0;
}

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. 还有CV_MAT_ELEM( matrix, elemtype, row, col )宏仅适用于单通道图像,但它实际上是所有指针算法的简写形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM