c++ - 如何从 C++ 中的矩阵 (Mat) 返回特定值的索引?

标签 c++ opencv matrix return return-value

如何返回二维数组中特定值的索引?

这是我到目前为止所做的:

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/

相关文章:

c++ - OpenCV 中文档的模式提取层

opencv - 调整图像大小可以给世界坐标变换提供不同的图像吗?

C++0x 模板函数对象推断

c++ - 对 boost 库的 undefined reference

c++ - 当我尝试使用读取和写入时,为什么 fstream 无法正常工作?

c++ - 指针内存使用

c++ - 来自 OpenCV Mat 对象的奇怪像素值

R:从其他表创建值矩阵

matlab - SVM 中的 Gram 矩阵/核不是半正定的

matlab - 在 MATLAB 中获取矩阵列最大值的索引