当前位置:网站首页>初识Opencv4.X----为图像添加椒盐噪声
初识Opencv4.X----为图像添加椒盐噪声
2022-07-25 09:22:00 【F l e】
//为图像添加椒盐噪声
#include <stdio.h>
#include <iostream>
#include <string>
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;
void add_salt(Mat & img);
int main()
{
//椒盐噪声,顾名思义就是在图像上撒上白色的盐和黑色的小黑椒
Mat img = imread("person3.jpeg");//读取的是三通道图像
namedWindow("原图", WINDOW_NORMAL);
imshow("原图", img);
for (int i = 0; i < img.cols*img.rows / 10; i++)
{
add_salt(img);
}
namedWindow("椒盐图", WINDOW_NORMAL);
imshow("椒盐图", img);
waitKey(0);
return 0;
}
void add_salt(Mat & img)
{
int x = std::rand() % img.cols;//得到增加噪点的列位置
int y = std::rand() % img.rows;//得到增加噪点的行位置
int select = std::rand() % 100;//与50比较用于决定是撒盐还是撒黑椒
if (img.channels() == 3)
{
if (select >= 50)//撒盐
{
img.at<Vec3b>(y, x)[0] = 255;
img.at<Vec3b>(y, x)[1] = 255;
img.at<Vec3b>(y, x)[2] = 255;
}
else//撒黑椒
{
img.at<Vec3b>(y, x)[0] = 0;
img.at<Vec3b>(y, x)[1] = 0;
img.at<Vec3b>(y, x)[2] = 0;
}
}
else//灰度图
{
if (select >= 50)//撒盐
{
img.at<float>(y, x) = 255;
}
else//撒黑椒
{
img.at<float>(y, x) = 0;
}
}
}

边栏推荐
猜你喜欢
随机推荐
How to obtain location information (longitude and latitude) by uni app
Class (2) and protocol
## 使用 Kotlin USE 简化文件读写
【代码源】每日一题 国家铁路
Redis list 结构命令
OC--Foundation--数组
*6-2 CCF 2015-03-3 Festival
A brief introduction to the interest of convolutional neural networks
[code source] daily problem segmentation (nlogn & n solution)
如何将Jar包部署到服务器,注:启动命令有无nohup有很大关系
[code source] daily question farmland Division
OC -- category extension agreement and delegation
pdf2Image Pdf文件存为jpg NodeJs实现
How to deploy the jar package to the server? Note: whether the startup command has nohup or not has a lot to do with it
OC--Foundation--字典
Object initialization
[code source] daily question tree
Redis installation (Ubuntu)
[HCTF 2018]admin
OC--Foundation--字符串+日期和时间









