当前位置:网站首页>opencv之访问图像像素的三种方法
opencv之访问图像像素的三种方法
2022-07-31 05:16:00 【xp_fangfei】
指针访问像素
这种方法最快,但是有点抽象
例1:简单像素操作
for (int i = 0; i < _img.rows; i++) //行循环
{
uchar *data = _img.ptr<uchar>(i); //获取第i行的首地址
for (int j = 0; j < _img.cols; j++) //列循环
{
data[j] = data[j]/2; //处理每个像素
}
}
动态地址操作像素
这种方法简单明了符合大家对像素的认识;
例2:简单像素操作
for (size_t i = 0; i < _img.rows; i++) //行循环
{
for (size_t j = 0; j < _img.cols; j++) //列循环
{
_img.at<cv::Vec3b>(i,j) = cv::Vec3b(0,0,0); //处理每个像素
}
}
例3:判断像素值在某种条件下,对象素值的操作
for (size_t i = 0; i < _img.rows; i++) //行循环
{
for (size_t j = 0; j < _img.cols; j++) //列循环
{
//以下像素操作
if (_img.at<cv::Vec3b>(i,j)[0] >= 100 && _img.at<cv::Vec3b>(i,j)[0] <= 124 &&
_img.at<cv::Vec3b>(i,j)[1] >= 43 && _img.at<cv::Vec3b>(i,j)[1] <= 255 &&
_img.at<cv::Vec3b>(i,j)[2] >= 46 && _img.at<cv::Vec3b>(i,j)[2] <=255)
{
_img.at<cv::Vec3b>(i,j) = cv::Vec3b(0,0,0);;
}else{
_img.at<cv::Vec3b>(i,j) = cv::Vec3b(255,255,255);
}
}
}
迭代器操作像素
这种方法是获取图像矩阵的begin和end,然后增加迭代从begin到end,将*操作符添加在迭代指针前,即可访问当前指向的内容。
例3
cv::Mat_<cv::Vec3b>::iterator it = _img.begin<cv::Vec3b>(); //初始位置的迭代器
cv::Mat_<cv::Vec3b>::iterator itend = _img.end<cv::Vec3b>(); //终止位置的迭代器
//存取彩色图像像素
for(;it != itend; ++it)
{
(*it)[0] = (*it)[0]/2;
(*it)[1] = (*it)[1]/2;
(*it)[2] = (*it)[2]/2;
}
至此几种方法介绍完毕!
如果以上内容帮到您了,请关注加收藏哦!您的鼓励是我创作的最大动力!
边栏推荐
猜你喜欢

OpenCV中的图像数据格式CV_8U定义

CMOS管原理,及其在推挽电路中的应用

MySql to create data tables

Global scope and function scope in js

安装Multisim出现 No software will be installed or removed解决方法

为数学而歌之伯努利家族

WeChat applet source code acquisition and decompilation method

cv2.imread()

Gradle sync failed: Uninitialized object exists on backward branch 142

Understanding of objects and functions in js
随机推荐
Why does read in bash need to cooperate with while to read the contents of /dev/stdin
深度学习知识点杂谈
C语言 | 获取字符串里逗号间隔的内容
自定dialog 布局没有居中解决方案
WeChat applet source code acquisition and decompilation method
Nmap的下载与安装
[swagger close] The production environment closes the swagger method
TransactionTemplate transaction programmatic way
sqlite 查看表结构 android.database.sqlite.SQLiteException: table splitTable has no column named
quick-3.6源码修改纪录
机器学习和深度学习概述
Podspec automatic upgrade script
Android软件安全与逆向分析阅读笔记
TransactionTemplate 事务编程式写法
SSH automatic reconnection script
This in js points to the prototype object
VS2017连接MYSQL
纯shell实现文本替换
cocos2d-x 实现跨平台的目录遍历
Flow control statement in js