当前位置:网站首页>Get to know opencv4.x for the first time --- add salt and pepper noise to the image
Get to know opencv4.x for the first time --- add salt and pepper noise to the image
2022-07-25 09:47:00 【F l e】
// Add salt and pepper noise to the image
#include <stdio.h>
#include <iostream>
#include <string>
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;
void add_salt(Mat & img);
int main()
{
// Salt and pepper noise , As the name suggests, it is to sprinkle white salt and black pepper on the image
Mat img = imread("person3.jpeg");// Read the three channel image
namedWindow(" Original picture ", WINDOW_NORMAL);
imshow(" Original picture ", img);
for (int i = 0; i < img.cols*img.rows / 10; i++)
{
add_salt(img);
}
namedWindow(" Salt and pepper map ", WINDOW_NORMAL);
imshow(" Salt and pepper map ", img);
waitKey(0);
return 0;
}
void add_salt(Mat & img)
{
int x = std::rand() % img.cols;// Get the column position where the noise is increased
int y = std::rand() % img.rows;// Get the row position where the noise is increased
int select = std::rand() % 100;// And 50 Comparison is used to decide whether to sprinkle salt or pepper
if (img.channels() == 3)
{
if (select >= 50)// Sprinkle salt
{
img.at<Vec3b>(y, x)[0] = 255;
img.at<Vec3b>(y, x)[1] = 255;
img.at<Vec3b>(y, x)[2] = 255;
}
else// Sprinkle black pepper
{
img.at<Vec3b>(y, x)[0] = 0;
img.at<Vec3b>(y, x)[1] = 0;
img.at<Vec3b>(y, x)[2] = 0;
}
}
else// grayscale
{
if (select >= 50)// Sprinkle salt
{
img.at<float>(y, x) = 255;
}
else// Sprinkle black pepper
{
img.at<float>(y, x) = 0;
}
}
}

边栏推荐
- 初识Opencv4.X----方框滤波
- UI原型资源
- A number converted from a decimal integer to another base
- Flutter rive multi state example
- OC -- Foundation -- string + date and time
- Browser access to swagger failed with error err_ UNSAFE_ PORT
- A picture explains SQL join left and right
- Learn redis Linux and install redis
- 【数据挖掘】第二章 认识数据
- OC--包装类和处理对象
猜你喜欢

【Android studio】批量数据导入到android 本地数据库

初识Opencv4.X----图像直方图匹配

Matlab drawing | some common settings of axis

@3-1 CCF 2020-09-1 scale detection point query

*6-3 save small experts
![[gplt] 2022 popular lover (Floyd)](/img/30/c96306ca0a93f22598cec80edabd6b.png)
[gplt] 2022 popular lover (Floyd)

Singleton mode

The shortest path problem Bellman Ford (single source shortest path) (illustration)

OC -- first acquaintance

Kotlin basic knowledge points
随机推荐
How to convert object data into arrays
A picture explains SQL join left and right
2022年的个人技术选型梳理
Swift创作天气APP
Flex layout syntax and use cases
Stm32+hc05 serial port Bluetooth design simple Bluetooth speaker
[code source] score split of one question per day
Constant power wireless charging based on stm32
Some skills to reduce the complexity of program space
The jar package has been launched on Alibaba cloud server and the security group has been opened, but postman still can't run. What should we do
OC -- Foundation -- string + date and time
微信小程序初步了解及实现底部导航栏
@1-1 CCF 2021-04-1 gray histogram
对象初始化
## 使用 Kotlin USE 简化文件读写
十进制整数转换为其它进制的数
[code source] daily one question non decreasing 01 sequence
Browser access to swagger failed with error err_ UNSAFE_ PORT
OC--初识
Raspberry sect door ban system based on face recognition