3 ways to iterate through the matrix and comparison - OpenCV 행렬 원소를 모두 참조하는 세 가지 방법
OpenCV 2019. 8. 18. 10:29 |반응형
There are 3 ways to iterate through the matrix. Below code shows how to use them and compares their speed.
행렬 원소를 모두 참조하는 세 가지 방법을 보여주고 속도를 비교 합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main(int argc, char** argv) { TickMeter tm; Mat mat = Mat::zeros(600, 800, CV_8UC1); tm.start(); for (int i = 0; i < mat.rows; i++) for (int j = 0; j < mat.cols; j++) mat.at<uchar>(i, j)++; tm.stop(); cout << ".at<> execution time: " << tm.getTimeSec() << endl; tm.reset(); tm.start(); for (int i = 0; i < mat.rows; i++) { uchar* p = mat.ptr<uchar>(i); for (int j = 0; j < mat.cols; j++) p[j]++; } tm.stop(); cout << ".ptr<> execution time: " << tm.getTimeSec() << endl; tm.reset(); tm.start(); for (MatIterator_<uchar> it = mat.begin<uchar>(); it != mat.end<uchar>(); it++) (*it)++; tm.stop(); cout << "MatIterator execution time: " << tm.getTimeSec() << endl; tm.reset(); return 0; } | cs |
반응형