实验内容及实验原理:
1.用均值滤波器(即邻域平均法)去除图像中的噪声;
2.用中值滤波器去除图像中的噪声
3.比较两种方法的处理结果
实验步骤:
用原始图像lena.bmp或cameraman.bmp加产生的3%椒盐噪声图像合成一幅有噪声的图像并显示;
1.用均值滤波器去除图像中的噪声(选3x3窗口):以当前像素点为中心,求窗口内所有灰度值的和,以其平均值作为中心像素新的灰度值
2. 用中值滤波器去除图像中的噪声(选3x3窗口做中值滤波);以当前像素点为中心,求窗口中所有像素点的灰度值的中值,以中值作为中心像素点的值
3.将两种处理方法的结果与原图比较,注意两种处理方法对边缘的影响。
以3%的几率产生随机为0/255的灰度值覆盖原来的灰度值
for (int i = 0; i height; i++){
for (int j = 0; j width; j++){
CvScalar s = cvGet2D(src, i,j);
srand(seed++);
if (rand() % 100 <3){//3%的几率覆盖
srand(seed++);
s.val[0]= rand() % 2 * 255;//随机是0还是255
}
cvSet2D(dst,i, j, s);
}
}
添加后:
计算当前像素周围的九个像素点的灰度值和,以其平均值作为当前像素的灰度值。如果是边缘的像素点就求有限的几个像素灰度值的平均值
for (int i = 0;i < dst->height; i++){
for (int j = 0; j < dst->width; j++){
CvScalar s =cvGet2D(dst, i, j);
double sum =0;//存放窗口中所有像素点灰度值的和
int num =0;//记录窗口中像素的个数,因为边界上可能不是9个像素
//求3x3的和
for (int k = -1; k <= 1; k++)
for (int m = -1; m <= 1; m++)
if (i + k>= 0 && i + k <= 255 && j + m >= 0 && j + m<= 255){
CvScalar temp =cvGet2D(dst, i+k, j+m);
sum += temp.val[0];
num++;
}
s.val[0] = sum/num;
cvSet2D(dst_sp, i, j, s);
}
}
获取当前像素位置周围的九个像素灰度值,排序选择中间值作为新的灰度值。如果是边界的像素点就只选择有限的几个灰度值的中值
for (int i = 0;i < dst->height; i++){
for (int j = 0; j < dst->width; j++){
CvScalar s =cvGet2D(dst, i, j);
double val[9]= {0},mid=0;//分别为存放窗口中所有灰度值的数组和中值
int num =0;//记录实际窗口中有几个像素点
//遍历当前像素点为中心的3X3窗口
for (int k = -1; k <= 1; k++)
for (int m = -1; m <= 1; m++)
if (i + k>= 0 && i + k <= 255 && j + m >= 0 && j + m<= 255){//在图像上,没有超出边界
CvScalar temp =cvGet2D(dst, i + k, j + m);
val[num++]= temp.val[0];
}
//冒泡排序,从大到小
for (int k = 0; k < 9; k++){
for (int m = 0; m < 8-k; m++){
if (val[m]< val[m + 1]){
double temp =val[m];
val[m] = val[m + 1];
val[m + 1] = temp;
}
}
}
s.val[0] = val[num/2];//求中值
cvSet2D(dst_sp2, i, j, s);
}
}
// opencv1.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace cv;
int main()
{
IplImage *src;
src = cvLoadImage("lena.bmp", 1);//原图
IplImage *dst = cvCreateImage(cvSize(src->width, src->height), IPL_DEPTH_8U, 1);
std::cout << "添加椒盐噪声" << std::endl;
int seed = 1;
for (int i = 0; i < src->height; i++)
{
for (int j = 0; j < src->width; j++)
{
CvScalar s = cvGet2D(src, i, j);
srand(seed++);
if (rand() % 100 < 3)
{
srand(seed++);
s.val[0] = rand() % 2 * 255;
}
cvSet2D(dst, i, j, s);
}
}
cvNamedWindow("Image1", 1);//创建窗口
cvShowImage("Image1", dst);//显示图像
IplImage *dst_sp = cvCreateImage(cvSize(dst->width, dst->height), IPL_DEPTH_8U, 1);
std::cout << "均值滤波" << std::endl;
for (int i = 0; i < dst->height; i++)
{
for (int j = 0; j < dst->width; j++)
{
CvScalar s = cvGet2D(dst, i, j);
double sum = 0;
int num = 0;
//求3x3的和
for (int k = -1; k <= 1; k++)
for (int m = -1; m <= 1; m++)
if (i + k >= 0 && i + k <= 255 && j + m >= 0 && j + m <= 255)
{
CvScalar temp = cvGet2D(dst, i+k, j+m);
sum += temp.val[0];
num++;
}
s.val[0] = sum/num;
cvSet2D(dst_sp, i, j, s);
}
}
cvNamedWindow("Image2", 1);//创建窗口
cvShowImage("Image2", dst_sp);//显示图像
IplImage *dst_sp2 = cvCreateImage(cvSize(dst->width, dst->height), IPL_DEPTH_8U, 1);
std::cout << "中值滤波" << std::endl;
for (int i = 0; i < dst->height; i++)
{
for (int j = 0; j < dst->width; j++)
{
CvScalar s = cvGet2D(dst, i, j);
double val[9] = {0},mid=0;
int num = 0;
for (int k = -1; k <= 1; k++)
for (int m = -1; m <= 1; m++)
if (i + k >= 0 && i + k <= 255 && j + m >= 0 && j + m <= 255)
{
CvScalar temp = cvGet2D(dst, i + k, j + m);
val[num++]= temp.val[0];
}
//排序,从大到小
for (int k = 0; k < 9; k++)
{
for (int m = 0; m < 8-k; m++)
{
if (val[m] < val[m + 1])
{
double temp = val[m];
val[m] = val[m + 1];
val[m + 1] = temp;
}
}
}
s.val[0] = val[num/2];
cvSet2D(dst_sp2, i, j, s);
}
}
cvNamedWindow("Image3", 1);//创建窗口
cvShowImage("Image3", dst_sp2);//显示图像
cvWaitKey(0); //等待按键
cvReleaseImage(&dst_sp2); //释放图像
cvReleaseImage(&dst_sp); //释放图像
cvReleaseImage(&dst); //释放图像
}
原图:(原来是bmp格式的,但是不能上传,改成png格式了,分辨率都是256X256的)