opecv Mat 遍历

博客围绕 OpenCV 中 Mat 对象的遍历展开,虽未给出具体内容,但可知聚焦于 OpenCV 图像处理领域,Mat 对象遍历是图像处理中常见操作,对图像数据处理有重要意义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//用时多少
void printMs(const char *text = "")
{
    static long long last = 0;
    long long cur = getTickCount();
    if(0 == last)
    {
        last = cur;
        return;
    }
    long long ms = 0;
    ms = ((double)((cur/last)/getTickFrequency())) * 1000;
    if(0 != *text)
    {
        printf("%s == %ld", text, ms);
        last = getTickCount();
    }
}
void test()
{
    Mat image(300, 400, CV_8UC3);
    int es = image.elemSize(); //一个像素占多少字节
    int size = image.rows*image.cols*es; //多大
    printMs(NULL);
    for(int i = 0; i<size; i += es)
    {
        image.data[i] = 255;
        image.data[i+1] = 0;
        image.data[i+2] = 0;
    }
    //它会抛出异常
    try
    {
        for(int row = 0; row < image.rows; ++row)
        {
            for(int col = 0; col < image.cols; ++col)
            {
                Vec3b *c = image.ptr<Vec3b>(row, col);
                c->val[0] = 0;
                c->val[1] = 255;
                c->val[2] = 0;
            }
        }
    }
    catch(Exception &ex)
    {
        cout << ex.what() << endl;
    }

    //不连续, 地址访问
    for(int row=0; row<image.rows; ++row)
    {
        for(int col = 0; col<image.cols; ++col)
        {
            (&image.data[row*image.step])[col*es] = 0;
            (&image.data[row*image.step])[col*es+1] = 0;
            (&image.data[row*image.step])[col*es+2] = 255;
        }
    }
    printMs("text");
    namedWindow("img");
    imshow("img", image);
    waitKey(0);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值