OpenCV2学习笔记---指针方式历遍图像
2013-07-24 21:24
495 查看
学习过程参考opencvchina庞峰的视频教程。
主要用到ptr模板函数来获取图像文件内容的指针。把一幅图像看成是一个行向量,然后通过指针偏移历遍图像。
//使用单循环的方式 将图像image赋值为白色
void setAllWhiteE(Mat& image)
{
int x;
//把二维矩阵 image看成是1*length的一维向量
int length = image.cols*image.channels()*image.rows;
//获取矩阵数据的起始地址
uchar* data = image.ptr<uchar>(0);
///逐个访问一维向量中的元素
for(x =0;x<length;x++)
{
data[x] = 255;
}
}
//使用双循环的方式 将图像image赋值为白色
void setAllWhite(Mat& image)
{
int x,y;
//计算图像一行需要被赋值的个数
int rowLength = image.cols*image.channels();
for(y=0;y<image.rows;y++)
{
//获取第行的起始地址
uchar* data = image.ptr<uchar>(y);
//对第y行逐个赋值
for(x=0;x<rowLength;x++)
{
*data++=255;
}
}
}
比较不同方法的用时,可以参考下面的代码
//比较 两种方法的运行速度
void compareTime(Mat& image)
{
int count = 100000;
long begin,end;
//统计双循环方式运行count次需要的时间
begin = clock();
while(count-->0)
setAllWhite(image);
end = clock();
//输出时间
printf("time is %f \n",(double)(end-begin)/(double)CLOCKS_PER_SEC);
//统计单循环方式运行count次需要的时间
count = 100000;
begin = clock();
while(count-->0)
setAllWhiteE(image);
end = clock();
//输出时间
printf("time is %f \n",(double)(end-begin)/(double)CLOCKS_PER_SEC);
}
主要用到ptr模板函数来获取图像文件内容的指针。把一幅图像看成是一个行向量,然后通过指针偏移历遍图像。
//使用单循环的方式 将图像image赋值为白色
void setAllWhiteE(Mat& image)
{
int x;
//把二维矩阵 image看成是1*length的一维向量
int length = image.cols*image.channels()*image.rows;
//获取矩阵数据的起始地址
uchar* data = image.ptr<uchar>(0);
///逐个访问一维向量中的元素
for(x =0;x<length;x++)
{
data[x] = 255;
}
}
//使用双循环的方式 将图像image赋值为白色
void setAllWhite(Mat& image)
{
int x,y;
//计算图像一行需要被赋值的个数
int rowLength = image.cols*image.channels();
for(y=0;y<image.rows;y++)
{
//获取第行的起始地址
uchar* data = image.ptr<uchar>(y);
//对第y行逐个赋值
for(x=0;x<rowLength;x++)
{
*data++=255;
}
}
}
比较不同方法的用时,可以参考下面的代码
//比较 两种方法的运行速度
void compareTime(Mat& image)
{
int count = 100000;
long begin,end;
//统计双循环方式运行count次需要的时间
begin = clock();
while(count-->0)
setAllWhite(image);
end = clock();
//输出时间
printf("time is %f \n",(double)(end-begin)/(double)CLOCKS_PER_SEC);
//统计单循环方式运行count次需要的时间
count = 100000;
begin = clock();
while(count-->0)
setAllWhiteE(image);
end = clock();
//输出时间
printf("time is %f \n",(double)(end-begin)/(double)CLOCKS_PER_SEC);
}
相关文章推荐
- opencv学习笔记1::访问图像中像素的三类方法(用指针,迭代器,动态地址)代码及用时检测
- OpenCV学习笔记_用指针操作图像元素
- OPENCV学习笔记2-2_图像遍历(使用指针)
- OpenCV 2 学习笔记(6): 使用指针来扫描图像:减少图像的位深
- OpenCV学习笔记(十一)图像导向滤波
- opencv学习笔记(十六)利用cvGetSubRect截取图像
- opencv学习笔记(十四)——图像像素的访问
- opencv3 学习笔记 图像与视频的转换
- openCV学习笔记(三)--图像处理Image Processing
- Opencv学习笔记(七)--图像修复
- 【OpenCV学习笔记】三十二、分水岭算法及图像修补
- opencv学习笔记(八)IplImage* 访问图像像素的值
- opencv学习笔记----浅析图像的载入,显示和输出
- Opencv学习笔记--图像处理的基本操作
- OpenCV学习笔记:基本的图像处理
- Python OpenCV学习笔记之:通过图像金字塔方式缩小放大图片
- OPENCV学习笔记2-4_编写高效的图像扫描循环
- OpenCV学习笔记15 OpenCV图像处理模块ImgProc Module. Image Processing(八)
- opencv学习笔记2——载入,显示,复制,旋转,保存图像及灰度图
- opencv学习笔记1:矩阵和图像基本操作