第二节记录一下自己学习图像遍历的一点点代码,摘自《opencv2编程手册》(张静译)

第一个代码是最简单的强行修改像素(添加椒盐噪声)

 #include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp> void salt(cv::Mat &image, int n) { int i,j;
for (int k=; k<n; k++) { // rand() is the MFC random number generator
i= rand()%image.cols;
j= rand()%image.rows; if (image.channels() == ) { // gray-level image image.at<uchar>(j,i)= ; } else if (image.channels() == ) { // color image image.at<cv::Vec3b>(j,i)[]= ;
image.at<cv::Vec3b>(j,i)[]= ;
image.at<cv::Vec3b>(j,i)[]= ;
}
}
} int main()
{
srand(cv::getTickCount()); // init random number generator cv::Mat image= cv::imread("../cat.jpg",); salt(image,); cv::namedWindow("Image");
cv::imshow("Image",image); cv::imwrite("salted.bmp",image); cv::waitKey(); return ;
}

书上的注释为image.<unchar>(j,i)=255,将i行j列的数据变为白色。

第二个程序才开始真正的遍历

 #include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp> void colorReduce0(cv::Mat &image, int div=) { int nl= image.rows; // 每行的像素数目
int nc= image.cols * image.channels(); // total number of elements per line for (int j=; j<nl; j++) { uchar* data= image.ptr<uchar>(j);//此句返回j行的首地址 for (int i=; i<nc; i++) { // process each pixel --------------------- data[i]= data[i]/div*div + div/; // end of pixel processing ---------------- } // end of line
}
} int main()
{
//srand(cv::getTickCount()); // init random number generator cv::Mat image= cv::imread("../cat.jpg"); colorReduce0(image); cv::namedWindow("Image");
cv::imshow("Image",image); cv::imwrite("cat.jpg",image); cv::waitKey(); return ;
}

这个程序写的是对小猫的颜色进行缩减,效果如下

另,对像素的操作可以采用

*data++ =*data/div*div + div/

来书写

接下来,我就继续看第二种颜色缩进的算法

 void colorReduce1(cv::Mat &image, int div=) {

       int nl= image.rows; // number of lines
int nc= image.cols * image.channels(); // total number of elements per line for (int j=; j<nl; j++) { uchar* data= image.ptr<uchar>(j); for (int i=; i<nc; i++) { // process each pixel --------------------- *data++= *data/div*div + div/; // end of pixel processing ---------------- } // end of line
}
}

效果如下

到这里我就整个人变傻了。两个算法不是一样的吗-  -,为什么效果差别这么大-  -

算了,还是将人家给的代码放出吧,希望有大神指正

#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp> // using .ptr and []
void colorReduce0(cv::Mat &image, int div=) { int nl= image.rows; // number of lines
int nc= image.cols * image.channels(); // total number of elements per line for (int j=; j<nl; j++) { uchar* data= image.ptr<uchar>(j); for (int i=; i<nc; i++) { // process each pixel --------------------- data[i]= data[i]/div*div + div/; // end of pixel processing ---------------- } // end of line
}
} // using .ptr and * ++
void colorReduce1(cv::Mat &image, int div=) { int nl= image.rows; // number of lines
int nc= image.cols * image.channels(); // total number of elements per line for (int j=; j<nl; j++) { uchar* data= image.ptr<uchar>(j); for (int i=; i<nc; i++) { // process each pixel --------------------- *data++= *data/div*div + div/; // end of pixel processing ---------------- } // end of line
}
} // using .ptr and * ++ and modulo
void colorReduce2(cv::Mat &image, int div=) { int nl= image.rows; // number of lines
int nc= image.cols * image.channels(); // total number of elements per line for (int j=; j<nl; j++) { uchar* data= image.ptr<uchar>(j); for (int i=; i<nc; i++) { // process each pixel --------------------- int v= *data;
*data++= v - v%div + div/; // end of pixel processing ---------------- } // end of line
}
} // using .ptr and * ++ and bitwise
void colorReduce3(cv::Mat &image, int div=) { int nl= image.rows; // number of lines
int nc= image.cols * image.channels(); // total number of elements per line
int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));
// mask used to round the pixel value
uchar mask= 0xFF<<n; // e.g. for div=16, mask= 0xF0 for (int j=; j<nl; j++) { uchar* data= image.ptr<uchar>(j); for (int i=; i<nc; i++) { // process each pixel --------------------- *data++= *data&mask + div/; // end of pixel processing ---------------- } // end of line
}
} // direct pointer arithmetic
void colorReduce4(cv::Mat &image, int div=) { int nl= image.rows; // number of lines
int nc= image.cols * image.channels(); // total number of elements per line
int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));
int step= image.step; // effective width
// mask used to round the pixel value
uchar mask= 0xFF<<n; // e.g. for div=16, mask= 0xF0 // get the pointer to the image buffer
uchar *data= image.data; for (int j=; j<nl; j++) { for (int i=; i<nc; i++) { // process each pixel --------------------- *(data+i)= *data&mask + div/; // end of pixel processing ---------------- } // end of line data+= step; // next line
}
} // using .ptr and * ++ and bitwise with image.cols * image.channels()
void colorReduce5(cv::Mat &image, int div=) { int nl= image.rows; // number of lines
int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));
// mask used to round the pixel value
uchar mask= 0xFF<<n; // e.g. for div=16, mask= 0xF0 for (int j=; j<nl; j++) { uchar* data= image.ptr<uchar>(j); for (int i=; i<image.cols * image.channels(); i++) { // process each pixel --------------------- *data++= *data&mask + div/; // end of pixel processing ---------------- } // end of line
}
} // using .ptr and * ++ and bitwise (continuous)
void colorReduce6(cv::Mat &image, int div=) { int nl= image.rows; // number of lines
int nc= image.cols * image.channels(); // total number of elements per line if (image.isContinuous()) {
// then no padded pixels
nc= nc*nl;
nl= ; // it is now a 1D array
} int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));
// mask used to round the pixel value
uchar mask= 0xFF<<n; // e.g. for div=16, mask= 0xF0 for (int j=; j<nl; j++) { uchar* data= image.ptr<uchar>(j); for (int i=; i<nc; i++) { // process each pixel --------------------- *data++= *data&mask + div/; // end of pixel processing ---------------- } // end of line
}
} // using .ptr and * ++ and bitwise (continuous+channels)
void colorReduce7(cv::Mat &image, int div=) { int nl= image.rows; // number of lines
int nc= image.cols ; // number of columns if (image.isContinuous()) {
// then no padded pixels
nc= nc*nl;
nl= ; // it is now a 1D array
} int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));
// mask used to round the pixel value
uchar mask= 0xFF<<n; // e.g. for div=16, mask= 0xF0 for (int j=; j<nl; j++) { uchar* data= image.ptr<uchar>(j); for (int i=; i<nc; i++) { // process each pixel --------------------- *data++= *data&mask + div/;
*data++= *data&mask + div/;
*data++= *data&mask + div/; // end of pixel processing ---------------- } // end of line
}
} // using Mat_ iterator
void colorReduce8(cv::Mat &image, int div=) { // get iterators
cv::Mat_<cv::Vec3b>::iterator it= image.begin<cv::Vec3b>();
cv::Mat_<cv::Vec3b>::iterator itend= image.end<cv::Vec3b>(); for ( ; it!= itend; ++it) { // process each pixel --------------------- (*it)[]= (*it)[]/div*div + div/;
(*it)[]= (*it)[]/div*div + div/;
(*it)[]= (*it)[]/div*div + div/; // end of pixel processing ----------------
}
} // using Mat_ iterator and bitwise
void colorReduce9(cv::Mat &image, int div=) { // div must be a power of 2
int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));
// mask used to round the pixel value
uchar mask= 0xFF<<n; // e.g. for div=16, mask= 0xF0 // get iterators
cv::Mat_<cv::Vec3b>::iterator it= image.begin<cv::Vec3b>();
cv::Mat_<cv::Vec3b>::iterator itend= image.end<cv::Vec3b>(); // scan all pixels
for ( ; it!= itend; ++it) { // process each pixel --------------------- (*it)[]= (*it)[]&mask + div/;
(*it)[]= (*it)[]&mask + div/;
(*it)[]= (*it)[]&mask + div/; // end of pixel processing ----------------
}
} // using MatIterator_
void colorReduce10(cv::Mat &image, int div=) { // get iterators
cv::Mat_<cv::Vec3b> cimage= image;
cv::Mat_<cv::Vec3b>::iterator it=cimage.begin();
cv::Mat_<cv::Vec3b>::iterator itend=cimage.end(); for ( ; it!= itend; it++) { // process each pixel --------------------- (*it)[]= (*it)[]/div*div + div/;
(*it)[]= (*it)[]/div*div + div/;
(*it)[]= (*it)[]/div*div + div/; // end of pixel processing ----------------
}
} void colorReduce11(cv::Mat &image, int div=) { int nl= image.rows; // number of lines
int nc= image.cols; // number of columns for (int j=; j<nl; j++) {
for (int i=; i<nc; i++) { // process each pixel --------------------- image.at<cv::Vec3b>(j,i)[]= image.at<cv::Vec3b>(j,i)[]/div*div + div/;
image.at<cv::Vec3b>(j,i)[]= image.at<cv::Vec3b>(j,i)[]/div*div + div/;
image.at<cv::Vec3b>(j,i)[]= image.at<cv::Vec3b>(j,i)[]/div*div + div/; // end of pixel processing ---------------- } // end of line
}
} // with input/ouput images
void colorReduce12(const cv::Mat &image, // input image
cv::Mat &result, // output image
int div=) { int nl= image.rows; // number of lines
int nc= image.cols ; // number of columns // allocate output image if necessary
result.create(image.rows,image.cols,image.type()); // created images have no padded pixels
nc= nc*nl;
nl= ; // it is now a 1D array int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));
// mask used to round the pixel value
uchar mask= 0xFF<<n; // e.g. for div=16, mask= 0xF0 for (int j=; j<nl; j++) { uchar* data= result.ptr<uchar>(j);
const uchar* idata= image.ptr<uchar>(j); for (int i=; i<nc; i++) { // process each pixel --------------------- *data++= (*idata++)&mask + div/;
*data++= (*idata++)&mask + div/;
*data++= (*idata++)&mask + div/; // end of pixel processing ---------------- } // end of line
}
} // using overloaded operators
void colorReduce13(cv::Mat &image, int div=) { int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));
// mask used to round the pixel value
uchar mask= 0xFF<<n; // e.g. for div=16, mask= 0xF0 // perform color reduction
image=(image&cv::Scalar(mask,mask,mask))+cv::Scalar(div/,div/,div/);
} #define NTESTS 14
#define NITERATIONS 20 int main()
{
int64 t[NTESTS],tinit;
cv::Mat image1;
cv::Mat image2; // timer values set to 0
for (int i=; i<NTESTS; i++)
t[i]= ; // repeat the tests several times
int n=NITERATIONS;
for (int k=; k<n; k++) { std::cout << k << " of " << n << std::endl; image1= cv::imread("../cat.jpg");
if (!image1.data)
return ; // using .ptr and []
tinit= cv::getTickCount();
colorReduce0(image1);
t[]+= cv::getTickCount()-tinit; image1= cv::imread("../cat.jpg");
// using .ptr and * ++
tinit= cv::getTickCount();
colorReduce1(image1);
t[]+= cv::getTickCount()-tinit; image1= cv::imread("../cat.jpg");
// using .ptr and * ++ and modulo
tinit= cv::getTickCount();
colorReduce2(image1);
t[]+= cv::getTickCount()-tinit; image1= cv::imread("../cat.jpg");
// using .ptr and * ++ and bitwise
tinit= cv::getTickCount();
colorReduce3(image1);
t[]+= cv::getTickCount()-tinit; image1= cv::imread("../cat.jpg");
// using direct pointer arithmetic
tinit= cv::getTickCount();
colorReduce4(image1);
t[]+= cv::getTickCount()-tinit; image1= cv::imread("../cat.jpg");
// using .ptr and * ++ and bitwise with image.cols * image.channels()
tinit= cv::getTickCount();
colorReduce5(image1);
t[]+= cv::getTickCount()-tinit; image1= cv::imread("../cat.jpg");
// using .ptr and * ++ and bitwise (continuous)
tinit= cv::getTickCount();
colorReduce6(image1);
t[]+= cv::getTickCount()-tinit; image1= cv::imread("../cat.jpg");
// using .ptr and * ++ and bitwise (continuous+channels)
tinit= cv::getTickCount();
colorReduce7(image1);
t[]+= cv::getTickCount()-tinit; image1= cv::imread("../cat.jpg");
// using Mat_ iterator
tinit= cv::getTickCount();
colorReduce8(image1);
t[]+= cv::getTickCount()-tinit; image1= cv::imread("../cat.jpg");
// using Mat_ iterator and bitwise
tinit= cv::getTickCount();
colorReduce9(image1);
t[]+= cv::getTickCount()-tinit; image1= cv::imread("../cat.jpg");
// using Mat_ iterator
tinit= cv::getTickCount();
colorReduce10(image1);
t[]+= cv::getTickCount()-tinit; image1= cv::imread("../cat.jpg");
// using at
tinit= cv::getTickCount();
colorReduce11(image1);
t[]+= cv::getTickCount()-tinit; image1= cv::imread("../cat.jpg");
// using input/output images
tinit= cv::getTickCount();
cv::Mat result;
colorReduce12(image1, result);
t[]+= cv::getTickCount()-tinit; image2= result; image1= cv::imread("../cat.jpg");
// using input/output images
tinit= cv::getTickCount();
colorReduce13(image1);
t[]+= cv::getTickCount()-tinit; //------------------------------
} cv::namedWindow("Result");
cv::imshow("Result",image2);
cv::namedWindow("Image Result");
cv::imshow("Image Result",image1); // print average execution time
std::cout << std::endl << "-------------------------------------------" << std::endl << std::endl;
std::cout << "using .ptr and [] =" << .*t[]/cv::getTickFrequency()/n << "ms" << std::endl;
std::cout << "using .ptr and * ++ =" << .*t[]/cv::getTickFrequency()/n << "ms" << std::endl;
std::cout << "using .ptr and * ++ and modulo =" << .*t[]/cv::getTickFrequency()/n << "ms" << std::endl;
std::cout << "using .ptr and * ++ and bitwise =" << .*t[]/cv::getTickFrequency()/n << "ms" << std::endl;
std::cout << "using direct pointer arithmetic =" << .*t[]/cv::getTickFrequency()/n << "ms" << std::endl;
std::cout << "using .ptr and * ++ and bitwise with image.cols * image.channels() =" << .*t[]/cv::getTickFrequency()/n << "ms" << std::endl;
std::cout << "using .ptr and * ++ and bitwise (continuous) =" << .*t[]/cv::getTickFrequency()/n << "ms" << std::endl;
std::cout << "using .ptr and * ++ and bitwise (continuous+channels) =" << .*t[]/cv::getTickFrequency()/n << "ms" << std::endl;
std::cout << "using Mat_ iterator =" << .*t[]/cv::getTickFrequency()/n << "ms" << std::endl;
std::cout << "using Mat_ iterator and bitwise =" << .*t[]/cv::getTickFrequency()/n << "ms" << std::endl;
std::cout << "using MatIterator_ =" << .*t[]/cv::getTickFrequency()/n << "ms" << std::endl;
std::cout << "using at =" << .*t[]/cv::getTickFrequency()/n << "ms" << std::endl;
std::cout << "using input/output images =" << .*t[]/cv::getTickFrequency()/n << "ms" << std::endl;
std::cout << "using overloaded operators =" << .*t[]/cv::getTickFrequency()/n << "ms" << std::endl; cv::waitKey();
return ;
}

LINUX 下Open cv练习使用小记(2)的更多相关文章

  1. LINUX 下Open cv练习使用小记(1)

    首先肯定离不开选一张自己喜欢的图像来显示 #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp ...

  2. linux 下cmake 编译 ,调用,调试 poco 1.6.0 小记

    上篇文章 小记了: 关于 Poco::TCPServer框架 (windows 下使用的是 select模型) 学习笔记. http://www.cnblogs.com/bleachli/p/4352 ...

  3. linux下小记

    今天碰到一个问题 记录下 /usr/bin/ld: cannot find ld 和ldconfig的区别 使用makefile编译的时候提示ld提示某个so找不到 当时使用ldconfig查了下 发 ...

  4. Linux下安装Tomcat服务器和部署Web应用

    一.上传Tomcat服务器

  5. Linux下安装OpenCV+Python支持

    以下说明在Linux下Python和OpenCV结合安装的过程,Python要使用OpenCV模块,则必须导入OpenCV提供的包,所以要提供Python支持,首先在安装OpenCV前安装必要的组件, ...

  6. Linux下配置OpenCV1.0环境

    自己一直嚷嚷着打算学学图像识别,识别个简单的,车牌号,验证码之类的,之前查过资料,OpenCV可以实现.昨天花了一个下午终于配置好环境了,今天写下总结. OpenCV这一名称包含了Open和Compu ...

  7. Linux下压缩与解压命令tar

    Linux下常见压缩文件的扩展名 *.gz:gzip压缩的: *.bz2:bzip2压缩的: *.tar:tar程序打包但没有压缩的: *.tar.gz:打包后并经过gzip压缩的: *.tar.bz ...

  8. 转】Linux下安装Tomcat服务器和部署Web应用

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4097608.html 感谢! 一.上传Tomcat服务器

  9. Linux下通过ioctl系统调用来获取和设置网络信息

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...

随机推荐

  1. linux驱动之USB驱动程序

    1. USB是主从结构的 所有的USB传输,都是从USB主机这方发起:USB设备没有"主动"通知USB主机的能力. 例子:USB鼠标滑动一下立刻产生数据,但是它没有能力通知PC机来 ...

  2. python 新旧类的问题

    老式类就是经典类,不是继承自object类.在多继承时采用深度优先遍历父类.新式类就是基类继承自object类 class xxx(object).多继承时采用一种新的C3 算法来遍历父类.实例如下: ...

  3. linux 下 C语言显示中文

    例如:tset.c int main() { printf("你好,世界\n"); retuen 0; } 编译时应该这样: iconv -f gb2312 -t utf8 tes ...

  4. Razor视图引擎-基础语法

    所有以 @开头 或 @{ /* 代码体 */ }  (在@与{直接不得添加任何空格) 的部分代码都会被ASP.NET引擎进行处理. 在 @{ /*代码体*/ } 内的代码每一行都必须以";& ...

  5. (原创)VM中的CentOS6.4中安装CloudStack6.3①

    CloudStack是一个功能强大.UI友好的开源云(IaaS)计算解决方案.自Ctrix将CloudStack捐献给 apache 后,一直持续高速发展,其社区活跃度已经渐渐赶上风头一时无两的另一开 ...

  6. CLR via C# 3rd - 04 - Type Fundamentals

    1. System.Object        The runtime requires every type to ultimately be derived from the System.Obj ...

  7. Android之ListView&ViewPager模拟新闻界面

    模拟新闻 APP 的界面 1)写 ListView 之前先写布局: 这里有两种 Item 的布局: <?xml version="1.0" encoding="ut ...

  8. 转载《Android Handler、Message》

    之前也是由于周末通宵看TI3比赛,一直没找到时间写博客,导致已经有好久没更新了.惭愧!后面还会恢复进度,尽量保证每周都写吧.这里也是先恭喜一下来自瑞典的Alliance战队夺得了TI3的冠军,希望明年 ...

  9. Django 创建APP简单步骤

    yum install epel-releaseyum install python34yum install python-pippip install django django-admin st ...

  10. 初涉hash

    今天和朋友讨论一个问题 有两百亿个数,我给出一个数,找到这两百亿个数中两数相加等于它的组合.要求时间复杂度为线性,空间2G 解决思路是开一个hash表比如a[1000]将所有数存入hash表中,a[i ...