当前位置:网站首页>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;
}
至此几种方法介绍完毕!
如果以上内容帮到您了,请关注加收藏哦!您的鼓励是我创作的最大动力!
边栏推荐
猜你喜欢
The latest MySql installation teaching, very detailed
unicloud 发布后小程序提示连接本地调试服务失败,请检查客户端是否和主机在同一局域网下
The browser looks for events bound or listened to by js
Pytorch常用函数
变分自编码器VAE实现MNIST数据集生成by Pytorch
计网 Packet Tracer仿真 | 简单易懂集线器和交换机对比(理论+仿真)
动态规划(一)| 斐波那契数列和归递
自定dialog 布局没有居中解决方案
jenkins +miniprogram-ci 一键上传微信小程序
小米手机短信定位服务激活失败
随机推荐
DC-CDN学习笔记
VS2017 connects to MYSQL
quick-3.5 ActionTimeline的setLastFrameCallFunc调用会崩溃问题
quick-3.5 无法使用模拟器修改
Android软件安全与逆向分析阅读笔记
npm WARN config global `--global`, `--local` are deprecated. Use `--location solution
cocos2d-x-3.2 不能混合颜色修改
360 hardening file path not exists.
this points to the problem
After unicloud is released, the applet prompts that the connection to the local debugging service failed. Please check whether the client and the host are under the same local area network.
Pytorch实现ResNet
微信小程序源码获取与反编译方式
腾讯云GPU桌面服务器驱动安装
Flutter mixed development module dependencies
The feign call fails, JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
SQLite 查询表中每天插入的数量
腾讯云轻量服务器删除所有防火墙规则
understand js operators
sqlite 查看表结构 android.database.sqlite.SQLiteException: table splitTable has no column named
2021美赛C题M奖思路