Ask Your Question

UMat operators

asked 2017-01-09 20:53:07 -0500

victor.dmdb gravatar image

How does one use basic matrix operators with cv::UMat, similar to those available for cv::Mat. For example using ~ for bitwise_not, or comparing 2 matrices with >.

I'm aware of getMat, but I assume that using that function would remove the advantages of using UMat.

Also are there any efficiency differences between the 2 options below?

  cv::UMat srcMat;
  cv::Mat dstMat;
  cv::cvtColor(srcMat, dstMat, CV_BGR2YUV);
  uint8_t * data =;


  cv::UMat srcMat, dstMat;
  cv::cvtColor(srcMat, dstMat, CV_BGR2YUV);
  uint8_t * data = dstMat.getMat(cv::ACCESS_READ).data;
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-01-09 21:48:12 -0500

Tetragramm gravatar image

For the basic matrix operations, you should use the functions, such as bitwise_and, bitwise_not and so forth. The operators are not supported. There is no efficiency loss in using the functions.

As for the second question, I have no idea. You should do what I always do and try it in a loop of 1000 iterations and time it.

edit flag offensive delete link more


so regarding the 1st issue, I would run cv::bitwise_not(umat, umat), though I've understood its not recommended to overwrite a matrix with itself, is that incorrect? Also how would I compare 2 umat matrices, since they don't have any equivalent bitwise functions?

victor.dmdb gravatar imagevictor.dmdb ( 2017-01-09 23:57:05 -0500 )edit

I don't understand "Also how would I compare 2 umat matrices, since they don't have any equivalent bitwise functions?"

this code is OK

UMat x(256,256,CV_8UC1),y(256, 256, CV_8UC1);
UMat z;

LBerger gravatar imageLBerger ( 2017-01-10 02:35:44 -0500 )edit

As in with cv::Mat one can do:

cv::Mat A = cv::Mat(1000, 1000, CV_8UC3), B = cv::Mat(1000, 1000, CV_8UC3);
cv::randu(A, Scalar::all(0), Scalar::all(255));
cv::randu(B, Scalar::all(0), Scalar::all(255));
cv::Mat C = A > B;

Is there any way to do the same with cv::UMat directly?

victor.dmdb gravatar imagevictor.dmdb ( 2017-01-10 02:47:55 -0500 )edit

yes it seems possible :

UMat ua, ub,uc;
Mat A = (Mat_<double>(3, 1) << 1, 2, 3) , B=(Mat_<double>(3, 1) << 3,2,1);
cout << A << endl << B << endl << A - B << endl;
cout << (A >B);
compare(ua, ub, uc, CMP_GT);
cout << uc.getMat(ACCESS_READ);
LBerger gravatar imageLBerger ( 2017-01-10 08:17:19 -0500 )edit

haha, alright I was asking for a compare function, and there is indeed a compare function, I probably should have looked better

victor.dmdb gravatar imagevictor.dmdb ( 2017-01-10 08:19:55 -0500 )edit

Question Tools

1 follower


Asked: 2017-01-09 20:47:46 -0500

Seen: 907 times

Last updated: Jan 09 '17