访问cv :: Mat中的所有像素 [英] Accessing all pixels in cv::Mat

查看:164
本文介绍了访问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).

这篇关于访问cv :: Mat中的所有像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆