Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How do I transfer buffer data to a Mat object using memcpy?

I'm trying to do image processing of frames from a Hamamatsu Orca flash 4 v 3 and am having a tough time transferring the buffer data to a Mat object.

Here is some example code from the live_average example in the Hamamatsu SDK from their website https://dcam-api.com/sdk-downloads/ :

Frames are monochrome 2048 x 2048 of 16 bit unsigned int

The "image" window is grey, suggesting that I am not actually transferring by reference like I think that I am. Any suggestions on troubleshooting the problem?

        // access image

        err = dcambuf_lockframe( hdcam, &bufframe);
        if( failed(err) )
        {
            dcamcon_show_dcamerr( hdcam, err, "dcambuf_lockframe()" );
            continue;
        }

        // a frame has come

        cv::Mat img(2048, 2048, CV_16U);
        ushort* pointer_to_data_start = img.ptr<ushort>();
        memcpy(pointer_to_data_start, bufframe.buf, bufframe.height * bufframe.rowbytes);
        imshow("image", img);

How do I transfer buffer data to a Mat object using memcpy?

I'm trying to do image processing of frames from a Hamamatsu Orca flash 4 v 3 and am having a tough time transferring the buffer data to a Mat object.

Here is some example code from the live_average example in the Hamamatsu SDK from their website https://dcam-api.com/sdk-downloads/ :

Frames are monochrome 2048 x 2048 of 16 bit unsigned int

The "image" window is grey, suggesting that I am not actually transferring by reference like I think that I am. Any suggestions on troubleshooting the problem?

Also, if this is a 12 bit image where 2 pixels are packed into 3 bytes, is there a way to directly import into a Mat object or must they be unpacked first? What would be an efficient way to do that?

        // access image

        err = dcambuf_lockframe( hdcam, &bufframe);
        if( failed(err) )
        {
            dcamcon_show_dcamerr( hdcam, err, "dcambuf_lockframe()" );
            continue;
        }

        // a frame has come

        cv::Mat img(2048, 2048, CV_16U);
        ushort* pointer_to_data_start = img.ptr<ushort>();
        memcpy(pointer_to_data_start, bufframe.buf, bufframe.height * bufframe.rowbytes);
        imshow("image", img);