高斯滤波程序编写 opencv C++ CSU
C++ vs2017 opencv
实验1 高斯滤波程序编写 CSU
一、 实验目的
- 掌握OpenCV配置;
- 掌握OpenCV彩色图像和灰度图像的读写操作;
- 读懂阈值分割程序;
- 实现高斯滤波,掌握图像的操作
二、实验内容与要求
编写Gauss滤波程序。
步骤:
(1) 由nwiddowsize 计算gauss滤波器空间
(2) 按照高斯公式计算计算滤波器(具体参考PPT)
(3) 利用上步高斯滤波器进行卷积,实现滤波过程。
三、正文
3.1设计思路
3.2代码及其属性
按钮ID | CADPTION | 对应函数 |
---|---|---|
IDOK | 打开并显示灰度图像 | OnBnClickedOk() |
IDC_read2 | 二值化图像 | OnBnClickedread2() |
IDC_read3 | Guass滤波/opencv的Guass滤波图像 | OnBnClickedread3() |
IDC_read4 | 帮助 | OnBnClickedread4() |
IDCANCEL | 取消 |
3.3主要代码:
3.3.1文件: < zrxCImgPro.h >
/***************************************************************************
类:zrxCImgPro
作用:封装图像操作函数,按钮操作功能也存储在其内 使用了opencv4.1.1
Welcome to my Github and my CSDN blog , more information will be available about the project!
Github:https://github.com/Yiqingde
CSDN Blog:https://me.csdn.net/weixin_42348202
历史:**日期** **理由** **签名**
2019年9月20日 创建 ***
/**************************************************************************/
#pragma once
#include <opencv2/opencv.hpp>
#include <string>
#include <vector>
using namespace cv;
using namespace std;
class zrxCImgPro
{
private:
Mat m_srcimg;//原始灰度图像
bool tag;//鲁棒性
void BinaryImage(Mat Ori, const int binaryT, Mat &binary);//将原始灰度图像转化为二进制图像
void CreateGauss(const int nwiddowsize, const float sigma, Mat &Gauss);//高斯卷积核
void Gaussianfilter(const Mat srcImg, const int nwiddowsize, const float sigma, Mat &dstImg);//高斯滤波函数 需要调用以上函数:高斯滤波卷积核函数
public:
zrxCImgPro();
~zrxCImgPro();
void OpenImg();//实现按钮功能,打开图像
void Button_binaryImg();//实现按钮功能,显示二值图像
void Button_GaussImg();//实现按钮功能,包括显示自写与opencv的gauss图像,并显示运行时间
void Button_Help(); //实现按钮功能,help
};
3.3.2文件: < zrxCImgPro.cpp >
#include "stdafx.h"
#include "zrxCImgPro.h"
zrxCImgPro::zrxCImgPro()
{
this->tag = 0;//起始置0
}
zrxCImgPro::~zrxCImgPro()
{
}
/***************************************************************************
函数:BinaryImage(Mat Ori, const int binaryT, Mat &binary)
作用:将原始灰度图像转化为二进制图像
参数:Mat Ori 原始图像
const int binaryT 阈值
Mat &binary 二进制图像
返回值:无
历史:**日期** **理由** **签名**
2019年9月20日 创建 ***
/**************************************************************************/
void zrxCImgPro::BinaryImage(Mat Ori, const int binaryT, Mat &binary)
{
//影像初始化
binary.create(Ori.rows, Ori.cols, Ori.type());
//循环带走
for (int i = 0; i < Ori.rows; i++)
{
for (int j = 0; j < Ori.cols; j++)
{
int tmp = Ori.at<uchar>(i, j);
(tmp > binaryT) ? binary.at<uchar>(i, j) = 255 : binary.at<uchar>(i, j) = 0;
}
}
}
/***************************************************************************
函数:CreateGauss(const int nwiddowsize, const float sigma, Mat &Gauss)
作用:高斯卷积核
参数:const int nwiddowsize 卷积核窗口宽度或长度
const float sigma 就是sigma
Mat &Gauss 大小为nwiddowsize*nwiddowsize的卷积阵
返回值:无
历史:**日期** **理由** **签名**
2019年9月20日 创建 ***
/**************************************************************************/
void zrxCImgPro::CreateGauss(const int nwiddowsize, const float sigma, Mat &Gauss)
{
Gauss.create(nwiddowsize, nwiddowsize, CV_32FC1);
int center = (nwiddowsize + 1) / 2;
const float PI = 4 * tan(1.0);
float sum = 0;//计算权阵
//给Mat复制
for (int i = 0; i < nwiddowsize; i++)
{
for (int j = 0; j < nwiddowsize; j++)
{
float radius = ((i + 1 - center)*(i + 1 - center) + (j + 1 - center)*(j + 1 - center));
float temp1 = (1 / (2 * PI *sigma*sigma))* exp(-(radius / (2 * sigma*sigma)));
Gauss.at<float>(i, j) = temp1;
sum += Gauss.at<float>(i, j);
}
}
for (int i = 0; i < nwiddowsize; i++)
{
for (int j = 0; j < nwiddowsize; j++)
{
Gauss.at<float>(i, j) = Gauss.at<float>(i, j) / (sum + 1e-6);
}
}
}
/***************************************************************************
函数:Gaussianfilter(const Mat srcImg, const int nwiddowsize, const float sigma, Mat &dstImg)
作用:高斯滤波函数 需要调用以上函数:高斯滤波卷积核函数
参数:const Mat srcImg 原始灰度图像
const int nwiddowsize 卷积核窗口宽度或长度
const float sigma 就是sigma
Mat &dstImg 返回经经卷积后的图像
返回值:无
历史:**日期** **理由** **签名**
2019年9月20日 创建 ***
/**************************************************************************/
void zrxCImgPro::Gaussianfilter(const Mat srcImg, const int nwiddowsize, const float sigma, Mat &dstImg)
{
double t = srcImg.rows;
dstImg.create(srcImg.rows, srcImg.cols, CV_32FC1);
Mat Gauss;
CreateGauss(nwiddowsize, sigma, Gauss);
for (int i = 2; i < srcImg.rows - 3; i++)
{
for (int j = 2; j < srcImg.cols - 3; j++)
{
Mat temp1;
temp1.create(nwiddowsize, nwiddowsize, CV_32FC1);
temp1 = srcImg(Range(i - 2, i + 3), Range(j - 2, j + 3));
Mat temp2;
temp1.convertTo(temp2, CV_32FC1);
dstImg.at<float>(i, j) = temp2.dot(Gauss);
}
}
dstImg.convertTo(dstImg, CV_8UC1);
}
/***************************************************************************
函数:OpenImg()
作用:实现按钮功能,打开图像并显示灰度图像,存储到 m_srcimg中; tag是标签、增加程序鲁棒性
参数:无
返回值:无
历史:**日期** **理由** **签名**
2019年9月20日 创建 ***
/**************************************************************************/
void zrxCImgPro::OpenImg()
{
CFileDialog FileDlg(TRUE, "*.jpg;*.bmp", "*.jpg;*.bmp", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "影像文件()");
if (FileDlg.DoModal() != IDOK)
{
return;
}
CString strImgName = FileDlg.GetPathName();
//打开影像
//IMREAD_GRAYSCALE 以灰度形式打开
m_srcimg = imread(strImgName.GetBuffer(), IMREAD_GRAYSCALE);
resize(m_srcimg, m_srcimg, Size(m_srcimg.cols * 800 / m_srcimg.rows, 800), 0, 0, INTER_CUBIC);
imshow("原始灰度图像", m_srcimg); //显示
//waitKey(); //等待操作,将窗口关闭\回车\ESC等操作会运行以下的程序
tag = 1;
}
/***************************************************************************
函数:Button_binaryImg()
作用:实现按钮功能,显示二值图像
参数:无
返回值:无
历史:**日期** **理由** **签名**
2019年9月20日 创建 ***
/**************************************************************************/
void zrxCImgPro::Button_binaryImg()
{
if (tag == 0)
{
AfxMessageBox(_T("需首先打开影像才能进行操作!"));
return ;
}
//二值化操作
Mat m_binary;
BinaryImage(m_srcimg, 100, m_binary);
imshow("图像二值化结果", m_binary); //显示
waitKey();
}
/***************************************************************************
函数:Button_GaussImg()
作用:实现按钮功能,包括显示自写与opencv的gauss图像,并显示运行时间
参数:无
返回值:无
历史:**日期** **理由** **签名**
2019年9月20日 创建 ***
/**************************************************************************/
void zrxCImgPro::Button_GaussImg()
{
if (tag == 0)
{
AfxMessageBox(_T("需首先打开影像才能进行操作!"));
return;
}
Mat distImg;
clock_t start, finish;
double totaltime;
clock_t start1, finish1;
double totaltime1;
start = clock();
Gaussianfilter(m_srcimg, 5, 1.0, distImg);
//namedWindow("ss", 1);
imshow("高斯滤波结果", distImg); //显示
finish = clock();
totaltime = (double)(finish - start) / CLOCKS_PER_SEC;
waitKey(100); //等待操作,将窗口关闭\回车\ESC等操作会运行以下的程序
//opencv自带高斯滤波
Mat dstImage;
start1 = clock();
GaussianBlur(m_srcimg, dstImage, Size(5, 5), 0, 0);
//显示效果图
imshow("opencv自带高斯滤波效果图", dstImage);
finish1 = clock();
totaltime1 = (double)(finish1 - start1) / CLOCKS_PER_SEC;
waitKey(3000);
CString runtime;
runtime.Format("%s%f%s\r\n%s%f%s",
_T("自写Gauss滤波运行时间:"),
totaltime,
_T("s"),
_T("opencvGauss滤波运行时间:"),
totaltime1,
_T("s")
);
AfxMessageBox(runtime);
}
/***************************************************************************
函数:Button_Help()
作用:实现按钮功能,help
参数:无
返回值:无
历史:**日期** **理由** **签名**
2019年9月20日 创建 ***
/**************************************************************************/
void zrxCImgPro::Button_Help()
{
CString help;
help.Format("%s\r\n%s\r\n%s\r\n\r\n%s\r\n%s\r\n%s\r\n" ,_T("感谢您使用本程序!提示:"), _T("需首先打开影像才能进行操作!"),_T("自写Gauss滤波代码运行较慢,请耐心等待!"),
_T("Welcome to my Github and my CSDN blog , more information will be available about the project!"),
_T("Github : https://github.com/Yiqingde"),
_T("CSDN Blog : https://me.csdn.net/weixin_42348202"));
AfxMessageBox(help);
}
3.3.3文件: < ZRX0107170110Dlg.cpp > (只摘取部分)
/***************************************************************************
类:ZRX0107170110Dlg
作用:按钮实现文件
历史:**日期** **理由** **签名**
2019年9月20日 创建 ***
/**************************************************************************/
……
zrxCImgPro t;//全局变量
/*************************************************
按钮:读取图像函数
*************************************************/
void CZRX0107170110Dlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
//CDialogEx::OnOK();
t.OpenImg();
}
/*************************************************
按钮:进行二值化显示
*************************************************/
void CZRX0107170110Dlg::OnBnClickedread2()
{
// TODO: 在此添加控件通知处理程序代码
t.Button_binaryImg();
}
/*************************************************
按钮:进行自写gauss显示与opencv显示且比较时间
*************************************************/
void CZRX0107170110Dlg::OnBnClickedread3()
{
// TODO: 在此添加控件通知处理程序代码
t.Button_GaussImg();
}
/*************************************************
按钮:help
*************************************************/
void CZRX0107170110Dlg::OnBnClickedread4()
{
// TODO: 在此添加控件通知处理程序代码
t.Button_Help();
}