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 ...
随机推荐
- MyEclipse 10.7.1 最新版官网下载地址
MyEclipse 10.7.1(Windows): http://downloads.myeclipseide.com/downloads/products/eworkbench/indigo/in ...
- Struts2框架下表单数据的流向以及映射关系
本例框架很简单:默认页面为用户登录界面login.jsp,提交后由action类LoginAction.java来判断成功或失败,登录结果分别由success.jsp和failure.jsp呈现. 一 ...
- 富文本编辑器kindeditor配置
<!--富文本编辑器kindeditor配置↓ --> <link type="text/css" rel="stylesheet" href ...
- 分布式blog系统 TFS总结
解决的问题 文件总量太大 一台服务器无法存放 只能放在网络集群中分节点存放 也就是通过屏蔽网络部分 形成一个“ one big CPU” 和 “one big disk” .Client只需要向 ...
- 《一个 Go 程序系统线程暴涨的问题》结论
原文地址:https://zhuanlan.zhihu.com/p/22474724 作者的结论没写好,我来说两句.. 结论: Docker swarm自己有个函数,叫setTcpUserTimeou ...
- 用GO扫描图片像素,复制图片
关键是使用image.image/png.image/color包 // main.go package main import ( "fmt" "bufio" ...
- js倒计时跳转链接
(function(){ var loadUrl = 'http://www.cnblogs.com/naokr/',//跳转链接 loadTime = 3000,//跳转时间 reTime = 10 ...
- android 中theme和style的语法相关
1.theme和style都是一组属性的集合,用于定义文本.颜色.大小等显示风格.他们都是资源,可以用android系统级别的一些默认的风格和主题资源,你也可以自定义你自己的主题和风格资源. 2.自定 ...
- node.js Websocket消息推送---GoEasy
Goeasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送!个人感觉goeasy推送更稳定,推送 速度快,代码简单易懂上手快 浏览器兼容性:GoEasy推送 支持websocket ...
- HDU 4857 逃生 (反向拓扑排序 & 容器实现)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others) Mem ...