LINUX 下Open cv练习使用小记(2)
第二节记录一下自己学习图像遍历的一点点代码,摘自《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)的更多相关文章
- LINUX 下Open cv练习使用小记(1)
首先肯定离不开选一张自己喜欢的图像来显示 #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp ...
- linux 下cmake 编译 ,调用,调试 poco 1.6.0 小记
上篇文章 小记了: 关于 Poco::TCPServer框架 (windows 下使用的是 select模型) 学习笔记. http://www.cnblogs.com/bleachli/p/4352 ...
- linux下小记
今天碰到一个问题 记录下 /usr/bin/ld: cannot find ld 和ldconfig的区别 使用makefile编译的时候提示ld提示某个so找不到 当时使用ldconfig查了下 发 ...
- Linux下安装Tomcat服务器和部署Web应用
一.上传Tomcat服务器
- Linux下安装OpenCV+Python支持
以下说明在Linux下Python和OpenCV结合安装的过程,Python要使用OpenCV模块,则必须导入OpenCV提供的包,所以要提供Python支持,首先在安装OpenCV前安装必要的组件, ...
- Linux下配置OpenCV1.0环境
自己一直嚷嚷着打算学学图像识别,识别个简单的,车牌号,验证码之类的,之前查过资料,OpenCV可以实现.昨天花了一个下午终于配置好环境了,今天写下总结. OpenCV这一名称包含了Open和Compu ...
- Linux下压缩与解压命令tar
Linux下常见压缩文件的扩展名 *.gz:gzip压缩的: *.bz2:bzip2压缩的: *.tar:tar程序打包但没有压缩的: *.tar.gz:打包后并经过gzip压缩的: *.tar.bz ...
- 转】Linux下安装Tomcat服务器和部署Web应用
原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4097608.html 感谢! 一.上传Tomcat服务器
- Linux下通过ioctl系统调用来获取和设置网络信息
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...
随机推荐
- 如何将C#类库做成COM
在类库项目的属性中, 选择生成, 最下方的"为COM的互操作注册"进行勾选, 并且将项目的Properties中, AssemblyInfo.cs中的[assembly: ComV ...
- php 使用 curl 发送 post 数据
作为第三方开发商,经常会需要调用平台接口,远程调用,就要用到curl,其实质就是叫调用的方法与用到的参数以http post的方式发送至平台服务器. 简单的例子: $url = 'http://'; ...
- Java(六)——抽奖系统
总体思路: 将编号加入ArrayList动态数组中,利用集合的静态方法Collections.shuffle() 乱序集合中的元素从而获得随机数,remove删除已抽编号 代码如下: import ...
- removeClass color-*
bootstrap推出一系列的class名称,例如col-md-*.btn-*等等,有时候就会有想要将这一类className删掉的冲动~ 那咋样才能妥妥的实现呢?你是不是已经看到下面答案了,诶诶.. ...
- Javascript 事件对象(四)一个事件绑定多个不同的函数
给一个对象绑定多个事件处理函数: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-T ...
- do while 和 while 的区别
package review20140419;/* * do while 和 while 的区别 */public class Test3 { //程序的入口 public static ...
- Activity、Task、应用和进程
http://www.cnblogs.com/franksunny/archive/2012/04/17/2453403.html Activity.Task.应用和进程 为了阅读方便,将文档转成pd ...
- 合理利用gradle的占位符功能
1.gradle中可以声明字符串或者其他变量,然后再buildType中使用buildConfigField 来往BuildConfig文件中插入一个字符类型的常量,如下 先声明 def umengD ...
- PHP乱码问题,UTF-8(乱码) (share)
一.PHP页面转UTF-8编码问题 1.在代码开始出加入一行: header("Content-Type: text/html;charset=utf-8"); 2.PHP文件编码 ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...