当前位置:网站首页>Three methods of accessing image pixels in opencv
Three methods of accessing image pixels in opencv
2022-07-31 06:32:00 【xp_fangfei】
Pointer to access pixels
这种方法最快,但是有点抽象
例1:Simple pixel manipulation
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; //处理每个像素
}
}
Dynamic address manipulation of pixels
This method is simple and clear in line with everyone's understanding of pixels;
例2:Simple pixel manipulation
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:Determine the pixel value under certain conditions,Operations on pixel values
for (size_t i = 0; i < _img.rows; i++) //行循环
{
for (size_t j = 0; j < _img.cols; j++) //列循环
{
//The following pixel operations
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);
}
}
}
迭代器操作像素
This method is to get the image matrixbegin和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;
}
So far, several methods have been introduced!
If the above helps you,Please follow and add favorites!您的鼓励是我创作的最大动力!
边栏推荐
猜你喜欢

Cholesterol-PEG-Acid CLS-PEG-COOH 胆固醇-聚乙二醇-羧基修饰肽类化合物

【解决问题】RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton

The browser looks for events bound or listened to by js

qt:cannot open C:\Users\XX\AppData\Local\Temp\main.obj.15576.16.jom for write

化学试剂磷脂-聚乙二醇-氨基,DSPE-PEG-amine,CAS:474922-26-4

ROS之service编程的学习和理解

日志jar包冲突,及其解决方法

Research reagents Cholesterol-PEG-Maleimide, CLS-PEG-MAL, Cholesterol-PEG-Maleimide

mysql 事务原理详解

VTK:Could not locate vtkTextRenderer object.
随机推荐
2022 SQL big factory high-frequency practical interview questions (detailed analysis)
2021年软件测试面试题大全
词向量——demo
Multi-Modal Face Anti-Spoofing Based on Central Difference Networks学习笔记
softmax函数详解
CLS-PEG-FITC Fluorescein-PEG-CLS 胆固醇-聚乙二醇-荧光素简介
会话和饼干,令牌
Tensorflow相关list
Shell/Vim related list
MySQL 出现 The table is full 的解决方法
Cholesterol-PEG-Acid CLS-PEG-COOH Cholesterol-Polyethylene Glycol-Carboxyl Modified Peptides
化学试剂磷脂-聚乙二醇-氨基,DSPE-PEG-amine,CAS:474922-26-4
CAS:474922-22-0 Maleimide-PEG-DSPE Phospholipid-Polyethylene Glycol-Maleimide Brief Description
如何修改数据库密码
这些数组技巧,我爱了
DSPE-PEG-COOH CAS: 1403744-37-5 Phospholipid-polyethylene glycol-carboxy lipid PEG conjugate
VTK环境配置
Chemical Reagent Phospholipid-Polyethylene Glycol-Amino, DSPE-PEG-amine, CAS: 474922-26-4
cv2.resize()是反的
wangeditor富文本编辑器上传图片以及跨域问题解决