当前位置:网站首页>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!您的鼓励是我创作的最大动力!
边栏推荐
- Web Screenshots and Reverse Proxy
- 2021-09-30
- Podspec automatic upgrade script
- opencv之图像二值化处理
- Navicat从本地文件中导入sql文件
- CAS:474922-22-0 Maleimide-PEG-DSPE 磷脂-聚乙二醇-马来酰亚胺简述
- DingTalk Enterprise Internal-H5 Micro Application Development
- Jupyter内核正忙、内核挂掉
- Cholesterol-PEG-NHS NHS-PEG-CLS cholesterol-polyethylene glycol-active ester can modify small molecular materials
- 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.
猜你喜欢

机器学习和深度学习概述

活体检测CDCN学习笔记

Getting Started with MySQL: The Case Statement Works Well

Cholesterol-PEG-Acid CLS-PEG-COOH Cholesterol-Polyethylene Glycol-Carboxyl Modified Peptides

DSPE-PEG-COOH CAS: 1403744-37-5 Phospholipid-polyethylene glycol-carboxy lipid PEG conjugate

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

CAS:1403744-37-5 DSPE-PEG-FA 科研实验用磷脂-聚乙二醇-叶酸

Multi-Modal Face Anti-Spoofing Based on Central Difference Networks学习笔记

Wangeditor rich text editor to upload pictures and solve cross-domain problems

Pytorch每日一练——预测泰坦尼克号船上的生存乘客
随机推荐
DSPE-PEG-Biotin, CAS: 385437-57-0, phospholipid-polyethylene glycol-biotin prolongs circulating half-life
TransactionTemplate transaction programmatic way
数据预处理、特征工程和特征学习-摘抄
【Latex】TexLive+VScode+SumatraPDF 配置LaTex编辑环境
应用usb_cam同时打开多个摄像头方法
CAS: 1403744-37-5 DSPE-PEG-FA Phospholipid-Polyethylene Glycol-Folic Acid for Scientific Research
[已解决]ssh连接报:Bad owner or permissions on C:\\Users/XXX/.ssh/config
2021年软件测试面试题大全
UR3机器人运动学分析之逆运动学分析
UR3机器人雅克比矩阵
科研试剂Cholesterol-PEG-Maleimide,CLS-PEG-MAL,胆固醇-聚乙二醇-马来酰亚胺
Web Screenshots and Reverse Proxy
mysql 事务原理详解
np.fliplr与np.flipud
Cholesterol-PEG-Amine CLS-PEG-NH2 胆固醇-聚乙二醇-氨基科研用
Research reagents Cholesterol-PEG-Maleimide, CLS-PEG-MAL, Cholesterol-PEG-Maleimide
活体检测PatchNet学习笔记
2022 SQL big factory high-frequency practical interview questions (detailed analysis)
解决background-size:cover时图片铺满但显示不完整?
Getting Started with MySQL: The Case Statement Works Well