#include<iostream>
#include<opencv2\opencv.hpp>
#include"generateGs.h";
#include"hist.h";
using namespace std;
using namespace cv;
//extern void generateGaussianTemplate(double window[3][3], int ksize, double sigma);
const int model_size = 1;//模板大小
const int model_element_number = (model_size * 2 + 1)*(model_size * 2 + 1);//模板元素的个数
const int model_centre_loction = (model_element_number + 1) / 2;//模板中心元素的位置
double model_element[model_element_number] = { 0.1111,0.1111,0.1111,0.111,0.1111,0.111,0.111,0.111,0.1111 };//模板的元素,数组有多大就写多少元素。
double model_gs[][3] = { {0.1,0.3,0.1},{ 0.05,0.2,0.05 },{0.05,0.1,0.05}};//手工设计的高斯模板
//double model_lpls[][3] = { { 0,-1,0 },{ -1,4,-1 },{ 0,-1,0 } };//常用的拉普拉斯卷积核
//double model_lpls[][3] = { { 1,1,1 },{ 1,-8,1 },{ 1,1,1 } };//常用的拉普拉斯卷积核
double model_lpls[][3] = { { -1,-1,-1 },{ -1,8,-1 },{ -1,-1,-1 } };//常用的拉普拉斯卷积核
double sigma = 0.6;//高斯模板生成时方差的赋值
int main(int argc, char **argv) {
Mat srcImage = imread("C://Users//Administrator//Desktop//2.bmp");//读入图像
int cols = srcImage.cols, rows = srcImage.rows;//获取图像大小
Mat grayImage = Mat::zeros(rows, cols, CV_8UC1);
Mat dstImage = Mat::zeros(rows, cols, CV_8UC1);//设置相同大小的灰度图
Mat gsImage = Mat::zeros(rows, cols, CV_8UC1);//设置相同大小的灰度图
Mat histImage = Mat::zeros(rows, cols, CV_8UC1);//设置相同大小的灰度图
Mat lplsImage = Mat::zeros(rows, cols, CV_8UC1);//设置相同大小的灰度图
cvtColor(srcImage, grayImage, CV_BGR2GRAY);//转化为灰度图
imshow("灰度图像", grayImage);
//下面是遍历图像元素
double sum = 0;//均值模板与对应像素的卷积和
double sumGs = 0;//高斯模板与对应像素的卷积和
int arry_i = 0;//均值滤波数组下标
int arry_gi = 0;//高斯滤波数组下标
int arry_gj = 0;//高斯滤波数组下标
int arry_li = 0;//拉普拉斯滤波数组下标
int arry_lj = 0;//拉普拉斯滤波数组下标
for (int i = 0 + model_size; i < rows - model_size; i++) {
for (int j = 0 + model_size; j < cols - model_size; j++) {
//以下是基于循环的均值滤波
sum = 0;
arry_i = 0;//数组下标清0
//遍历模板元素并与对应像素卷积
for (int k = i - model_size; k <= i + model_size; k++) {
for (int m = j - model_size; m <= j + model_size; m++) {
sum += (double)grayImage.at<uchar>(k, m)*model_element[arry_i];//模板元素与对应像素相乘,然后求和;
arry_i += 1;
}
}
dstImage.at<uchar>(i, j) = (uchar)sum;
}
}
//生成高斯模板,这个模板生成的不太好,自己设计的更理想,就用上边自己定义的观察
//generateGs(model_gs,model_element_number,sigma);
//高斯滤波
for (int i = 0 + model_size; i < rows - model_size; i++) {
for (int j = 0 + model_size; j < cols - model_size; j++) {
//以下是基于循环的高斯滤波
sum = 0;
arry_gi = 0;//数组下标清0
arry_gj = 0;//数组下标清0
//遍历模板元素并与对应像素卷积
for (int k = i - model_size; k < i + model_size; k++) {
for (int m = j - model_size; m < j + model_size; m++) {
sum += (double)grayImage.at<uchar>(k, m)*model_gs[arry_gi][arry_gj];//模板元素与对应像素相乘,然后求和;
arry_gj += 1;
}
arry_gj = 0;
arry_gi += 1;
}
gsImage.at<uchar>(i, j) = (uchar)sum;
}
}
//直方图均衡化
histImage = equalize_hist(grayImage);
//下边注释用于检验错误,要确保模板中数相加=1
/*double a = 0;
for (int ii = 0; ii < 3; ii++) {
for (int jj = 0; jj < 3; jj++) {
a += model_gs[ii][jj];
}
}
cout << a << endl*/;
//拉普拉斯滤波器进行图像锐化
for (int i = 0 + model_size; i < rows - model_size; i++) {
for (int j = 0 + model_size; j < cols - model_size; j++) {
//以下是基于循环的高斯滤波
sum = 0;
arry_li = 0;//数组下标清0
arry_lj = 0;//数组下标清0
//遍历模板元素并与对应像素卷积
for (int k = i - model_size; k < i + model_size; k++) {
for (int m = j - model_size; m < j + model_size; m++) {
sum += (double)grayImage.at<uchar>(k, m)*model_lpls[arry_li][arry_lj];//模板元素与对应像素相乘,然后求和;
arry_lj += 1;
}
arry_lj = 0;
arry_li += 1;
}
lplsImage.at<uchar>(i, j) = (uchar)sum;
}
}
namedWindow("均值滤波后图像");
imshow("均值滤波后图像", dstImage);
imshow("高斯滤波后图像", gsImage);
imshow("直方图均衡化后图像", histImage);
imshow("拉普拉斯滤波后图像", lplsImage);
waitKey(0);
return 0;
}
a19920110
- 粉丝: 6
- 资源: 15
最新资源
- 散装物料卸船机step全套技术开发资料100%好用.zip
- MSS市场专项考试题库
- (174756810)跨年烟花代码python
- (175424836)JSP企业电子投票系统(源代码+论文+开题报告+外文翻译+文献综述).rar
- (175470002)JSP企业电子投票系统(源代码+论文+开题报告+外文翻译+文献综述)
- (175759628)贪吃蛇.zip
- (175833246)JSP企业电子投票系统(源代码+论文+开题报告+外文翻译+文献综述).rar.tar.gz
- 自行车、汽车、猫、狗、人类、入侵者检测39-YOLO(v5至v11)、COCO数据集合集.rar
- (175860660)基于51单片机直流电压电流表设计LCD1602液晶实训仿真
- (175931624)基于jsp的投票管理系统源码数据库论文.doc
- 在ARM9核心板KNM1001上实现uIP FTP及TFTP客户端
- (176056440)zotero 插件分享 茉莉花压缩包
- Overview of the Scalable Video Coding Extension of the H.264/AVC Standard
- 汽车之家计量学分析.zip
- (176074624)EPLAN P8部件库:包含低压电气控制系统设计常用品Pai型号 导入单个文件很小几十M,简单易用
- (176333852)《数据库原理及应用教程(微课版)》关系数据库思维导图源文件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈