我的matlab代码是
imTemp(imTemp ~= maxInd) = 0;
其中imTemp为100x100双矩阵,maxInd==1
我考虑过使用cv::thresholdhttp://docs.opencv.org/doc/tutorials/imgproc/threshold/threshold.html
但这并不能真正帮助我。
只有当src(x,y)>thresh。。。。做点什么
你能想到另一个openCV函数来实现这个逻辑吗?
您可以尝试compare
,它可以使用CMP_EQ
检查矩阵和标量(或其他矩阵)之间的相等性。
不幸的是,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;
输出:
imTemp Original:
[9, 7, 4;
4, 9, 6;
2, 0, 1]
imTemp Compared:
[9, 0, 0;
0, 9, 0;
0, 0, 0]
您也可以直接使用比较运算符来获得相同的结果(具有相同的255行为):
Mat imTemp = (imTemp == maxInd)*maxInd/255;