Ask Your Question
1

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

asked 2019-09-10 12:27:19 -0600

jjsword gravatar image

updated 2019-09-20 08:41:23 -0600

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);
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2019-09-10 15:42:22 -0600

updated 2019-09-10 16:02:52 -0600

Please see the documentation. You don't need to copy data

try

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

note: (as stated in the documentation ) The external data is not automatically deallocated, so you should take care of it.

edit flag offensive delete link more

Comments

That worked! Thanks!

jjsword gravatar imagejjsword ( 2019-09-11 09:15:45 -0600 )edit

One more question is that if this is a 12 bit image that is bit packed where 2 pixels are contained in 3 bytes, is there a function in opencv to import that image or must I unpack it first?

jjsword gravatar imagejjsword ( 2019-09-19 16:51:34 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-09-10 12:27:19 -0600

Seen: 5,183 times

Last updated: Sep 20 '19