当前位置:网站首页>[opencv450] salt and pepper noise demo
[opencv450] salt and pepper noise demo
2022-06-23 15:05:00 【Ten year dream Lab】

Salt and pepper noise
Source code :
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
// Salt and pepper noise function
void saltAndPepper(Mat image, int n)
{
int i = 0, j = 0, write_black = 0;
for (int k = 0; k < n; ++k)
{
// Randomly determine the position in the image
i = rand() % image.cols;// Take the remainder operation , Ensure that within the number of columns of the image
j = rand() % image.rows;// Take the remainder operation , Ensure that within the number of lines of the image
write_black = 1;// rand() % 2;// A variable that determines whether it is white noise or black noise
if (write_black == 0)
{// Add white noise
if (image.type() == CV_8UC1)
{// Process grayscale images
image.at<uchar>(j, i) = 255;// White noise
}
else if (image.type() == CV_8UC3) {// Process color images
image.at<Vec3b>(j, i)[0] = 255;//Vec3b by opencv Defined 3 Vector type of a value
image.at<Vec3b>(j, i)[1] = 255;//[] Establish access ,B:0,G:1,R:2
image.at<Vec3b>(j, i)[2] = 255;
}
}
else
{// Add black noise
if (image.type() == CV_8UC1)
{
image.at<uchar>(j, i) = 0;
}
if (image.type() == CV_8UC3)
{
image.at<Vec3b>(j, i)[0] = 0;//Vec3b by opencv Defined 3 Vector type of a value
image.at<Vec3b>(j, i)[1] = 0;//[] Establish access ,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 << " Please confirm whether the entered path is correct " << endl;
return -1;
}
imshow("luffy Original picture ", img);
imshow("luffy_gray Original picture ", gray);
saltAndPepper(img, 1000);// Add salt and pepper noise to color images
saltAndPepper(gray, 1000);// Add salt and pepper noise to gray image
imshow("luffy Add noise ", img);
imshow("luffy_gray Add noise ", gray);
imwrite(".\\salt5.jpg", img);
waitKey(0);
destroyAllWindows();
return 0;
}Reference resources :
https://blog.csdn.net/weixin_51326570/article/details/115184484
边栏推荐
猜你喜欢

乐高宣布涨价,炒家更嗨皮了

Simple tutorial of live streaming with OBS

阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题

Summary of operating system underlying knowledge (interview)
![[in depth understanding of tcapulusdb technology] tcapulusdb business data backup](/img/1a/449b123c0f2046217ec60af36344eb.png)
[in depth understanding of tcapulusdb technology] tcapulusdb business data backup
![[Level 2 warranty] which brand of Fortress machine is good for Level 2 warranty?](/img/84/4fa07c54786aaf423ca333ecde62f4.png)
[Level 2 warranty] which brand of Fortress machine is good for Level 2 warranty?

加快 yarn install 的三个简单技巧

General sequence representation learning in kdd'22 "Ali" recommendation system

2021-04-15

Babbitt | metauniverse daily must read: meta, Microsoft and other technology giants set up the metauniverse Standards Forum. Huawei and Alibaba joined. NVIDIA executives said that they welcomed partic
随机推荐
等保备案是什么意思?应该去哪里办理备案?
ACM Player Illustration leetcode remove element
JS垃圾回收
《墨者学院——SQL手工注入漏洞测试(MySQL数据库)》
MySQL 创建和管理表
Introduction to helm basics helm introduction and installation
2021-06-03
When I went to oppo for an interview, I got numb...
Effect evaluation of regression model under credit product quota pricing scenario
RF Analyzer Demo搭建
LEGO announces price increase, speculators are more excited
[datahub] LinkedIn datahub learning notes
聚合生态,使能安全运营,华为云安全云脑智护业务安全
After nine years at the helm, the founding CEO of Allen Institute retired with honor! He predicted that Chinese AI would lead the world
php 二维数组插入
Sectigo(Comodo)证书的由来
The well-known face search engine provokes public anger: just one photo will strip you of your pants in a few seconds
Xampp中mysql无法启动问题的解决方法
2021-06-07
中国矿大团队,开发集成多尺度深度学习模型,用于 RNA 甲基化位点预测