【发布时间】:2018-02-27 16:01:08
【问题描述】:
在实现一些图像处理算法时,我遇到了 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;
}
看起来行选择的Iterators不能执行“-”操作,而整个Mat的iterators可以。
【问题讨论】: