OpenCV: Matrix Iteration

前端 未结 2 1198
隐瞒了意图╮
隐瞒了意图╮ 2020-12-15 11:46

I am new to OpenCV. I am trying to use iterator instead of \"for\" loop, which is too slow for my case. I tried some codes like this:

MatIterator_

        
2条回答
  •  死守一世寂寞
    2020-12-15 12:25

    It's not the for loop which is slow it is the exampleMat.at(i) which is doing range checking.

    To efficently loop over all the pixels you can get a pointer to the data at the start of each row with .ptr()

    for(int row = 0; row < img.rows; ++row) {
        uchar* p = img.ptr(row);
        for(int col = 0; col < img.cols; ++col) {
             *p++  //points to each pixel value in turn assuming a CV_8UC1 greyscale image 
        }
    
        or 
        for(int col = 0; col < img.cols*3; ++col) {
             *p++  //points to each pixel B,G,R value in turn assuming a CV_8UC3 color image 
        }
    
    }   
    

提交回复
热议问题