实验二 图像增强(1)
一、实验目的
1、熟悉并掌握MATLAB图像处理工具箱的使用;
2、理解并掌握常用的图像增强技术。
二、实验环境
vs2019+opencv
三、实验内容
1、为图像分别加高斯噪声和椒盐噪声,采用中值滤波方法对受噪声干扰的图像滤波,窗口分别采用3*3,5*5,7*7;
2、对受噪声干扰的图像进行均值滤波(简单均值滤波、加权均值滤波、阈值均值滤波);
3、分别采用Roberts 算子、Sobel算子、Prewitt算子和Log算子进行图像锐化;
4、对一幅低对比度分辨率的图像采用直方图均衡化方法实现图像增强,分别采用系统函数和自己编写函数实现相应用功能;
5、对一幅图像分别进行傅里叶变换和离散余弦变换,并把傅里叶变换直流分量移到频谱中心。
四、代码
1、为图像分别加高斯噪声和椒盐噪声,采用中值滤波方法对受噪声干扰的图像滤波,窗口分别采用3*3,5*5,7*7;
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
#define TWO_PI 6.2831853071795864769252866
Mat g_srcImage, g_dstImage1, g_dstImage3,m1;//存储图片的Mat类型
static void salt(cv::Mat& image, int n); //给图像添加椒盐噪声
static void AddGaussianNoise(cv::Mat& image); //给图像加高斯噪声
int main()
{
// 载入原图
g_srcImage = imread("C:\\Users\\23128\\Pictures\\Saved Pictures\\16.jpg", 1);
if (!g_srcImage.data) { printf("Oh,no,读取srcImage错误~! \n"); return false; }
//克隆原图到三个Mat类型中
g_dstImage1 = g_srcImage.clone();
g_dstImage3 = g_srcImage.clone();
//显示原图
namedWindow("【<0>原图窗口】", 0);
resizeWindow("【<0>原图窗口】", 400, 400);
imshow("【<0>原图窗口】", g_srcImage);
//=================【<1>加椒盐噪声】==================
namedWindow("【<1>加椒盐噪声】", 0);
salt(g_dstImage1, 4000);
resizeWindow("【<1>加椒盐噪声】", 400, 400);
imshow("【<1>加椒盐噪声】", g_dstImage1);
//=================【<3>加高斯噪声】==================
namedWindow("【<3>加高斯噪声】", 0);
AddGaussianNoise(g_dstImage3);
resizeWindow("【<3>加高斯噪声】", 400, 400);
imshow("【<3>加高斯噪声】", g_dstImage3);
Mat out3, out5, out7, out33, out55, out77;
medianBlur(g_dstImage1,out3,3);
namedWindow("3*3处理椒盐噪声", 0);
resizeWindow("3*3处理椒盐噪声", 400, 400);
imshow("3*3处理椒盐噪声", out3);
medianBlur(g_dstImage1, out5, 5);
namedWindow("5*5处理椒盐噪声", 0);
resizeWindow("5*5处理椒盐噪声", 400, 400);
imshow("5*5处理椒盐噪声", out5);
medianBlur(g_dstImage1, out7, 7);
namedWindow("7*7处理椒盐噪声", 0);
resizeWindow("7*7处理椒盐噪声", 400, 400);
imshow("7*7处理椒盐噪声", out7);
medianBlur(g_dstImage3, out33, 3);
namedWindow("3*3处理高斯噪声", 0);
resizeWindow("3*3处理高斯噪声", 400, 400);
imshow("3*3处理高斯噪声", out33);
medianBlur(g_dstImage3, out55, 5);
namedWindow("5*5处理高斯噪声", 0);
resizeWindow("5*5处理高斯噪声", 400, 400);
imshow("5*5处理高斯噪声", out55);
medianBlur(g_dstImage3, out77, 7);
namedWindow("7*7处理高斯噪声", 0);
resizeWindow("7*7处理高斯噪声", 400, 400);
imshow("7*7处理高斯噪声", out77);
//按下“q”键时,程序退出
while (char(waitKey(1)) != 'q') {}
return 0;
}
//给图像加椒盐噪声
static void salt(cv::Mat& image, int n)
{
int i, j;
fo