OpenCV4 学习笔记 - 3. 遍历图像

遍历图像在OpenCV中经常用到,高效的方法是使用Pointer,需要考虑图像size,避免出错:

Mat& ScanImageAndReduceC(Mat& I, const uchar* const table)
{
    CV_Assert(I.depth() == CV_8U); // 断言是否8bit uchar
    const int channels = I.channels();
    int rows = I.rows;
    int cols = I.cols * channels;

    if (I.isContinuous()) 
    {
        cols *= rows; // 连续存储则将列数设为row*col,行数row设为1
        rows = 1;
    }

    // 遍历图像,修改
    uchar *p;
    for (int i = 0; i < rows; ++i)
    {
        p = I.ptr(i);
        for (int j = 0; j < cols; ++j)
            // lookup table;读取则直接 X = p[j]
            //也可以用I.at(row, col)来读取指定元素(效率低)
            p[j] = table[p[j]]; 
    }
    return I;
}

还可以使用 Iterator的方式进行迭代,可以不用考虑图像的size,会自动处理:

Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table)
{
    CV_Assert(I.depth() == CV_8U);
    const int channels = I.channels();
    
    switch (channels)
    {
    case 1: // 单通道 gray
    {
        MatIterator_ it, end;
        for (it = I.begin(), end = I.end(); it != end; ++it)
            *it = table[*it];
        break;
    }
    case 3: // 3通道 RGB,使用 Vec3b
    {
        MatIterator_ it, end;
        for (it = I.begin(), end = I.end(); it != end; ++it)
        {
            (*it)[0] = table[(*it)[0]];
            (*it)[1] = table[(*it)[1]];
            (*it)[2] = table[(*it)[2]];
        }
    }
    default:
        break;
    }

    return I;
}

除了上述两种方法还有一种随机访问,使用I.at(row, col),但这种方法效率低,除非需要访问特定元素,遍历一般不考虑这种。使用指针的方式是最高效的,但是需要注意图像的size,避免越界;而迭代器则方便点,不需要考虑size问题,但是效率稍低。

你可能感兴趣的:(OpenCV4 学习笔记 - 3. 遍历图像)