当前位置: 首页 > news >正文

【c++|opencv】一、基础操作---3.访问图像元素

every blog every motto: You can do more than you think.
https://blog.csdn.net/weixin_39190382?type=blog

0. 前言

访问图像元素

1. 访问图像像素

1.1 访问某像素

//灰度图像:
image.at<uchar>(j, i) //j为行数,i为列数
//BGR彩色图像
image.at<Vec3b>(j, i)[0] //B分量
image.at<Vec3b>(j, i)[1] //G分量
image.at<Vec3b>(j, i)[2] //R分量

1.2 遍历像素

以添加噪声为例

1.2.1 准备

创建添加噪声函数

Salt.h

#pragma once
#include <iostream>
#include <opencv2/opencv.hpp>
#include <random>using namespace cv;
using namespace std;void Salt(Mat img,int n); // n加入噪声点数

Salt.cpp

#include "Salt.h"void Salt(Mat img,int n){// 随机数生成器default_random_engine generater;uniform_int_distribution<int> randRow(0,img.rows-1);uniform_int_distribution<int> randCol(0,img.cols-1);int i,j;for (int k=0;k<n;k++){i=randRow(generater);j=randCol(generater);if (img.channels() == 1){img.at<uchar>(i,j) = 255;}else if (img.channels() == 3){img.at<Vec3b>(i,j)[0] = 255;img.at<Vec3b>(i,j)[1] = 255;img.at<Vec3b>(i,j)[2] = 255;}}
}

1.2.2 添加噪声

#include "Salt.h"
#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main(){Mat img = imread("/home/v/home.png");if (img.empty()){cout<<"Could not open or find the image"<<endl;return -1;}imshow("Img",img);Salt(img,5000); // 加入噪声点imshow("Salt",img);waitKey(0);return 0;
}

在这里插入图片描述

1.3 指针遍历

1.3.1 以卷积运算为例

#include "Salt.h"
#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main(){Mat img = imread("/home/v/home.png");if (img.empty()){cout<<"Could not open or find the image"<<endl;return -1;}Mat out_img;out_img = Mat(img.size(),img.type()); // 定义输出图像大小out_img = img.clone(); // clone原图像像素值int rows = img.rows; // 原图行数int stepx = img.channels(); // 原图通道数int cols = (img.cols)*img.channels(); // 矩阵总列数for (int row=1;row<rows-1;row++){const uchar* previous = img.ptr<uchar>(row-1); // 原图上一行指针const uchar* current = img.ptr<uchar>(row); // 原图当前行指针const uchar* next = img.ptr<uchar>(row+1); // 原图下一行指针uchar* output = out_img.ptr<uchar>(row); // 输出图像当前指针for (int col=stepx;col<cols-stepx;col++){ // 对列进行遍历// saturate_cast<uchar>(a) 当a小于0,输出0,当a大于255输出255,0-255之间原样输出output[col] = saturate_cast<uchar>(5*current[col]-(previous[col]+current[col-stepx] + current[col+stepx] + next[col]));}}imshow("Img",img);imshow("Out",out_img);waitKey(0);return 0;
}

在这里插入图片描述

1.3.2 自带的卷积运算

#include "Salt.h"
#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main(){Mat img = imread("/home/v/home.png");if (img.empty()){cout<<"Could not open or find the image"<<endl;return -1;}Mat out_img;Mat kernel = (Mat_<char>(3,3)<<0,-1,0,-1,5,-1,0,-1,0);filter2D(img,out_img,img.depth(),kernel); // 卷积imshow("Img",img);imshow("Out",out_img);waitKey(0);return 0;
}

在这里插入图片描述

http://www.lryc.cn/news/213415.html

相关文章:

  • 机器视觉3D项目评估的基本要素及测量案例分析
  • 力扣日记10.31-【栈与队列篇】前 K 个高频元素
  • TensorFlow案例学习:简单的音频识别
  • css小程序踩坑记录
  • Android sqlite分页上传离线订单后删除
  • Flask基本教程以及Jinjia2模板引擎简介
  • Django实战项目-学习任务系统-兑换物品管理
  • jmeter和postman你选哪个做接口测试?
  • mac版本 Adobe总是弹窗提示验证问题如何解决
  • 钡铼技术ARM工控机在机器人控制领域的应用
  • HTML+CSS+JS实现计算器
  • Git工作原理和常见问题处理方案
  • C++-实现一个简单的菜单程序
  • Git更新 fork 的仓库
  • chorme安装esay scholar及chrome 无法从该网站添加应用、扩展程序和用户脚本解决方案
  • 数据库-扩展语句,约束方式
  • 精密数据工匠:探索 Netty ChannelHandler 的奥秘
  • Python四种基本结构的操作
  • Eureka:com.netflix.discovery.TimedSupervisorTask - task supervisor timed out
  • 1.spark standalone环境安装
  • 【问题解决】 avue dicUrl 动态参数加载字典数据(已解决)
  • ​学习一下,什么是预包装食品?​
  • 从零开始学习搭建量化平台笔记
  • 【whisper】在python中调用whisper提取字幕或翻译字幕到文本
  • git diff对比差异时指定或排除特定的文件和目录
  • 数据结构介绍与时间、空间复杂度
  • (c语言进阶)字符串函数、字符分类函数和字符转换函数
  • 解决MySQL大版本升级导致.Net(C#)程序连接报错问题
  • Java 将对象List转为csv文件并上传远程文件服务器实现方案
  • 分享8个分布式Kafka的使用场景