How to mask a float mat in opencv
Asked Answered
C

2

7

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 ?

Crowell answered 3/4, 2015 at 12:32 Comment(3)
Why do you need to convert the mask to CV_32F?Amando
convert or not, neither worksCrowell
Please provide your code and explain how doesn't it work.Amando
A
4

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
Amando answered 7/4, 2015 at 2:20 Comment(1)
This works, thanks. I was trying to use bitwise_and without success. Can you add that option?Onesided
W
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 ) ;
Walloon answered 28/5, 2018 at 6:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.