0

I have a matrix D which I would like to set to zero where another matrix T is zero and keep intact otherwise. In numpy, I'd do this:

D[T==0]=0;

but with cv::Mat, not sure how to do it. I tried this:

D&=(T!=0);

with this result:

OpenCV Error: Assertion failed (type == B.type()) in gemm, file /build/opencv-AuXD2R/opencv-3.3.1/modules/core/src/matmul.cpp, line 1558
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/opencv-AuXD2R/opencv-3.3.1/modules/core/src/matmul.cpp:1558: error: (-215) type == B.type() in function gemm

Is the problem that I am mixing numeric types? I also tried this then (D is CV_32F as well, which I verified by outputting T.type(), 5):

cv::Mat TnotZero;
cv::Mat(T!=0).convertTo(TnotZero,CV_32F);
D&=TnotZero;

with the same result.

What is the solution?

eudoxos
  • 18,545
  • 10
  • 61
  • 110
  • 3
    Use [`Mat::setTo()`](https://docs.opencv.org/3.0-beta/modules/core/doc/basic_structures.html#mat-setto) with the `mask` parameter. You can invert the mask first with [`cv::bitwise_not()`](https://docs.opencv.org/3.0-beta/modules/core/doc/operations_on_arrays.html#bitwise-not). – alkasm Jan 09 '18 at 20:39
  • Thanks, very good tip for opencv beginner :) – eudoxos Jan 09 '18 at 20:42

0 Answers0