当前位置:网站首页>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;
}
至此几种方法介绍完毕!
如果以上内容帮到您了,请关注加收藏哦!您的鼓励是我创作的最大动力!
边栏推荐
猜你喜欢
随机推荐
Talking about the understanding of CAP in distributed mode
人脸识别AdaFace学习笔记
Pytorch实现ResNet
活体检测CDCN学习笔记
VTK:Could not locate vtkTextRenderer object.
数据库 | SQL查询进阶语法
cv2.imread()
禅道安装及使用教程
WeChat applet source code acquisition and decompilation method
cocoscreator 显示刘海内容
对js的数组的理解
jenkins +miniprogram-ci 一键上传微信小程序
quick lua加密
unicloud 云开发记录
Markdown help documentation
Global scope and function scope in js
The browser looks for events bound or listened to by js
活体检测PatchNet学习笔记
360 加固 file path not exists.
计算图像数据集均值和方差









