c++ - OpenCV Mat.row() 的迭代器是否不适用于 stdlib 算法?

标签 c++ opencv iterator std

在实现一些图像处理算法时,我遇到了 OpenCV 的一个奇怪行为。目标是使用该行的 Mat.row(i) 和 OpenCV 迭代器 (Mat.begin()/Mat.end()) 来应用 C++ 标准库的算法(如 std::transform、std::accumulate、等)到基础数据。

#include <numeric>
#include "opencv2/opencv.hpp"

using namespace std;

int main() {

    int rows = 3;
    int cols = 4;
    int x = 1;
    int y = 1;

    cv::Mat_<float> mat(rows,cols);
    iota(mat.begin(),mat.end(),0);

    // Element access works (result is always 5)
    cout << mat(x,y) << endl;
    cout << *(mat.begin()+x*mat.cols+y) << endl;
    cout << *(mat.row(x).begin()+y) << endl;

    // Range is correct for the Mat (result is 12 = rows * cols) ...
    cout << mat.end() - mat.begin() << endl;     
    cout << mat.begin()+x*mat.cols - mat.begin()+(x+1)*mat.cols << endl;

    // ... but incorrect for the row (9223372036854775807)
    cout << mat.row(x).end() - mat.row(x).begin() << endl;

    // So this works (result is 22 = 4+5+6+7 = sum of row 1) ...
    cout << accumulate(mat.begin()+x*mat.cols,mat.begin()+(x+1)*mat.cols,static_cast<float>(0)) << endl;

    // ... but this does not.
    cout << accumulate(mat.row(x).begin(),mat.row(x).end(),static_cast<float>(0)) << endl;

}

看起来行选择的迭代器不能执行“-”操作,而整个Mat的迭代器可以。

最佳答案

您可以使用 Mat::beginMat::end 但这些函数必须应用于同一个 Mat 对象。在这一行

cout << mat.row(x).end() - mat.row(x).begin() << endl;

第一次调用 mat.row(x) 创建了新的 Mat 对象,第二次调用 mat.row(x)创建了另一个新的 Mat 对象。您不能减去 end()begin() 的结果,因为它们引用不同的对象。 你可以写

cv::Mat_<float> rowMat = mat.row(x);
cout << rowMat.end() - rowMat.begin() << endl; // works

cout << accumulate(rowMat.begin(),rowMat.end(),static_cast<float>(0)) << endl;

也适用。

关于c++ - OpenCV Mat.row() 的迭代器是否不适用于 stdlib 算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49012933/

相关文章:

python - 有没有更简洁的方法来获取某些内容的第一次出现?

java - 为什么不能循环直接实现 Iterator 的类?

C++11:未触发 move 构造函数

c++ - Windows Visual C++ 链接到 Mesa3D OpenGL

c++ - 使用 opencv 通过 Filestorage 保存时出错

python - 如何选择正确的颜色来使用 OpenCV 对图像进行阈值处理?

c++ - 有没有办法检查迭代器是否在C++中的IF语句中是random_access?

c++ - 将 holder 类与 reader 分开

c++ - SFML 组合可绘制对象

python - 查找草率手绘矩形的属性