简体   繁体   English

opencv阈值或不等于运算符

[英]opencv threshold or not equal operator

my matlab code is 我的matlab代码是

imTemp(imTemp ~= maxInd) = 0;

where imTemp is 100x100 double matrix and maxInd == 1 其中imTemp是100x100双矩阵且maxInd == 1

I thought about using cv::threshold http://docs.opencv.org/doc/tutorials/imgproc/threshold/threshold.html 我考虑过使用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 只有当src(x,y)> thresh ....做点什么
can you think of another openCV function which can implement this logic? 您能想到另一个可以实现此逻辑的openCV函数吗?

You can try compare , which can check for equality between a matrix and a scalar (or another matrix) using CMP_EQ . 您可以尝试compare ,它可以使用CMP_EQ检查矩阵和标量(或另一个矩阵)之间的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. 不幸的是, compare具有令人讨厌的功能,即满足比较运算符的值设置为255而不是1或原始值,因此必须进行除法以获得Matlab行为。

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): 您还可以直接使用比较运算符来获得相同的结果(具有相同的255行为):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM