6

I have a mat A of type CV_32F and a mask M with binary value 0 and 255. For example,

A = [0.1 0.2; 0.3 0.4]  
M = [1   0  ; 0   0  ]

I want to get the result of A&B = [0.1, 0;0 0] While bitwise operation does not work on float mat. And I tried to convert the mask to CV_32F and then mask like the following, also not work.

M.convertTo(M, CV_32F);
A.copyTo(A, M);

So how to do it ?

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
Frazer
  • 329
  • 1
  • 4
  • 17

2 Answers2

3

Your code doesn't work because, as the doc of Mat::copyTo says, the function does not handle the case of a partial overlap between the source and the destination matrices, while the source and the destination matrices are the same in your case.

You should save the result elsewhere, like

cv::Mat dst;
A.copyTo(dst, M);  // dst is what you want
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
0

I think this might help:

//to create mask
cv :: Mat floatMask
cv::threshold( someimage , floatMask , 0 , 1 , cv ::THRESH_BINARY );
floatMask.convertTo( floatMask , cv :: CV_32F);

// now to mask image 
floatImageToBeMasked = floatImageToBeMasked.mul ( floatMask ) ;
cela
  • 2,352
  • 3
  • 21
  • 43