将 opencv 图像中的像素值打印到屏幕

发布于 2024-12-28 09:33:08 字数 618 浏览 0 评论 0 原文

我一直尝试在 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2025-01-04 09:33:08

您可以使用 cvGet2D() 操作:

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

您必须从 CvScalar 中取出要打印的值。由于它基本上是double[4],所以我认为这不会成为问题。

You can use cvGet2D() doing:

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

You have to pull out the values you want to print from CvScalar. Since it is basically double[4], I don't think it will be a problem.

猥︴琐丶欲为 2025-01-04 09:33:08

查看 OpenCV 常见问题解答。特别是,请查看“如何访问矩阵元素?”部分。

这是我为正确打印浮点 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;
}

还有 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文