opencv初学习-椒盐噪声-中值滤波-均值滤波-腐蚀膨胀

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv初学习-椒盐噪声-中值滤波-均值滤波-腐蚀膨胀相关的知识,希望对你有一定的参考价值。

#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;
}*/

以上是关于opencv初学习-椒盐噪声-中值滤波-均值滤波-腐蚀膨胀的主要内容,如果未能解决你的问题,请参考以下文章

中值滤波

opencv学习笔记15种图像滤波辨析:方框均值高斯中值双边

均值滤波

OpenCV图像处理之图像平滑

opencv学习-均值滤波高斯滤波中值滤波双边滤波

几种常见空间滤波器MATLAB实现