访问cv :: Mat中的所有像素

问题描述:

这是访问 cv :: Mat 中所有像素的正确方法:

Is this the correct way of accessing all pixels in cv::Mat:

for( row = 0; row < mat.rows; ++row) 
    {
            for ( col = 0; col < mat.cols; ++col) 
            {



            }
    }


$ b b

或者有一个类似于 IplImage * 的公式方法:

temp_ptr = &((uchar*)(img->imageData + (img->widthStep*pt.x)))[pt.y*3];


在最好的情况下,您应该能够:

In the best case, where all the pixels are stored contiguously you should be able to do:

uchar* pixel = mat.data;
for(int i = 0; i < mat.rows * mat.cols; ++i) 
{
    // access pixel[0],pixel[1],pixel[2] here
    pixel += 3; // move to next pixel
}

要更加通用, ,请查看示例代码中提到的 Mat :: isContinuous()。用于计算元素地址的常规公式可以在此处

To be a bit more generic, but still fast, have a look at the sample code mentioned with Mat::isContinuous(). The general formula for calculating the address of an element can be seen here (Reproduced below).