当前位置:网站首页>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;
}
至此几种方法介绍完毕!
如果以上内容帮到您了,请关注加收藏哦!您的鼓励是我创作的最大动力!
边栏推荐
- cv2.imread()
- npm WARN config global `--global`, `--local` are deprecated. Use `--location solution
- 使用 OpenCV 提取图像的 HOG、SURF 及 LBP 特征 (含代码)
- flutter 混合开发 module 依赖
- cocoscreator 显示刘海内容
- Flutter mixed development module dependencies
- random.randint函数用法
- VS通过ODBC连接MYSQL(一)
- 2021年京东数据分析工程师秋招笔试编程题
- This in js points to the prototype object
猜你喜欢

Gradle sync failed: Uninitialized object exists on backward branch 142

Hyper-V新建虚拟机注意事项

浏览器查找js绑定或者监听的事件

场效应管 | N-mos内部结构详解

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

Understanding of js arrays

自定dialog 布局没有居中解决方案

np.fliplr与np.flipud

The feign call fails, JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r

变分自编码器VAE实现MNIST数据集生成by Pytorch
随机推荐
TransactionTemplate 事务编程式写法
cv2.imread()
VS connects to MYSQL through ODBC (1)
活体检测CDCN学习笔记
js中流程控制语句
朴素贝叶斯文本分类(代码实现)
通信原理——纠错编码 | 汉明码(海明码)手算详解
Take you to understand the MySQL isolation level, what happens when two transactions operate on the same row of data at the same time?
cocos create EditBox 输入文字被刘海屏遮挡修改
VTK环境配置
[Cloud native] Simple introduction and use of microservice Nacos
quick-3.5 ActionTimeline的setLastFrameCallFunc调用会崩溃问题
SSH automatic reconnection script
break and continue exit in js
Gradle sync failed: Uninitialized object exists on backward branch 142
为什么bash中的read要配合while才能读取/dev/stdin的内容
网页截图与反向代理
Access database query
Android software security and reverse analysis reading notes
this指向问题