【问题标题】:How to save an image in Intel RealSense(Visual C++)如何在英特尔实感(Visual C++)中保存图像
【发布时间】:2015-11-17 09:23:15
【问题描述】:

我正在开发英特尔实感 SDK (R2)。我想从 Camera_viewer 保存图像。我一直在工作,直到将帧保存到特定的缓冲区并从中检索。我想知道如何将这些帧/图像保存到指定的位置/文件夹。

这是我的代码:

PXCImage *colorIm, *depthIm;
for (int i=0; i<MAX_FRAMES; i++) {

    // This function blocks until all streams are ready (depth and color)
    // if false streams will be unaligned
    if (psm->AcquireFrame(true)<PXC_STATUS_NO_ERROR) break; 

    // retrieve all available image samples
    PXCCapture::Sample *sample = psm->QuerySample();

    // retrieve the image or frame by type from the sample
    colorIm = sample->color;
    depthIm = sample->depth;

    // render the frame
    if (!renderColor->RenderFrame(colorIm)) break;
    if (!renderDepth->RenderFrame(depthIm)) break;

    // release or unlock the current frame to fetch the next frame
    psm->ReleaseFrame();
}

我能够成功检索帧/图像,但我想保存这些文件以供进一步使用。所以我想知道如何将这些文件保存在文件夹中。

提前致谢

【问题讨论】:

  • 你能解决你的问题吗?
  • stackoverflow.com/questions/32351213/… 上有这个问题的答案。简而言之:使用PXCImage::AcquireAccess() 获取图像数据,然后使用该数据设置(例如)Gdiplus::Bitmap 实例,并将其保存到磁盘。

标签: c++ visual-studio-2012 realsense


【解决方案1】:

here 提出了同样的问题,发布的答案解决了最初的问题。但是有一个关于如何将图像保存到特定文件夹的后续问题。

如果您有那个特定的问题,那么答案将是相同的 SetFileName()。根据this linkpxcCHAR *file is the full path of the file to playback or to be recorded.。话虽如此,您可以创建一个自定义文件夹并将您的路径指向该custom folder,后跟一个有效的文件名来保存您的图像。

【讨论】:

  • 如何将外部图像(BMP、JPG、PNG 等)加载到 PXCImage 中?
【解决方案2】:

为了保存数据,我会按照 ThorngardSO 所说的从图像数据创建一个位图,并使用来自http://www.runicsoft.com/bmp.cpp 的代码来保存它 -

#include <windows.h>
#include <stdio.h>       // for memset

bool SaveBMP ( BYTE* Buffer, int width, int height, long paddedsize, LPCTSTR bmpfile )
{
    // declare bmp structures 
    BITMAPFILEHEADER bmfh;
    BITMAPINFOHEADER info;

    // andinitialize them to zero
    memset ( &bmfh, 0, sizeof (BITMAPFILEHEADER ) );
    memset ( &info, 0, sizeof (BITMAPINFOHEADER ) );

    // fill the fileheader with data
    bmfh.bfType = 0x4d42;       // 0x4d42 = 'BM'
    bmfh.bfReserved1 = 0;
    bmfh.bfReserved2 = 0;
    bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + paddedsize;
    bmfh.bfOffBits = 0x36;      // number of bytes to start of bitmap bits

    // fill the infoheader

    info.biSize = sizeof(BITMAPINFOHEADER);
    info.biWidth = width;
    info.biHeight = height;
    info.biPlanes = 1;          // we only have one bitplane
    info.biBitCount = 24;       // RGB mode is 24 bits
    info.biCompression = BI_RGB;    
    info.biSizeImage = 0;       // can be 0 for 24 bit images
    info.biXPelsPerMeter = 0x0ec4;     // paint and PSP use this values
    info.biYPelsPerMeter = 0x0ec4;     
    info.biClrUsed = 0;         // we are in RGB mode and have no palette
    info.biClrImportant = 0;    // all colors are important

    // now we open the file to write to
    HANDLE file = CreateFile ( bmpfile , GENERIC_WRITE, FILE_SHARE_READ,
         NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
    if ( file == NULL )
    {
        CloseHandle ( file );
        return false;
    }

    // write file header
    unsigned long bwritten;
    if ( WriteFile ( file, &bmfh, sizeof ( BITMAPFILEHEADER ), &bwritten, NULL ) == false )
    {   
        CloseHandle ( file );
        return false;
    }
    // write infoheader
    if ( WriteFile ( file, &info, sizeof ( BITMAPINFOHEADER ), &bwritten, NULL ) == false )
    {   
        CloseHandle ( file );
        return false;
    }
    // write image data
    if ( WriteFile ( file, Buffer, paddedsize, &bwritten, NULL ) == false )
    {   
        CloseHandle ( file );
        return false;
    }

    // and close file
    CloseHandle ( file );

    return true;
}

这可以随后使用来自同一链接的更多代码加载 -

/*******************************************************************
BYTE* LoadBMP ( int* width, int* height, long* size 
        LPCTSTR bmpfile )

The function loads a 24 bit bitmap from bmpfile, 
stores it's width and height in the supplied variables
and the whole size of the data (padded) in <size>
and returns a buffer of the image data 

On error the return value is NULL. 

  NOTE: make sure you [] delete the returned array at end of 
        program!!!
*******************************************************************/

BYTE* LoadBMP ( int* width, int* height, long* size, LPCTSTR bmpfile )
{
    // declare bitmap structures
    BITMAPFILEHEADER bmpheader;
    BITMAPINFOHEADER bmpinfo;
    // value to be used in ReadFile funcs
    DWORD bytesread;
    // open file to read from
    HANDLE file = CreateFile ( bmpfile , GENERIC_READ, FILE_SHARE_READ,
         NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL );
    if ( NULL == file )
        return NULL; // coudn't open file


    // read file header
    if ( ReadFile ( file, &bmpheader, sizeof ( BITMAPFILEHEADER ), &bytesread, NULL ) == false )
    {
        CloseHandle ( file );
        return NULL;
    }

    //read bitmap info

    if ( ReadFile ( file, &bmpinfo, sizeof ( BITMAPINFOHEADER ), &bytesread, NULL ) == false )
    {
        CloseHandle ( file );
        return NULL;
    }

    // check if file is actually a bmp
    if ( bmpheader.bfType != 'MB' )
    {
        CloseHandle ( file );
        return NULL;
    }

    // get image measurements
    *width   = bmpinfo.biWidth;
    *height  = abs ( bmpinfo.biHeight );

    // check if bmp is uncompressed
    if ( bmpinfo.biCompression != BI_RGB )
    {
        CloseHandle ( file );
        return NULL;
    }

    // check if we have 24 bit bmp
    if ( bmpinfo.biBitCount != 24 )
    {
        CloseHandle ( file );
        return NULL;
    }


    // create buffer to hold the data
    *size = bmpheader.bfSize - bmpheader.bfOffBits;
    BYTE* Buffer = new BYTE[ *size ];
    // move file pointer to start of bitmap data
    SetFilePointer ( file, bmpheader.bfOffBits, NULL, FILE_BEGIN );
    // read bmp data
    if ( ReadFile ( file, Buffer, *size, &bytesread, NULL ) == false )
    {
        delete [] Buffer;
        CloseHandle ( file );
        return NULL;
    }

    // everything successful here: close file and return buffer

    CloseHandle ( file );

    return Buffer;
}

您可以在以后使用来自 Intel https://software.intel.com/sites/landingpage/realsense/camera-sdk/v1.1/documentation/html/manuals_image_and_audio_data.html 的代码加载这些位图文件 -

// Image info
PXCImage::ImageInfo info={};
info.format=PXCImage::PIXEL_FORMAT_RGB32;
info.width=image_width;
info.height=image_height;

// Create the image instance
PXCImage image=session->CreateImage(&info);

// Write data
PXCImage::ImageData data;
image->AcquireAccess(PXCImage::ACCESS_WRITE,&data);
... // copy the imported image to data.planes[0]
image->ReleaseAccess(&data); 

使用这三组代码,您应该可以轻松地将位图保存在使用 WriteFile 的任何指定文件夹中,然后一旦加载,您就可以将位图转换回 ImageData。

告诉我进展如何。

【讨论】:

  • 这看起来很不错!,我可以看到你在这方面做得很好:D,你能帮我this吗?
  • 恐怕我没有使用英特尔图像处理套件,所以我只能引用其他人编写的代码。步骤应该是 1. 使用最终链接中的 PXCImage 工具转换为位图 2. 获得位图格式后,使用 runicsoft 方法将其保存到所需的文件。 (作为位图它也可以被其他程序访问) 3. 以后您可以使用第二个 runicsoft 代码将其加载回来。 4. 使用引用的英特尔代码将位图数据加载回 ImageInfo。我希望我能提供更多帮助!
  • 第一次听说runicsoft方法。你有关于那个的链接吗?谢谢
  • 它的runicsoft.com/bmp.cpp - 它将在任何可以打开文本文件的浏览器中打开。
  • @AlexanderLeonVI - 你写的东西有用吗?我看到这个问题得到了积极的关注,我敢打赌,很多人都希望看到你组装的最终产品。
猜你喜欢
  • 2019-12-24
  • 2015-11-24
  • 2015-11-27
  • 1970-01-01
  • 1970-01-01
  • 2014-11-27
  • 1970-01-01
  • 2020-09-19
  • 1970-01-01
相关资源
最近更新 更多