码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-04-29 18:44:09      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:lte   min   char   排序   学习   random   gen   open   array   

#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初学习-椒盐噪声-中值滤波-均值滤波-腐蚀膨胀

标签:lte   min   char   排序   学习   random   gen   open   array   

原文地址:http://www.cnblogs.com/xujingxin1997/p/6785658.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!