当前位置:网站首页>【opencv450】椒盐噪声demo
【opencv450】椒盐噪声demo
2022-06-23 14:20:00 【十年一梦实验室】

椒盐噪声
源码:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
//椒盐噪声函数
void saltAndPepper(Mat image, int n)
{
int i = 0, j = 0, write_black = 0;
for (int k = 0; k < n; ++k)
{
//随机确定图像中位置
i = rand() % image.cols;//取余数运算,保证在图像的列数内
j = rand() % image.rows;//取余数运算,保证在图像的行数内
write_black = 1;// rand() % 2;//判定为白色噪声还是黑色噪声的变量
if (write_black == 0)
{//添加白色噪声
if (image.type() == CV_8UC1)
{//处理灰度图像
image.at<uchar>(j, i) = 255;//白色噪声
}
else if (image.type() == CV_8UC3) {//处理彩色图像
image.at<Vec3b>(j, i)[0] = 255;//Vec3b为opencv定义的3个值的向量类型
image.at<Vec3b>(j, i)[1] = 255;//[]制定通道,B:0,G:1,R:2
image.at<Vec3b>(j, i)[2] = 255;
}
}
else
{//添加黑噪声
if (image.type() == CV_8UC1)
{
image.at<uchar>(j, i) = 0;
}
if (image.type() == CV_8UC3)
{
image.at<Vec3b>(j, i)[0] = 0;//Vec3b为opencv定义的3个值的向量类型
image.at<Vec3b>(j, i)[1] = 0;//[]制定通道,B:0,G:1,R:2
image.at<Vec3b>(j, i)[2] = 0;
}
}
}
}
int main() {
Mat img_ = imread(".\\5.jpg");
Mat img;
resize(img_, img, Size(img_.cols / 2, img_.rows / 2));
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
if (img.empty() || gray.empty()) {
cout << "请确认输入的路径是否正确" << endl;
return -1;
}
imshow("luffy原图", img);
imshow("luffy_gray原图", gray);
saltAndPepper(img, 1000);//彩色图像添加椒盐噪声
saltAndPepper(gray, 1000);//灰度图像添加椒盐噪声
imshow("luffy添加噪声", img);
imshow("luffy_gray添加噪声", gray);
imwrite(".\\salt5.jpg", img);
waitKey(0);
destroyAllWindows();
return 0;
}参考:
https://blog.csdn.net/weixin_51326570/article/details/115184484
边栏推荐
- 2021-04-15
- 狂奔的极兔,摔了一跤
- Technology sharing | do you understand the requirements of the tested project?
- General sequence representation learning in kdd'22 "Ali" recommendation system
- 2021-05-08
- Why is Xiaomi stuck in the chip quagmire?
- Short talk about community: how to build a game community?
- The first public available pytorch version alphafold2 is reproduced, and Columbia University is open source openfold, with more than 1000 stars
- 腾讯云服务器发送邮件失败
- MySQL create and manage tables
猜你喜欢

As a software testing practitioner, do you understand your development direction?

2021-05-08

MySQL 创建和管理表
![[datahub] LinkedIn datahub learning notes](/img/ca/9c4a87d38155edd093cbb81d81ee81.png)
[datahub] LinkedIn datahub learning notes

分布式数据库使用逻辑卷管理存储之扩容

Simple tutorial of live streaming with OBS

掌舵9年,艾伦研究所创始CEO光荣退休!他曾预言中国AI将领跑世界

After nine years at the helm, the founding CEO of Allen Institute retired with honor! He predicted that Chinese AI would lead the world

巴比特 | 元宇宙每日必读:Meta、微软等科技巨头成立元宇宙标准论坛组织,华为、阿里加入,英伟达高管称欢迎来自加密世界的参与者...

LEGO announces price increase, speculators are more excited
随机推荐
用OBS做直播推流简易教程
useState vs useRef 和 useReducer:相同点、不同点和用例
The first public available pytorch version alphafold2 is reproduced, and Columbia University is open source openfold, with more than 1000 stars
2022 ICT market in China continues to rise and enterprise digital infrastructure is imperative
MySQL 创建和管理表
Why is Xiaomi stuck in the chip quagmire?
After nine years at the helm, the founding CEO of Allen Institute retired with honor! He predicted that Chinese AI would lead the world
如何使用笔记软件 FlowUs、Notion 进行间隔重复?基于公式模版
High quality coding - air quality map visualization
Introduction to helm basics helm introduction and installation
go语言的变量声明
Google &huggingface| zero sample language model structure with the strongest ability
港股今年最大IPO来了,660亿身家,坐在矿山上的“大王”
AI intelligent robot saves us time and effort
Ie mode of selenium edge
k8s--部署单机版MySQL,并持久化
2021-05-08
[compréhension approfondie de la technologie tcaplusdb] données de construction tcaplusdb
The largest IPO of Hong Kong stocks this year, with a net worth of 66billion, is the "King" sitting on the mine
[in depth understanding of tcapulusdb technology] tcapulusdb business data backup