OpenCV图像处理基础
来源:互联网 发布:淘宝导航背景颜色代码 编辑:程序博客网 时间:2024/06/04 22:22
OpenCV中程序的模板
#include <iostream>#include"opencv2/opencv.hpp"using namespace std;using namespace cv;
在OpenCV中,灰度图像的存储示意图
彩色图像的存储示意图,在OpenCV中彩色图像的颜色存储顺序为蓝,绿,红(BGR)
Mat常用的构造函数
Mat::Mat()Mat::Mat(int rows, int cols, int type)Mat::Mat(Size size, int type)Mat::Mat(int rows, int cols, int type, const Scalar& s)Mat::Mat(Size size, int type, const Scalar& s)Mat::Mat(const Mat& m)Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP)Mat::Mat(Size size, int type, void* data, size_t step=AUTO_STEP)Mat::Mat(const Mat& m, const Range& rowRange, const Range& colRange)Mat::Mat(const Mat& m, const Rect& roi)//构造函数创建图像Mat M(2,2,CV_8UC3);//释放内存重新创建图像M.create(3,2,CV_8UC2);
OpenCV深拷贝和浅拷贝的问题
遍历图像(几种像素值得读写)
方法1
//灰度图像uchar value=grayim.at<uchar>(i,j);for(int i=0;igrayim.rows;++i) for(int j=0;j<grayim.cols;++j) grayim.at<uchar>(i,j)=(i+j)%255;//彩色图像for(int i=0;i<colorim.rows;++i) for(int j=0;j<colorim.cols;++j) { Vec3b pixel; pixel[0]=i%255; //Blue pixel[1]=j%255; //Green pixel[2]=0; //Red colorim.at<Vec3b>(i,j)=pixel; }
方法2(迭代器)
//灰度图像cv::Mat Iterator_<uchar> grayit, grayend;for( grayit = grayim.begin<uchar>(), grayend = grayim.end<uchar>(); grayit != grayend; ++grayit) *grayit = rand()%255;//彩色图像cv::Mat Iterator_<Vec3b> colorit,colorend;for(colorit=colorim.begin<Vec3b>(),colorend=colorim.end<Vec3b>();colorit!=colorend;++colorit){ (*colorit)[0]=rand()%255; //Blue (*colorit)[1]=rand()%255; //Green (*colorit)[2]=rand()%255; //Red}
方法3
for( int i = 0; i < grayim.rows; ++i){ //获取第 i 行首像素指针 uchar * p = grayim.ptr<uchar>(i); //对第 i 行的每个像素(byte)操作 for( int j = 0; j < grayim.cols; ++j ) p[j] = (i+j)%255;}
方法4(step)
二维数据
三维数据
addr(Mi0,i1,…im-1) = M.data + M.step[0] * i0 + M.step[1] * i1 + … +M.step[m-1] * im-1 (其中 m = M.dims M的维度)
int main(){ //新建一个uchar类型的单通道矩阵(grayscale image 灰度图) Mat m(400, 400, CV_8U, Scalar(0)); for (int col = 0; col < 400; col++) { for (int row = 195; row < 205; row++) { cout << (int)(*(m.data + m.step[0] * row + m.step[1] * col)) << "==>"; //获取第[row,col]个像素点的地址并用*符号解析 *(m.data + m.step[0] * row + m.step[1] * col) = 255; cout << (int)(*(m.data + m.step[0] * row + m.step[1] * col)) << endl; } } imshow("binary image", m); cvWaitKey(); return 0;}
int main(){ Mat m = imread("lena.jpg"); Vec3i color; for (int col = 20; col < 40; col++) for (int row = 5; row < 25; row++) { color[0] = (int)(*(m.data + m.step[0] * row + m.step[1] * col)); color[1] = (int)(*(m.data + m.step[0] * row + m.step[1] * col + m.elemSize1())); color[2] = (int)(*(m.data + m.step[0] * row + m.step[1] * col + m.elemSize1() * 2)); //获取第[row,col]个像素点的地址并用*符号解析 cout << color[0] << "," << color[1] << "," << color[2] << "==>"; color[0] = 255; color[1] = 0; color[2] = 0; *(m.data + m.step[0] * row + m.step[1] * col) = color[0]; *(m.data + m.step[0] * row + m.step[1] * col + m.elemSize1()) = color[1]; *(m.data + m.step[0] * row + m.step[1] * col + m.elemSize1() * 2) = color[2]; cout << (int)*(m.data + m.step[0] * row + m.step[1] * col) << (int)*(m.data + m.step[0] * row + m.step[1] * col + 1) << (int)*(m.data + m.step[0] * row + m.step[1] * col + 2) << endl; } imshow("lena", m); cvWaitKey(); return 0;}
方法5(Mat_类)
Mat M(600, 800, CV_8UC1);for (int i = 0; i < M.rows; ++i){ uchar * p = M.ptr<uchar>(i); for (int j = 0; j < M.cols; ++j) { double d1 = (double)((i + j) % 255); M.at<uchar>(i, j) = d1; double d2 = M.at<double>(i, j); }}
Mat_<uchar> M1 = (Mat_<uchar>&)M;for (int i = 0; i < M1.rows; ++i){ uchar * p = M1.ptr(i); for (int j = 0; j < M1.cols; ++j) { double d1 = (double)((i + j) % 255); M1(i, j) = d1; double d2 = M1(i, j); }}
int main(){Mat m = imread("lena.jpg"); Mat_<Vec3b>m2 = m; //for循环画一个蓝色的实心圆 for (int y = 21; y < 42; y++) for (int x = 2; x < 21; x++) { if (pow(double(x - 11), 2) + pow(double(y - 31), 2) - 64 < 0.0000000001) { //Mat_模板类实现了对()的重载,可以定位到一个像素 m2(x, y) = Vec3b(255, 0, 0); } } imshow("CircleImage", m2); cvWaitKey(); return 0;}
方法6
int divideWith=10;uchar table[256];for (int i = 0; i < 256; ++i) table[i] = divideWith* (i/divideWith); //量化
Mat lookUpTable(1, 256, CV_8U);uchar* p = lookUpTable.data;for( int i = 0; i < 256; ++i) p[i] = table[i];LUT(I, lookUpTable, Out);
学习OpenCV必备资料
手册:http://docs.opencv.org/
教程:http://docs.opencv.org/2.4/doc/tutorials/tutorials.html
进阶:http://github.com/opencv/opencv/wiki
在OpenCV安装目录下build->doc->opencv_tutorials.pdf 为学习教程
阅读全文
0 0
- OpenCV - 图像处理基础
- OpenCV图像处理基础
- 转:openCV基础图像处理
- openCV笔记:图像处理基础
- OpenCV基础02(GUI+图像处理)
- OpenCV图像处理的一些基础
- OPENCV图像处理基础(六)序列化储存图像
- Opencv教程基础篇(一)--MFC使用Opencv处理图像
- OPENCV图像处理基础(一)读取一张图片
- OPENCV图像处理基础(二)感兴趣区域ROI
- OPENCV图像处理基础(三)播放一个视频
- OPENCV图像处理基础(五)鼠标事件画个框
- opencv-python人眼识别图像处理基础
- OpenCV笔记第一天:图像处理基础结构
- 图像处理基础(2):自适应中值滤波器(基于OpenCV实现)
- OpenCV----图像处理
- opencv 图像处理
- OpenCV图像处理---->>滤波器
- 使用myeclipse创建maven web项目
- Hive设置连接用户名和密码
- 2017大学生程序设计邀请赛(华东师范大学)F. 丽娃河的狼人传说
- Asp.net采用UpdatePanel局部刷新js失效问题
- JSON.stringify()方法将javascript对象转换为字符串
- OpenCV图像处理基础
- 【JS】可多次使用的jQuery tab选项卡
- mysql5.7 创建用户并授权
- 解决WebSocket在angularjs里scope的值无法更新
- saiku无缝对接kylin
- html在线转word ssm
- 王者荣耀
- 大白话讲解小波变换由来
- BIO,NIO,AIO的区别