- 对灰度图进行平滑滤波(采用邻域平均模板
和高斯平均模板)lena_noise.yuv starsky.yuv - 对灰度图进行锐化滤波(分别采用拉普拉斯
算子和Sobel算子)moon.yuv
(一)所用图片格式说明
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ade24c23150a0005c7d88714160f8647.png#pic_center)
(二)实验代码
main.cpp
#include <iostream>
#include <cstdio>
#include <fstream>
#include "filtering.h"
using namespace std;
int main()
{
int lenawidth = 512;
int lenaheight = 512;
int moonwidth = 464;
int moonheight = 538;
int starskywidth = 485;
int starskyheight = 528;
int lenasize = lenaheight * lenawidth * 3;
int moonsize = moonheight * moonwidth * 3;
int starsize = starskyheight * starskywidth * 3;
ifstream lenafile("lena_noise.yuv", ios::binary);
ifstream starskyfile("starsky.yuv", ios::binary);
ifstream moonfile("moon.yuv", ios::binary);
if (!lenafile) { cout << "error to open lena!" << endl; }
if (!starskyfile) { cout << "error to open starsky!" << endl; }
if (!moonfile) { cout << "error to open moon!" << endl; }
ofstream lenaNewFile("lena_new.yuv", ios::binary);
ofstream starskyNewFile("starsky_new.yuv", ios::binary);
ofstream moonLaNewFile("moon_Laplace_new.yuv", ios::binary);
ofstream moonLaNewFileFull("moon_Laplace_new_full.yuv", ios::binary);
ofstream moonSoNewFile("moon_Sobel_new.yuv", ios::binary);
ofstream moonSoNewFileFull("moon_Sobel_new_full.yuv", ios::binary);
if (!lenaNewFile) { cout << "error to create lena!" << endl; }
if (!starskyNewFile) { cout << "error to create starsky!" << endl; }
if (!moonLaNewFile) { cout << "error to create moonfile1!" << endl; }
if (!moonLaNewFileFull) { cout << "error to create moonfile1!" << endl; }
if (!moonSoNewFile) { cout << "error to create moonfile2!" << endl; }
if (!moonSoNewFileFull) { cout << "error to create moonfile2!" << endl; }
unsigned char* lena = new unsigned char[lenasize];
unsigned char* starsky = new unsigned char[starsize];
unsigned char* moon = new unsigned char[moonsize];
unsigned char* lenaNew = new unsigned char[lenasize];
unsigned char* starskyNew = new unsigned char[starsize];
unsigned char* moonLaNew = new unsigned char[moonsize];
unsigned char* moonSobelNew = new unsigned char[moonsize];
unsigned char* moonLaNew_full = new unsigned char[moonsize];
unsigned char* moonSobelNew_full = new unsigned char[moonsize];
lenafile.read((char*)lena, lenasize);
starskyfile.read((char*)starsky, starsize);
moonfile.read((char*)moon, moonsize);
SmoothingFilterAve(lena, lenaNew, lenaheight * 3, lenawidth);
SmoothingFilterGausian(starsky, starskyNew, starskyheight * 3, starskywidth);
SharpenFilterLaplace(moon, moonLaNew, moonheight * 3, moonwidth);
SharpenFilterLaplaceFull(moon, moonLaNew_full, moonheight * 3, moonwidth);
SharpenFilterSobel(moon, moonSobelNew, moonheight * 3, moonwidth);
SharpenFilterSobelFull(moon, moonSobelNew_full, moonheight * 3, moonwidth);
lenaNewFile.write((char*)lenaNew, lenasize);
starskyNewFile.write((char*)starskyNew, starsize);
moonLaNewFile.write((char*)moonLaNew, moonsize);
moonLaNewFileFull.write((char*)moonLaNew_full, moonsize);
moonSoNewFile.write((char*)moonSobelNew, moonsize);
moonSoNewFileFull.write((char*)moonSobelNew_full, moonsize);
if (lena) { delete []lena; lena = NULL; }
if (starsky) { delete []starsky; starsky = NULL; }
if (moon) { delete []moon; moon = NULL; }
if (lenaNew) { delete []lenaNew; lenaNew = NULL; }
if (starskyNew) { delete []starskyNew; starskyNew = NULL; }
if (moonLaNew) { delete []moonLaNew; moonLaNew = NULL; }
if (moonSobelNew) { delete []moonSobelNew; moonSobelNew = NULL; }
lenafile.close();
starskyfile.close();
moonfile.close();
lenaNewFile.close();
starskyNewFile.close();
moonLaNewFile.close();
moonLaNewFileFull.close();
moonSoNewFile.close();
moonSoNewFileFull.close();
return 0;
}
filtering.h
#pragma once
void SmoothingFilterAve(unsigned char* lena, unsigned char* lenaNew, int height, int width);
void SmoothingFilterGausian(unsigned char* starsky, unsigned char* starskyNew, int height, int width);
void SharpenFilterLaplace(unsigned char* moon, unsigned char* moonLaNew, int height, int width);
void SharpenFilterLaplaceFull(unsigned char* moon, unsigned char* moonLaNew, int height, int width);
void SharpenFilterSobel(unsigned char* moon, unsigned char* moonSobelNew, int height, int width);
void SharpenFilterSobelFull(unsigned char* moon, unsigned char* moonSobelNew, int height, int width);
int Mask(unsigned char* ini, int i, int j, int height, int width, int* w, int order);
filtering.cpp
#include <iostream>
#include <cstdio>
#include <fstream>
#include "filtering.h"
using namespace std;
int Gausian[25] = { 2,4,5,4,2,4,9,12,9,4,5,12,15,12,5,4,9,12,9,4,2,4,5,4,2 };
int Smooth[25] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };
int Laplace[9] = { 0,1,0,1,-4,1,0,1,0 };
int Sobel[9] = { -1,-2,-1,0,0,0,1,2,1 };
int Laplace_full[9] = { 0,1,0,1,-5,1,0,1,0 };
int Sobel_full[9] = { -1,-2,-1,0,1,0,1,2,1 };
int Mask(unsigned char* ini,int i,int j, int height, int width,int* w,int order)
{
int sum = 0;
int ini_x = i - 2;
int ini_y = j - 2;
for (int m = 0; m < order; m++)
{
for (int n = 0; n < order; n++)
{
if ((ini_x + m < 0) || (ini_y + n < 0) || (ini_x + m + 1 > height) || (ini_y + n + 1 > width))
sum += 0;
else
sum += ini[(ini_x + m) * width + ini_y + n] * w[m * order + n];
}
}
return abs(sum);
}
void SmoothingFilterAve(unsigned char* lena, unsigned char* lenaNew, int height, int width)
{
int i = 0, j = 0;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
lenaNew[i * width + j] = Mask(lena, i, j, height, width, Smooth, 5) / 25;
}
}
}
void SmoothingFilterGausian(unsigned char* starsky, unsigned char* starskyNew, int height, int width)
{
int i = 0, j = 0;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
starskyNew[i * width + j] = Mask(starsky, i, j, height, width, Gausian, 5) / 159;
}
}
}
void SharpenFilterLaplace(unsigned char* moon, unsigned char* moonLaNew, int height, int width)
{
int i = 0, j = 0;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
moonLaNew[i * width + j] = Mask(moon, i, j, height, width, Laplace, 3);
}
}
}
void SharpenFilterLaplaceFull(unsigned char* moon, unsigned char* moonLaNew, int height, int width)
{
int i = 0, j = 0;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
moonLaNew[i * width + j] = Mask(moon, i, j, height, width, Laplace_full, 3);
}
}
}
void SharpenFilterSobel(unsigned char* moon, unsigned char* moonSobelNew, int height, int width)
{
int i = 0, j = 0;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
moonSobelNew[i * width + j] = Mask(moon, i, j, height, width, Sobel, 3);
}
}
}
void SharpenFilterSobelFull(unsigned char* moon, unsigned char* moonSobelNew, int height, int width)
{
int i = 0, j = 0;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
moonSobelNew[i * width + j] = Mask(moon, i, j, height, width, Sobel_full, 3);
}
}
}
(三)实验结果
1 邻域平均模板
实验前 | 实验后 |
---|
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3882900f4c37cb6c88f8c8519050b2f7.png) | ![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a53f3727e797a32ca8a7343287c718de.png) |
2 高斯平均模板
实验前 | 实验后 |
---|
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ef926206ff512361f36619f0a75f1b1c.png) | ![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8edf4d5dffe86af1dace8d03c4e2b797.png) |
3 Laplace锐化滤波
4 Sobel滤波
至此,实验结束。