Search code examples
c++opencvmaskroielementwise-operations

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.


Solution

  • // 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]