数字视频处理(三)——空域模板操作

  1. 对灰度图进行平滑滤波(采用邻域平均模板
    和高斯平均模板)lena_noise.yuv starsky.yuv
  2. 对灰度图进行锐化滤波(分别采用拉普拉斯
    算子和Sobel算子)moon.yuv


(一)所用图片格式说明

在这里插入图片描述

(二)实验代码

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);
}

/*使用的是5*5的平均滤波模板*/
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;
		}
	}
}
/*使用的是5*5的高斯滤波模板*/
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 邻域平均模板

实验前实验后
在这里插入图片描述在这里插入图片描述

2 高斯平均模板

实验前实验后
在这里插入图片描述在这里插入图片描述

3 Laplace锐化滤波

实验前锐化得到的边缘信息锐化后图片
在这里插入图片描述在这里插入图片描述在这里插入图片描述

4 Sobel滤波

实验前锐化得到的边缘信息锐化后图片
在这里插入图片描述在这里插入图片描述在这里插入图片描述

  至此,实验结束。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值