当前位置:网站首页>初识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;
}
}
}

边栏推荐
猜你喜欢

深入解读C语言随机数函数和如何实现随机数

OC -- first acquaintance

初识Opencv4.X----图像直方图均衡

语音聊天app源码-钠斯网络源码出品

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

Redis installation (Ubuntu)

Stm32+hc05 serial port Bluetooth design simple Bluetooth speaker

Data query language (DQL)

OC -- Foundation -- array
![[gplt] 2022 popular lover (Floyd)](/img/30/c96306ca0a93f22598cec80edabd6b.png)
[gplt] 2022 popular lover (Floyd)
随机推荐
Jar包在阿里云服务器起起来了,安全组也开通了,但postman仍跑不通怎么办
Redis string 结构命令
Kotlin协程:协程的基础与使用
Prim 最小生成树(图解)
Basic network knowledge
为什么要使用JSON.stringify()和JSON.parse()
*6-1 CCF 2015-03-2 数字排序
【代码源】 每日一题 素数之欢(bfs)
@3-1 CCF 2020-09-1 scale detection point query
[code source] daily problem segmentation (nlogn & n solution)
[code source] a prime number of fun every day (BFS)
main函数的一些操作
Redis set structure command
Laravel calls a third party to send mail (PHP)
@1-1 CCF 2021-04-1 灰度直方图
Week summary
类(2) 和 协议
Job 7.15 shell script
最短路问题 Bellman-Ford(单源最短路径)(图解)
Numpy - Construction of array