Ask Your Question

cv::Mat::copyTo and cv::gpu::GpuMat::copyTo show different behaviour

asked 2014-01-21 06:22:36 -0600

Wolf gravatar image

updated 2014-01-21 06:43:33 -0600

The doc of Mat::copyTo (void Mat::copyTo(OutputArray m, InputArray mask) const) says:

When the operation mask is specified, and the Mat::create call shown above reallocated the matrix, the newly allocated matrix is initialized with all zeros before copying the data.

I called the method with an empty Mat and a mask for both CPU and GPU. The CPU variant initializes all values to zero, while the GPU variant apparently does not.

For clarity:

My Code CPU1:

// have defined and filled cv::Mat lo_input, lo_mask;
cv::Mat lo_mat;
lo_input.copyTo( lo_mat, lo_mask );

--> works perfect lo_mat now contains the values of lo_input, where lo_mask is non-zero, and has value zero everywhere else

My Code GPU1:

// have defined and filled cv::gpu::GpuMat lo_input, lo_mask;
cv::gpu::GpuMat lo_mat;
lo_input.copyTo( lo_mat, lo_mask );

--> does not cause seg fault and lo_mat has size and type, so apparently calls create on lo_mat but lo_mat contains random fragments of released matrices where the lo_mask is zero -> apparently lo_mat values are not initialized to zero

My Code GPU2:

// have defined and filled cv::gpu::GpuMat lo_input, lo_mask;
cv::gpu::GpuMat lo_mat( lo_input.size(), lo_input.type() );
lo_mat.setTo( cv::Scalar::all( 0 ) );
lo_input.copyTo( lo_mat, lo_mask );

--> works perfect like CPU1

Is this difference in the behaviours of cv::Mat::copyTo and cv::gpu::GpuMat::copyTo intended or a bug?

I am using ubuntu 12.04 64 bit with cuda 5.5 and OpenCV 2.4.7

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-01-23 03:34:34 -0600

Vladislav Vinogradov gravatar image


It was a bug in GpuMat::copyTo method. It was fixed in current 2.4 branch, the fix will be available in the next release.

Thanks for report!

edit flag offensive delete link more

Question Tools


Asked: 2014-01-21 06:22:36 -0600

Seen: 4,074 times

Last updated: Jan 23 '14