Capture Image from Depth/Color Stream

Capture Image from Depth/Color Stream

Hi all, I'm wondering how do I capture images from a depth or color stream? I'm using the sample C++ code from PCSDK

#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <wchar.h>
#include <vector>
#include <tchar.h>

#include "pxcsession.h"
#include "pxcsmartptr.h"
#include "pxccapture.h"
#include "util_render.h"
#include "util_capture_file.h"
#include "util_cmdline.h"

int wmain(int argc, WCHAR* argv[]) {
    PXCSmartPtr<PXCSession> session;
    pxcStatus sts=PXCSession_Create(&session);
    if (sts<PXC_STATUS_NO_ERROR) {
        wprintf_s(L"Failed to create an SDK session\n");
        return 3;
    }

    UtilCmdLine cmdl(session);
    if (!cmdl.Parse(L"-listio-nframes-sdname-csize-dsize-file-record",argc,argv)) return 3;

    UtilCaptureFile capture(session, cmdl.m_recordedFile, cmdl.m_bRecord);
    /* set source device search critieria */
    for (std::list<std::pair<PXCSizeU32,pxcU32>>::iterator itrc=cmdl.m_csize.begin();itrc!=cmdl.m_csize.end();itrc++)
        capture.SetFilter(PXCImage::IMAGE_TYPE_COLOR,itrc->first,itrc->second);
    for (std::list<std::pair<PXCSizeU32,pxcU32>>::iterator itrd=cmdl.m_dsize.begin();itrd!=cmdl.m_dsize.end();itrd++)
        capture.SetFilter(PXCImage::IMAGE_TYPE_DEPTH,itrd->first,itrd->second);

    PXCCapture::VideoStream::DataDesc request; 
    memset(&request, 0, sizeof(request));
    request.streams[0].format=PXCImage::COLOR_FORMAT_RGB32; 
    request.streams[1].format=PXCImage::COLOR_FORMAT_DEPTH;
    sts = capture.LocateStreams (&request); 
    if (sts<PXC_STATUS_NO_ERROR) {
        // let's search for color only
        request.streams[1].format=0;
        sts = capture.LocateStreams(&request); 
        if (sts<PXC_STATUS_NO_ERROR) {
            wprintf_s(L"Failed to locate any video stream(s)\n");
            return 1;
        }
    }

    std::vector<UtilRender*> renders;
    std::vector<PXCCapture::VideoStream*> streams; 
    for (int idx=0;;idx++) {
        PXCCapture::VideoStream *stream_v=capture.QueryVideoStream(idx);
        if (!stream_v) break;

        PXCCapture::VideoStream::ProfileInfo pinfo;
        stream_v->QueryProfile(&pinfo);
        WCHAR stream_name[256];
        switch (pinfo.imageInfo.format&PXCImage::IMAGE_TYPE_MASK) {
        case PXCImage::IMAGE_TYPE_COLOR: 
            swprintf_s<256>(stream_name, L"Stream#%d (Color) %dx%d", idx, pinfo.imageInfo.width, pinfo.imageInfo.height);
            break;
        case PXCImage::IMAGE_TYPE_DEPTH: 
            swprintf_s<256>(stream_name, L"Stream#%d (Depth) %dx%d", idx, pinfo.imageInfo.width, pinfo.imageInfo.height);
            break;
        }
        renders.push_back(new UtilRender(stream_name));
        streams.push_back(stream_v);
    }

    int i, nwindows=0, nstreams = (int)renders.size();
    PXCSmartSPArray sps(nstreams);
    PXCSmartArray<PXCImage> image(nstreams);

    for (i=0;i<nstreams;i++) { 
        sts=streams[i]->ReadStreamAsync (&image[i], &sps[i]); 
        if (sts>=PXC_STATUS_NO_ERROR) nwindows++;
    }

    for (int k=0; k<(int)cmdl.m_nframes && nwindows>0; k++) {
        pxcU32 sidx=0;
        if (sps.SynchronizeEx(&sidx)<PXC_STATUS_NO_ERROR) break;

        // loop through all active streams as SynchronizeEx only returns the first active
        for (int j=(int)sidx;j<nstreams;j++) {
            if (!sps[j]) continue;
            if (sps[j]->Synchronize(0)<PXC_STATUS_NO_ERROR) continue;
            sps.ReleaseRef(j);

            /* disable rendering during recording. */
            bool active=cmdl.m_bRecord?true:renders[j]->RenderFrame(image[j]);
            if (active) {
                sts=streams[j]->ReadStreamAsync(image.ReleaseRef(j), &sps[j]);
                if (sts<PXC_STATUS_NO_ERROR) sps[j]=0, active=false;
            }
            if (active) continue;
            // we have to close the window here, or the window would not be responsive (No MessageLoop)
            --nwindows;
            renders[j]->Release(); 
            renders[j]=0;
        }
    }
    sps.SynchronizeEx();

    // destroy resources
    for (i=0;i<nstreams;i++)
        if (renders[i]) renders[i]->Release();

    return 0;
}

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Yes, you can capture both RGB and depth image in PXCImage format at the same time. Look at the sample code C:\Program Files (x86)\Intel\PCSDK\sample\camera_viewer for details. The usage is

•Record:    camera_viewer.exe -nframes  <nframes> -file <filename> -record

 

•Playback:  camera_viewer.exe  -file <filename>

If I want to change the size of the depth map, what should I do, for example, set the image size 1280 * 720, can you help me?

The resolution of depth image is QVGA. You can not set the image size to 1280*720.

Leave a Comment

Please sign in to add a comment. Not a member? Join today