Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare OpenCV Mat with scalar elementwise

I have a cv::Mat A, which has CV_32F. However it holds integer values like 1,2....100. I want to form a mask of same size as A.

But the mask must contain zeros if A(x,y) not equal to 5 (say). The mask must contain ones if A(x,y) equal to 5 (say).

I want to later use it as ROIs.

like image 617
mkuse Avatar asked Dec 31 '25 14:12

mkuse


1 Answers

// you will have a much simpler construct, 
// this is just for demonstration
Mat_<float> A(3,3); mf << 1,5,5,2,5,5,1,2,3;

// now use a simple MatExpr to get a mask:
Mat mask = (A == 5);

// show results:
cerr << A << endl;
cerr << mask << endl;

------------------------------

[1, 5, 5;
  2, 5, 5;
  1, 2, 3]
[0, 255, 255;
  0, 255, 255;
  0, 0, 0]
like image 97
berak Avatar answered Jan 03 '26 04:01

berak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!