博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
opencv初学习-椒盐噪声-中值滤波-均值滤波-腐蚀膨胀
阅读量:5291 次
发布时间:2019-06-14

本文共 6247 字,大约阅读时间需要 20 分钟。

#include <opencv2\opencv.hpp>

#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgcodecs\imgcodecs.hpp>
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<iomanip>
void salt(cv::Mat image, int n);
void pepper(cv::Mat image, int n);
void medeanFilter(cv::Mat& src, int win_size);
void meanFilter(cv::Mat& src, int size);
using namespace cv;
using namespace std;
//int Max(vector<uchar> Array, int size);
void Open(Mat& src,Mat& img, int size);
//int Min(vector<uchar> Array1, int size);
void Close(Mat& src, Mat& img, int size);
int main() {
Mat img= imread("2.jpg");
Mat image = imread("2.jpg");//原图
imshow("原图",image);

//salt(image, 3000);//加入盐噪声255

//pepper(image, 3000);//加入椒噪声0
//imshow("椒盐噪声", image);//椒盐噪声图片

//medeanFilter(image, 3);

//imshow("中值滤波",image);//中值滤波图片

//meanFilter(image,3);

//imshow("均值滤波",image);//均值滤波图片

Open(image,img,3);

imshow("开运算-膨胀", img);//膨胀

//Close(image, img,3);

//imshow("闭运算-腐蚀",img);//腐蚀

waitKey(0);

return 0;

}

void salt(Mat image, int n) {

int i, j;

for (int k = 0; k<n / 2; k++) {

i = rand() % image.cols; // % 整除取余数运算符,rand=1022,cols=1000,rand%cols=22

j = rand() % image.rows;

if (image.type() == CV_8UC1) { // gray-level image

image.at<uchar>(j, i) = 255; //at方法需要指定Mat变量返回值类型,如uchar等

}

else if (image.type() == CV_8UC3) { // color image

image.at<Vec3b>(j, i)[0] = 255; //cv::Vec3b为opencv定义的一个3个值的向量类型

image.at<Vec3b>(j, i)[1] = 255; //[]指定通道,B:0,G:1,R:2
image.at<Vec3b>(j, i)[2] = 255;
}
}
}

void pepper(cv::Mat image, int n) {

int i, j;
for (int k = 0; k < n; k++) {

// rand() is the random number generator

i = rand() % image.cols; // % 整除取余数运算符,rand=1022,cols=1000,rand%cols=22
j = rand() % image.rows;

if (image.type() == CV_8UC1) { // gray-level image

image.at<uchar>(j, i) = 0; //at方法需要指定Mat变量返回值类型,如uchar等

}

else if (image.type() == CV_8UC3) { // color image

image.at<Vec3b>(j, i)[0] = 0; //cv::Vec3b为opencv定义的一个3个值的向量类型

image.at<Vec3b>(j, i)[1] = 0; //[]指定通道,B:0,G:1,R:2
image.at<Vec3b>(j, i)[2] = 0;
}
}
}

//中值滤波

void medeanFilter(Mat& src, int size) {
int row = src.rows, col= src.cols;
int start = size / 2;
for (int m = start; m <row - start; m++) {
for (int n = start; n < col - start; n++) {
vector<uchar> Array;
for (int i = -start + m; i <= start + m; i++) {
for (int j = -start + n; j <= start + n; j++) {
Array.push_back(src.at<Vec3b>(i, j)[0]);
}
}
sort(Array.begin(), Array.end());//快速排序
src.at<Vec3b>(m, n)[0] = Array[size*size / 2];
}

for (int n = start; n < col - start; n++) {

vector<uchar> Array;
for (int i = -start + m; i <= start + m; i++) {
for (int j = -start + n; j <= start + n; j++) {
Array.push_back(src.at<Vec3b>(i, j)[1]);
}
}
sort(Array.begin(), Array.end());
src.at<Vec3b>(m, n)[1] = Array[size*size / 2];
}

for (int n = start; n < col - start; n++) {

vector<uchar> Array;
for (int i = -start + m; i <= start + m; i++) {
for (int j = -start + n; j <= start + n; j++) {
Array.push_back(src.at<Vec3b>(i, j)[2]);
}
}
sort(Array.begin(), Array.end());
src.at<Vec3b>(m, n)[2] = Array[size*size / 2];
}
}
}
//row行,col列

//均值滤波:

void meanFilter(Mat& src,int size) {

int row = src.rows, col = src.cols;
int start = size / 2;
for (int m = start; m < row - start; m++) {

for (int n = start; n < col - start; n++) {

int sum = 0;
for(int i = -start + m; i <= start + m; i++) {
for (int j = -start + n; j <= start + n; j++)
{
sum = sum + src.at<Vec3b>(i, j)[0];
}
}
src.at<Vec3b>(m, n)[0] = uchar(sum / size / size);
}

for (int n = start; n < col - start; n++) {

int sum = 0;
for (int i = -start + m; i <= start + m; i++) {
for (int j = -start + n; j <= start + n; j++)
{
sum = sum + src.at<Vec3b>(i, j)[1];
}
}
src.at<Vec3b>(m, n)[1] = uchar(sum / size / size);
}

for (int n = start; n < col - start; n++) {

int sum = 0;
for (int i = -start + m; i <= start + m; i++) {
for (int j = -start + n; j <= start + n; j++)
{
sum = sum + src.at<Vec3b>(i, j)[2];
}
}
src.at<Vec3b>(m, n)[2] = uchar(sum / size / size);
}
}
}

//膨胀
void Open(Mat& src, Mat& img, int size) {
int row = src.rows, col = src.cols;
int start = size / 2;
for (int m = start; m < row - start; m++) {

for (int n = start; n < col - start; n++) {

vector<uchar> Array;
for (int i = -start + m; i <= start + m; i++) {
for (int j = -start + n; j <= start + n; j++) {
Array.push_back(src.at<Vec3b>(i, j)[0]);
}
}
sort(Array.begin(), Array.end());
img.at<Vec3b>(m, n)[0] = Array[8];
}

for (int n = start; n < col - start; n++) {

vector<uchar> Array;
for (int i = -start + m; i <= start + m; i++) {
for (int j = -start + n; j <= start + n; j++) {
Array.push_back(src.at<Vec3b>(i, j)[1]);
}
}
sort(Array.begin(), Array.end());
img.at<Vec3b>(m, n)[1] = Array[8];
}

for (int n = start; n < col - start; n++) {

vector<uchar> Array;
for (int i = -start + m; i <= start + m; i++) {
for (int j = -start + n; j <= start + n; j++) {
Array.push_back(src.at<Vec3b>(i, j)[2]);
}
}
sort(Array.begin(), Array.end());
img.at<Vec3b>(m, n)[2] = Array[8];
}
}
}

/*int Max(vector<uchar> Array1,int size) {

int max = Array1[0];
for (int i = 0; i < 9; i++) {
if (Array1[i] > max)
max = Array1[i];
}
return max;
}*/

//腐蚀
void Close(Mat& src, Mat& img, int size) {
int row = src.rows, col = src.cols;
int start = size / 2;
for (int m = start; m < row - start; m++) {

for (int n = start; n < col - start; n++) {

vector<uchar> Array;
for (int i = -start + m; i <= start + m; i++) {
for (int j = -start + n; j <= start + n; j++) {
Array.push_back(src.at<Vec3b>(i, j)[0]);
}
}
sort(Array.begin(), Array.end());
img.at<Vec3b>(m, n)[0] = Array[0];
}

for (int n = start; n < col - start; n++) {

vector<uchar> Array;
for (int i = -start + m; i <= start + m; i++) {
for (int j = -start + n; j <= start + n; j++) {
Array.push_back(src.at<Vec3b>(i, j)[1]);
}
}
sort(Array.begin(), Array.end());
img.at<Vec3b>(m, n)[1] = Array[0];
}

for (int n = start; n < col - start; n++) {

vector<uchar> Array;
for (int i = -start + m; i <= start + m; i++) {
for (int j = -start + n; j <= start + n; j++) {
Array.push_back(src.at<Vec3b>(i, j)[2]);
}
}
sort(Array.begin(), Array.end());
img.at<Vec3b>(m, n)[2] = Array[0];
}
}
}

/*int Min(vector<uchar> Array1, int size) {

int min = Array1[0];
for (int i = 0; i < 9; i++) {
if (Array1[i] < min)
min = Array1[i];
}
return min;
}*/

转载于:https://www.cnblogs.com/xujingxin1997/p/6785658.html

你可能感兴趣的文章
[51Nod1089] 最长回文子串 V2(Manacher算法)
查看>>
Asp.Net生命周期系列六
查看>>
php引用 =& 详解
查看>>
Codeforces 914D Bash and a Tough Math Puzzle (ZKW线段树)
查看>>
POJ 3009: Curling 2.0
查看>>
DLNA介绍(包含UPnP,2011/6/20 更新)
查看>>
ANGULARJS5从0开始(2) - 整合bootstrap和font-awesome
查看>>
Android 使用Parcelable序列化对象
查看>>
Python Web框架Django (零)
查看>>
Foxmail出现 错误信息:553 mailbox not found怎么解决
查看>>
spring_远程调用
查看>>
js 中基本数据类型和引用数据类型 ,,,, js中对象和函数的关系
查看>>
登录服务器,首先用到的5个命令
查看>>
多米诺骨牌
查看>>
区间DP 等腰三角形
查看>>
mysql 存储引擎对索引的支持
查看>>
Linq 学习(1) Group & Join--网摘
查看>>
asp.net 调用前台JS调用后台,后台掉前台JS
查看>>
【转】iOS 宏(define)与常量(const)的正确使用-- 不错
查看>>
【转】iOS开发UI篇—iPad和iPhone开发的比较
查看>>