opencv学习笔记 一 图像读取与操作
来源:互联网 发布:网络机房装修标准 编辑:程序博客网 时间:2024/06/05 04:48
包括基本的图像读取,深拷贝和浅拷贝,salt,使用指针遍历图像,颜色缩减,图像锐化
#include <iostream>#include <opencv2\opencv.hpp>#include <opencv2\highgui\highgui.hpp>#define _CRT_SECURE_NO_DEPRECATEvoid salt(cv::Mat &image, int n); //图像中随机元素设置为白色(255)void colreduc( cv::Mat &image, int div = 64); //遍历图像颜色缩减void Sharp(const cv::Mat &image, cv::Mat &result); //图像锐化int main(){ cv::Mat image2; cv::Mat image = cv::imread("1.jpg"); std::cout << "size:" << image.size().height <<","<< image.size().width << std::endl; image.copyTo(image2); //图像拷贝,深拷贝 double duration; duration = static_cast<double>(cv::getTickCount()); //时钟初始个数 Sharp(image, image2); //经历总的时钟个数 duration = static_cast<double>(cv::getTickCount()) - duration; //时间等于时钟个数除以频率 duration /= cv::getTickFrequency(); std::cout << "Time:" << duration << std::endl; cv::namedWindow("Orignial image"); cv::imshow("Orignial image",image); cv::namedWindow("Salt image"); cv::imshow("Salt image", image2); cv::waitKey(0); return 0;}void salt(cv::Mat &image, int n){ for (int k = 0; k < n; k++) { int i = rand() % image.cols; int j = rand() % image.rows; if (image.channels() == 1) { image.at<uchar>(j, i) = 255; } else if (image.channels() == 3) { image.at<cv::Vec3b>(j, i)[0] = 255; //通道表示方法 image.at < cv::Vec3b>(j, i)[1] = 255; image.at<cv::Vec3b>(j, i)[2] = 255; } }}//使用迭代器进行颜色缩减void colreduc(cv::Mat &image,int div){ //result.create(image.rows, image.cols, image.type()); //初始位置的迭代器 cv::Mat_<cv::Vec3b>::iterator it = image.begin<cv::Vec3b>(); cv::Mat_<cv::Vec3b>::iterator itend = image.end<cv::Vec3b>(); for (; it != itend; ++it) { (*it)[0] = (*it)[0] / div * div + div / 2; //对元素进行操作 (*it)[1] = (*it)[1] / div * div + div / 2; (*it)[2] = (*it)[2] / div * div + div / 2; } //指针遍历图像 /*int n = result.rows; int nc = result.cols * result.channels(); uchar mask = 0xFF << 4; for (int j = 0; j < n; j++) { const uchar* data_in = image.ptr<uchar>(j); uchar* data_out = result.ptr<uchar>(j); for (int i = 0; i < nc; i++) { data_out[i] = (data_in[i] & mask) + div / 2; } }*/}//图像锐化void Sharp(const cv::Mat &image, cv::Mat &result){ result.create(image.size(), image.type()); for (int j = 1; j < image.rows - 1; j++) { const uchar* current = image.ptr<const uchar>(j); const uchar* up = image.ptr<const uchar>(j - 1); const uchar* down = image.ptr<const uchar>(j + 1); uchar* output = result.ptr<uchar>(j); for (int i = 1; i < image.cols - 1; i++) { *output++ = cv::saturate_cast<uchar>( 5 * current[i] - current[i + 1] - current[i - 1] - up[i] - down[i]); } } result.row(0).setTo(cv::Scalar(0)); result.row(result.rows - 1).setTo(cv::Scalar(0)); result.col(0).setTo(cv::Scalar(0)); result.col(result.cols - 1).setTo(cv::Scalar(0));}
注:
1.大多数的c++操作符都被重载了,所以可以写出例如:
result = 0.1 * image1 + 0.8 * image2
阅读全文
0 0
- opencv学习笔记 一 图像读取与操作
- OpenCV学习笔记一:图像基础操作
- OpenCV图像读取与操作
- OpenCV学习记录一:图像的读取与显示
- opencv 学习笔记--图像的基本操作(一)
- openCV Python 学习笔记(一)图像的基本操作
- OpenCV学习(一)图像的读取
- OpenCV 学习笔记 1、图像读取
- 双目视觉学习笔记(一)--qtcreator下使用opencv读取并存储双摄像头图像
- Python下opencv使用笔记(一)(图像简单读取、显示与储存)
- OpenCV学习笔记(一):图像显示
- openCV-Python笔记一:图像的读取、显示和保存
- openCV-Python笔记一:图像的读取、显示和保存
- OpenCV入门_学习笔记_读取、显示、处理图像
- opencv学习笔记之读取,修改,保存图像
- 【OpenCV学习笔记】三、图像读取、显示及保存
- OpenCV for Python 学习笔记 1.1 图像的读取
- OpenCV学习笔记1-读取图像/程序计时
- 删除数字
- 能ping通Linux但是ssh连不上,可能的原因
- Hibernate ORM 之 单向一对多
- 试试
- MYSQL自带工具使用介绍
- opencv学习笔记 一 图像读取与操作
- 机会是给有准备的人-为了进入牛B软件公司我需要------------------------
- Codeforces Round #439 (Div. 2) C. The Intriguing Obsession
- 数据结构实验二之顺序表
- Golang
- TCP粘包与拆包问题
- 亮眼的弹出框--SweetAlert2
- 小白做游戏的辛酸路程
- 多列等高布局