如何返回二维数组中特定值的索引?
这是我到目前为止所做的:
Mat *SubResult;
for(int i=0; i < height; i++){
for(int j=0; j< width; j++){
if(SubResult[i][j]<0){
return [i][j];
}
}
}
这是我在你的解释后所做的,但我仍然得到错误:
void Filter(float* currentframe, float* previousframe, float* SubResult){
int width ;
int height ;
std::vector< std::pair< std::vector<int>, std::vector<int> > > Index;
cv::Mat curr = Mat(height, width, CV_32FC1, currentframe);
cv::Mat prev = Mat(height, width, CV_32FC1, previousframe);
//cv::Mat Sub = Mat(height, width, CV_32FC1, SubResult);
cvSub(currentframe, previousframe, SubResult);
cv::Mat Sub = Mat(height, width, CV_32FC1, SubResult);
for(int i=0; i < height; i++){
for(int j=0; j< width; j++){
if(Sub[i][j] < 0){
Index.push_back(std::make_pair(i,j));
}
}
}
}
最佳答案
使用 pair<int,int>
作为你的返回类型,并返回这样的一对:
return make_pair(i, j);
在接收端,调用者需要按如下方式访问该对的元素:
pair<int,int> p = find_2d(.....); // <<== Call your function
cout << "Found the value at (" << p.first << ", " << p.second << ")" << endl;
关于c++ - 如何从 C++ 中的矩阵 (Mat) 返回特定值的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19515467/