opencv访问像素

1. 访问像素的三种方法
①指针访问:最快
②迭代器iterator:较慢,非常安全,指针访问可能出现越界问题
③动态地址计算:更慢,通过at()实现。适用于访问具体某个第i行,j列的像素,而不适用遍历像素


2. Mat在内存中存储形式
灰度图的存储形式:
这里写图片描述
RGB的存储形式:
这里写图片描述
一般情况下,Mat是连续存储的,按行连接。可以通过isContinuous()函数,判断矩阵是否连续存储,若连续返回true。


3. 指针访问像素。输入原图像inImg,为原图像的每个像素赋值(得到输出图像outImg)

注意注意,一定不要直接修改原图像。

void VisitContinueImgByPointer(Mat &inImg, Mat &outImg)
{
    outImg = inImg.clone();//得到原图像的完全拷贝图像
    int rows = outImg.rows;
    int cols = outImg.cols;
    int channels = outImg.channels();

    //判断Mat是否连续存储,实际应用中我认为不用这步
    if(outImg.isContinuous())
    {
        cols *= rows;
        rows = 1;
        //cout << "is continuous " << endl;
    }

    //遍历图像像素 方法1
    for(int i = 0; i < rows; i++)
    {
        unsigned char* data = outImg.ptr<uchar>(i);//data是outImg的行指针
        for(int j = 0; j < cols * channels; j++) //注意:cols * channels,如果输入的是灰度图像,channel=1
        {
            data[j] = 155;  //处理每一个像素
            //add code
        }
} 

    //遍历图像像素 方法2:若存储连续,方法1等效于以下代码
    unsigned char* data = outImg.data;
    for(int i = 0; i < cols * rows * channels; i++)
    {
        data[i] = 155;    //处理每一个像素
    }
}

4. 载入图像image 并将图像Mat image转换成unsigned char* inImg

这里写图片描述

代码如下:

首先要给unsigned char 型的inImg图像malloc申请内存(记住程序结束后要free):

unsigned char* inImg = (unsigned char*)malloc(width * height);//为图像inImg动态分配内存,分配内存的类型为unsigned char*
Mat_convertTo_YUV(image, inImg);

然后在main()的外部实现Mat_convertTo_YUV():

void Mat_convertTo_YUV(Mat &inImg, unsigned char* outImg)
{
    int height = inImg.rows;
    int width = inImg.cols;
    for(int row = 0; row < height; row++)
    {
        unsigned char* ptr_inImg = inImg.ptr<unsigned char>(row);//获取指向inImg当前行的指针
        for(int col = 0; col < width; col++)
            outImg[row * width + col] = ptr_inImg[col];
    }
}

5. 将图像unsigned char* outImg转换成Mat dst,得到输出图像dst

unsigned char* inImg = (unsigned char*)malloc(width * height);//为图像inImg动态分配内存,分配内存的类型为unsigned char*
Mat_convertTo_YUV(image, inImg);

main()外实现函数:

void YUV_convertTo_Mat(unsigned char* inImg, Mat &outImg)
{
    int height = outImg.rows;
    int width = outImg.cols;
    for(int row = 0; row < height; row++)
    {
        unsigned char* ptr_outImg = outImg.ptr<unsigned char>(row);//获取指向outImg当前行的指针
        for(int col = 0; col < width; col++)
        {
            ptr_outImg[col] = inImg[row * width + col];
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值