Ask Your Question

upload GpuMat from memory pointer

asked 2017-03-16 04:27:22 -0500

JayneDoe gravatar image

I want to upload a camera image to a GpuMat without unnecessary copy operations.

I get the image as a naked char* from the camera API and at the moment convert it to a GpuMat approximately like this:

is_GetImageMem(hCam, (void*)pLast);
tmpI = cvCreateImageHeader(cvSize(iWidth, iHeight), IPL_DEPTH_8U, 1);
tmpI->imageData = pLast;
Mat M = cv::cvarrToMat(tmpI);
GpuMat gM.upload(M);

Is this the "optimal" way to do it without copying data around? The CPU memory gets overwritten by the camera next time around, is this OK after I uploaded it to the GPU?


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-03-16 04:38:56 -0500

berak gravatar image

updated 2017-03-16 04:42:57 -0500

please AVOID using IplImage* at all cost (or, anything from opencv's deprecated c-api)

rather use cv::Mat:

Mat m(H,W,type, data_pointer);

GpuMat gM; gm.upload(M);
edit flag offensive delete link more


Will do, thank you! After the upload, the content of M does not matter anymore?

JayneDoe gravatar imageJayneDoe ( 2017-03-16 04:52:13 -0500 )edit

a Mat constructed like this will use a "borrowed" pointer, it won't attempt to refcount or free the borrowed memory, so indeed, after uploading to GPU it does not matter any more.

still, there's a caveat: you have to make sure, your data_pointer does not go out of scope (or become otherwise invalid), before you finished uploading the borrowed memory to GPU

berak gravatar imageberak ( 2017-03-16 05:11:37 -0500 )edit

Question Tools

1 follower


Asked: 2017-03-16 04:27:22 -0500

Seen: 983 times

Last updated: Mar 16 '17