## openCV学习笔记 ##

来源:互联网 发布:手机mp3录音软件 编辑:程序博客网 时间:2024/05/21 04:18

openCV学习笔记


opencv对图像的每一个像素的读取操作可以分为三种方法

一、使用指针

int main(){Mat img=imreag("1.jpg");int rowNumber=img.rows;int colNumber=img.cols*img.channels();for(int i=0;i<rowNumber;i++)//航循环{    uchar *p=img.ptr<uchar>(i);//获取每一行的首地址    for(int j=0;j<colNumber;j++)    {        p[j]=p[j]/100;//操作每个像素    }}}

指针的速度是最快的。
二、使用迭代器
使用迭代器仅仅是获得Mat矩阵的begin和end。

int main(){Mat img=imreag("1.jpg");Mat_<Vec3b>::iterator  it=img.begin<Vec3d>();Mat_<Vec3b>::iterator itend=img>end<Vec3d>();for(;it!=itend;it++)//处理每个像素{    (*it)[0]=(*it)[0]/10*10;    (*it)[1]=(*it)[1]/10*10;    (*it)[2]=(*it)[2]/10*10;}}迭代器的使用是非常安全的,不会出现越界的情况。

三、动态地址配合成员函数at

int main(){Mat img=imread("1.jpg");int rowNumber=img.rows;int colNumber=img.cols*img.channels();for(int i=;i<rowNumber;i++){    for(int j;j<colNumber;j++)    img.at<Vec3d>(i,j)[0]=img.at<Vec3d>(i,j)[0]/10*10;    img.at<Vec3d>(i,j)[1]=img.at<Vec3d>(i,j)[1]/10*10;    img.at<Vec3d>(i,j)[2]=img.at<Vec3d>(i,j)[2]/10*10;}