Luis  Hernández
Luis Hernández

Reputation: 29

equivalence matlab code in c ++ opencv

I basically like to know the equivalent of the following code in matlab to opencv using c ++

MascMmax = s3 > Mmax

This compares all positions of the two matrices and to fulfill the condition wards 1 and 0 otherwise.

In opencv I found that I can do but I saved 255, 0. I wonder how I do like 1 and 0.

example:

a [1,2,3]
b [2,1,4]

c = a > b

c[0,1,0]

Upvotes: 1

Views: 146

Answers (2)

Kornel
Kornel

Reputation: 5354

Sure, the function compare() performs the per-element comparison of two arrays or an array and scalar value.

You can have the following comparisons:

  • CMP_EQ src1 is equal to src2.
  • CMP_GT src1 is greater than src2.
  • CMP_GE src1 is greater than or equal to src2.
  • CMP_LT src1 is less than src2.
  • CMP_LE src1 is less than or equal to src2.
  • CMP_NE src1 is unequal to src2.

When the comparison result is true, the corresponding element of output array is set to 255. The comparison operations can be replaced with the equivalent matrix expressions:

Mat dst1 = src1 >= src2;
Mat dst2 = src1 < 8;

Upvotes: 2

FooTheBar
FooTheBar

Reputation: 857

I haven't seen a function like this in Opencv, but you could do the following:

a) compute c as a-b using

addWeighted(a,1,b,-1,0,c). 

addWeighted checks the ranges of your datatype, so for your 8UC1-Data, all negative values will be mapped to zero so that all points with a[i] <= b[i], you will have c[i] = 0, otherwise c[i] > 0 b) use cv::threshold to set all positive entries in c to a fixed value, in your case

cv::threshold(c,0,1,THRESH_BINARY)

The 0 is your threshold and 1 the value to which all pixels with a value larger than 0 (your threshold) are set.

If you have a signed datatype, the approach will work exactly the same :)

Upvotes: 1

Related Questions