第二节记录一下自己学习图像遍历的一点点代码,摘自《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. C# xpath

    XPath最通俗的教程(ZZ)   以下是本人找到的最完整最易懂的XPath教程,不敢私藏,拿出来与大家分享.帮我点旁边的google广告呀. 实例 1基本的XPath语法类似于在一个文件系统中定位文 ...

  2. Ansible-Tower快速入门-8.创建组织【翻译】

    创建组织 首行,点击组织标签,组织中将包括有:用户,团队,项目,和清单等项,在tower的对象层级中,组织是最高级对象. 然后,点击增加按钮,如: 为所创建的组织键入一个简单的名称和描述,这些信息你在 ...

  3. php判断json对象是否存在的方法

    在实际测试中php读取json数组时 使用简单的 if 或者 array_key_exists 去判断对象是否存在是会报错的,以下是google搜寻的正确判断方法 实际上出现报错只是我对php还不是很 ...

  4. android 中theme和style的语法相关

    1.theme和style都是一组属性的集合,用于定义文本.颜色.大小等显示风格.他们都是资源,可以用android系统级别的一些默认的风格和主题资源,你也可以自定义你自己的主题和风格资源. 2.自定 ...

  5. “添加到收藏夹”功能(share)

    以下分享自: 如何给网站增加“添加到收藏夹” 给网站添加“添加到收藏夹”理论上应该是很简单的事情,但是受到各种浏览器和操作系统的不一致的问题,使得这个问题异常的繁琐啊. 下面是梳理的一些资料,仅供参考 ...

  6. Latex 数学符号

    本文完全转自 <常用数学符号的 LaTeX 表示方法>,在此转载仅仅为了便于查阅,谨向原作者致以崇高的敬意. 常用数学符号的 LaTeX 表示方法 (以下内容主要摘自“一份不太简短的 LA ...

  7. 2015.10.18 do while练习

    /*乘法表*/ #define COLMAX 10 #define ROWMAX 12 main() { int row,column,y; row=1; printf("          ...

  8. 动态生成元素动作绑定,jquery 1.9如何实现

    1.7后增加了 live()1.9后被移除了 网上说可以用 on() 代替 可以实际在动态生成元素上绑定动作,没效果,求解绝方法(用低版本的jQuery这种方法,求别说..) 答: 之前有老兄回答过类 ...

  9. 关于C语言的问卷调查(补交)

    你对自己的未来有什么规划?做了哪些准备?(还是处于比较迷茫的状态:我做的准备是吧自己对计算机的兴趣提起来!) 你认为什么是学习?学习有什么用?现在学习动力如何?为什么?(学习就是学自己不会的东西:增加 ...

  10. sql关于Group by

    SELECT JBGS.XMID, SUM(JBGS.JBGS * JBYXXS.YXXS) / (SELECT  SUM(B.GS)  FROM T_XMCBHZ B  WHERE  B.XMID= ...