Gilad
Gilad

Reputation: 6575

opencv threshold or not equal operator

my matlab code is

imTemp(imTemp ~= maxInd) = 0;

where imTemp is 100x100 double matrix and maxInd == 1

I thought about using cv::threshold http://docs.opencv.org/doc/tutorials/imgproc/threshold/threshold.html

but this doesn't really helps me.
it is only if src(x,y)>thresh.... do something
can you think of another openCV function which can implement this logic?

Upvotes: 0

Views: 745

Answers (1)

beaker
beaker

Reputation: 16791

You can try compare, which can check for equality between a matrix and a scalar (or another matrix) using CMP_EQ.

Unfortunately, compare has the annoying feature that values that satisfy the comparison operator are set to 255 rather than 1 or the original value, so you have to divide to get the Matlab behavior.

Mat imTemp = (Mat_<double>(3,3) << 9,7,4,4,9,6,2,0,1);
double maxInd = 9;
cout << "imTemp Original:" << endl;
cout << imTemp << endl;

compare(imTemp, Scalar(maxInd), imTemp, CMP_EQ);
imTemp = imTemp*maxInd/255;

cout << "imTemp Compared:" << endl;
cout << imTemp << endl;

Output:

imTemp Original:
[9, 7, 4;
  4, 9, 6;
  2, 0, 1]
imTemp Compared:
[9, 0, 0;
  0, 9, 0;
  0, 0, 0]

You can also use the comparison operator directly to get the same results (with the same 255 behavior):

Mat imTemp = (imTemp == maxInd)*maxInd/255;

Upvotes: 1

Related Questions