C语言 如何保存 IplImage?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5410167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:08:20  来源:igfitidea点击:

How to save an IplImage?

copencv

提问by vapo

When we have an IplImage, how can we save it so we can use it later, or view it as an image outside of our code (such as via png, or jpeg)?

当我们有一个IplImage 时,我们如何保存它以便我们以后使用它,或者在我们的代码之外查看它(例如通过 png 或 jpeg)?

As a code example, I have the following:

作为代码示例,我有以下内容:

void SaveImage()
{
    CvSize size;
    IplImage *rgb_img;
    int i = 0;

    size.height = HEIGHT;
    size.width = WIDTH;

    rgb_img = cvCreateImageHeader(size, IPL_DEPTH_8U, 3);
    rgb_img->imageData = my_device.ColorBuffer;
    rgb_img->imageDataOrigin = rgb_img->imageData;

    /*for (i = 2; i < rgb_img->imageSize; i+= 3)
    {
        // confirming all values print correctly
        printf("%d, ", rgb_img->imageData[i]);
    }*/

    cvSaveImage("foo.png",rgb_img);
}

I have printed out all of the values in the commented out for loop, and it seems like the data is in the buffer correctly. Using cvShowImage to display the image also works correctly, so it seems like the structure of the image is fine.

我已经打印出注释掉的 for 循环中的所有值,并且数据似乎正确地位于缓冲区中。使用 cvShowImage 显示图像也可以正常工作,所以看起来图像的结构很好。

采纳答案by Dat Chu

void SaveImage()
{
    CvSize size;
    IplImage *rgb_img;
    int i = 0;

    size.height = HEIGHT;
    size.width = WIDTH;

    rgb_img = cvCreateImageHeader(size, IPL_DEPTH_8U, 3);
    rgb_img->imageData = my_device.ColorBuffer;

    // You should NOT have the line below or OpenCV will try to deallocate your data
    //rgb_img->imageDataOrigin = rgb_img->imageData;

    for (i = 0; i < size.height; i++)
    {
        for (j = 0;j < size.width; j++)
        {
        // confirming all values print correctly
        printf("%c, ", rgb_img->imageData[i*width + j]);
        }
    }

    cvSaveImage("foo.png",rgb_img);
}

Running this should not crash.

运行它不应该崩溃。

Some problems with your code

您的代码存在一些问题

  • You use %f to print but IPL_DEPTH_8U is 1-byte uchar
  • 您使用 %f 打印但 IPL_DEPTH_8U 是 1 字节 uchar

回答by n00dle

To save:

保存:

cvSaveImage(outFileName,img)

If you wanted to check it had saved, you could do the following:

如果您想检查它是否已保存,您可以执行以下操作:

  if(!cvSaveImage(outFileName,img)) printf("Could not save: %s\n",outFileName);

Taken from http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html#SECTION00052000000000000000- top result on Google for "opencv write iplimage".

取自http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html#SECTION00052000000000000000- Google 上“opencv write iplimage”的最高结果。