快速遍历OpenCV Mat图像数据的多种方法和性能分析 | opencv mat for loop

本文探讨了遍历OpenCV Mat图像数据的不同方法,包括C++和Python中的ptr、at、Iterator、LUT、forEach等,并通过性能分析展示了它们之间的速度差异。对于图像处理的加速,还讨论了使用openMP和各种Python库(如Cython、Numba、NumPy)的可能性。实验结果显示,某些方法如forEach与lambda表达式能显著提升效率。
摘要由CSDN通过智能技术生成

本文首发于个人博客https://kezunlin.me/post/61d55ab4/,欢迎阅读!

opencv mat for loop

Series

Guide

Mat

  • for gray image, use type
  • for RGB color image,use type

gray format storagegray

color format storage: BGRBGR

we can use method isContinuous() to judge whether the memory buffer is continuous or not.

color space reduction

uchar color_space_reduction(uchar pixel)
{
    /*
    0-9 ===>0
    10-19===>10
    20-29===>20
    ...
    240-249===>24
    250-255===>25

    map from 256*256*256===>26*26*26
    */

    int divideWith = 10;
    uchar new_pixel = (pixel / divideWith)*divideWith;
    return new_pixel;
}

color table

void get_color_table()
{
    // cache color value in table[256]
    int divideWith = 10;
    uchar table[256];
    for (int i = 0; i < 256;   i)
        table[i] = divideWith* (i / divideWith);
}

C

ptr []

// C ptr []: faster but not safe
Mat& ScanImageAndReduce_Cptr(Mat& I, const uchar* const table)
{
    // accept only char type matrices
    CV_Assert(I.depth() != sizeof(uchar));
    int channels = I.channels();
    int nRows = I.rows;
    int nCols = I.cols* channels;
    if (I.isContinuous())
    {
        nCols *= nRows;
        nRows = 1;
    }
    int i, j;
    uchar* p;
    for (i = 0; i < nRows;   i)
    {
        p = I.ptr<uchar>(i);
        for (j = 0; j < nCols;   j)
        {
            p[j] = table[p[j]];
        }
    }
    return I;
}

ptr

// C ptr   : faster but no
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值