opencv 使用 forEach 像素遍历(pixel 和 const int* position参数都有介绍)

建议直接看3,是成功跑通的例子。
前面的forEach例子,有官方文档的坑,仅作参考意义。

1.简介

forEach函数,可以快速遍历mat图像,获得堪比指针的搜索效率,

template<typename _Tp , typename Functor >
void cv::Mat::forEach(const Functor& operation)	

下面是一些通常的遍历方法

例子来自官方文档 但关于forEach的例子有坑,不要信它,只看最后自己写的forEach例子

Mat image(1920, 1080, CV_8UC3);
typedef cv::Point3_<uint8_t> Pixel;
// first. raw pointer access.
for (int r = 0; r < image.rows; ++r) {
    Pixel* ptr = image.ptr<Pixel>(r, 0);
    const Pixel* ptr_end = ptr + image.cols;
    for (; ptr != ptr_end; ++ptr) {
        ptr->x = 255;
    }
}
// Using MatIterator. (Simple but there are a Iterator's overhead)
for (Pixel &p : cv::Mat_<Pixel>(image)) {
    p.x = 255;
}

2.forEach使用方法

下面分别是使用函数对象,和c++11的lambda函数
作为遍历到每个像素的回调函数。

typedef cv::Point3_<uint8_t> Pixel;

// Parallel execution with function object.
struct Operator {
    void operator ()(Pixel &pixel, const int * position) {
        pixel.x = 255;
    }
};
image.forEach<Pixel>(Operator());

// =============================================

// Parallel execution using C++11 lambda.
image.forEach<Pixel>([](Pixel &p, const int * position) -> void {
    p.x = 255;
});

我们可以注意到,每一个回调函数都有两个参数
Pixel 和 position (分别指,该像素的 数值信息 和 位置信息)

其实讲解这个的函数的人很多,但奇怪的是,很多文章都只讲解了参数的一半, 即只讲pixel, 不讲postion
明明两者的信息都很重要,但逃避了不讲真是像饭吃到一半被咽住了一样,很难受

下面还是官方的例子

// Creating 3D matrix (255 x 255 x 255) typed uint8_t
// and initialize all elements by the value which equals elements position.
// i.e. pixels (x,y,z) = (1,2,3) is (b,g,r) = (1,2,3).
int sizes[] = { 255, 255, 255 };
typedef cv::Point3_<uint8_t> Pixel;
Mat_<Pixel> image = Mat::zeros(3, sizes, CV_8UC3);
image.forEach<Pixel>([&](Pixel& pixel, const int position[]) -> void {
    pixel.x = position[0];
    pixel.y = position[1];
    pixel.z = position[2];
});

上面这些例子举的是三通道图片的情况

  • 可以看到 pixel ,因为一开始定义存储像素所使用的结构是三维坐标点,所以在访问像素时,使用了x,y,z 分别代表该像素点的 blue, grean, red 这三个通道的颜色值

这个例子跑起来有问题,因为position 无论是在单通道还是三通道图片,它返回的都只有两个元素,即x,y坐标,当使用postion[2]时,已经越界了

注意: position 是一个int数组,postion[0]、position[1],分别代表的就是该像素点的x , y坐标,
(无论是在单通道还是三通道图片中)

在这里插入图片描述

在使用opencv4.4编译使用时,按官方的例子(即使用 Pixel),总是报错,
于是我改用Vec3b 后,可以正常使用

3.真正可以用的代码

下面是自己写的一些能够跑通的例子
(在三通道和单通道情况下,同时使用lambda函数)

    // 单通道
    Mat image = Mat::zeros(3, 3, CV_8UC1);
    image.forEach<uchar>([&](uchar& pixel, const int position[]) -> void {
        // position 在单通道情况下,是一个包含两个元素的数组,即x, y;
        pixel = position[0] + position[1];
    });

    // 下面都是打印用的,不看也没关系
    cv::Mat A = image;
    for(int i=0;i<A.rows;i++)
    {
        for(int j=0;j<A.cols;j++)
        cout<<(int)A.at<uchar>(i,j)<<' ';
        cout<<endl;
    }

输出为

0 1 2
1 2 3
2 3 4
    // 三通道
    Mat image = Mat::zeros(3, 3, CV_8UC3);
    image.forEach<Vec3b>([&](Vec3b& pixel, const int position[]) -> void {
        pixel[0] = position[0];
        pixel[1] = position[1];
    });
    
    // 下面都是打印用的,不看也没关系
    cv::Mat A = image;
    for(int i=0;i<A.rows;i++)
    {
        for(int j=0;j<A.cols;j++)
        cout<<(int)A.at<Vec3b>(i,j)[0]<<' ';
        cout<<endl;
    }
    cout<<endl;
    for(int i=0;i<A.rows;i++)
    {
        for(int j=0;j<A.cols;j++)
        cout<<(int)A.at<Vec3b>(i,j)[1]<<' ';
        cout<<endl;
    }
    cout<<endl;
    for(int i=0;i<A.rows;i++)
    {
        for(int j=0;j<A.cols;j++)
        cout<<(int)A.at<Vec3b>(i,j)[2]<<' ';
        cout<<endl;
    }

输出为

0 0 0
1 1 1
2 2 2

0 1 2
0 1 2
0 1 2

0 0 0
0 0 0
0 0 0

如果有错误,还请前辈们批评指正

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值