图像的高斯平滑

//
#include "stdafx.h"
#include<iostream>
#include<Windows.h>
#include"highgui.h"
#include"cv.h"
#include"math.h" 
using namespace std;
//使用floor函数。floor(x)返回的是小于或等于x的最大整数。
#define pi 3.1415
//#define dem_gauss 3 
//高斯模板的维数
//高斯模板
double** gauss_model(double sigma)
{
int dem_gauss = ceil(6 * sigma + 1);//使用ceil函数。ceil(x)返回的是大于x的最小整数。
int mid = dem_gauss / 2;
double guiyi_total=0;
double** template_su = new double*[dem_gauss];
for (int i = 0; i < dem_gauss; i++)
{
template_su[i] = new double[dem_gauss];
}
for (int m = 0; m < dem_gauss; m++)
{
for (int n = 0; n < dem_gauss; n++)
{
template_su[m][n] = 1 / (2 * pi*sigma*sigma);
template_su[m][n] = template_su[m][n] * exp((-pow((mid - m), 2) - pow((mid - n), 2)) / (2 * pow(sigma, 2)));
guiyi_total = guiyi_total + template_su[m][n];
}
}
cout << guiyi_total<<endl;
//归一化
for (int i = 0; i < dem_gauss; i++)
{
for (int j = 0; j < dem_gauss; j++)
{
template_su[i][j] = template_su[i][j] / guiyi_total;
}
}
for (int k = 0; k < dem_gauss; k++)

for (int v = 0; v < dem_gauss; v++)
{
cout<<'\t' << template_su[k][v] <<",";
}
cout << endl;

}

return template_su;
}
//图像的灰度化
CvMat* grey_img(IplImage* image)
{
IplImage  *GrayImage;
CvMat* pGrayMat = NULL;
GrayImage = cvCreateImage(cvGetSize(image), 8, 1);
pGrayMat = cvCreateMat(image->height, image->width, CV_32FC1);
BYTE data_b;   //BYTE类型的头文件为windows.h,相当于unsigned char
BYTE data_g;
BYTE data_r;
BYTE data_gray;
for (int j = 0; j<image->height; j++)
{
for (int i = 0; i<image->width; i++)
{
data_b = (BYTE)image->imageData[j*image->widthStep + i * 3];     //B分量  
data_g = (BYTE)image->imageData[j*image->widthStep + i * 3 + 1]; //G分量  
data_r = (BYTE)image->imageData[j*image->widthStep + i * 3 + 2]; //R分量
//Gray= 0.072169B+ 0.715160G+ 0.212671R
data_gray = (BYTE)(0.072169*data_b + 0.715160*data_g + 0.212671*data_r);
cvmSet(pGrayMat, j, i, data_gray);
}
}
cvConvert(pGrayMat, GrayImage);
cvNamedWindow("Image_1", CV_WINDOW_AUTOSIZE);
cvShowImage("Image_1", GrayImage);
//cvWaitKey(0);
//cvReleaseImage(&GrayImage);
//cvDestroyWindow("Image_1");
return pGrayMat;
}
//图像高斯化
void gauss_image(CvMat *Gray_array, double sigma_float)
{
IplImage  *GrayImage;
CvMat* pGrayMat = NULL;
GrayImage = cvCreateImage(cvGetSize(Gray_array), 8, 1);
pGrayMat = cvCreateMat(Gray_array->height, Gray_array->width, CV_32FC1);


int dem_gauss = ceil(6 * sigma_float + 1);//使用ceil函数。ceil(x)返回的是大于x的最小整数。
int mid_num = dem_gauss / 2;
double** template_gauss = NULL;
//double** template_gauss = new double*[dem_gauss];
//for (int i = 0; i < dem_gauss; i++)
//{
// template_gauss[i] = new double[dem_gauss];
//}


template_gauss=gauss_model(sigma_float);
cout << template_gauss[0][0] << endl;
int gray_height = Gray_array->height;
int gray_width = Gray_array->width;
//double filter_num = 0.0;
//double filter_sum = 0.0;
for (int i = 0; i < gray_height; i++)
{
for (int j = 0; j < gray_width; j++)
{
double dFilter = 0.0;
double dSum = 0.0;
for (int x = -mid_num; x <= mid_num; x++)
{
for (int y = -mid_num; y <= mid_num; y++)
{
if ((j + x) >= 0 && (j + x)<gray_width && (i + y) >= 0 && (i + y)<gray_height)  //判断边缘  
{
double Imgaedata = cvmGet(Gray_array, i + y, j + x);//访问矩阵元素:
//cout << Imgaedata << endl;
///int y1 = (y + mid_num)*dem_gauss;
//int x1=x + mid_num;
//cout << template_gauss[y1][x1];
dFilter += Imgaedata *template_gauss[(y + mid_num) ][x + mid_num];
dSum += template_gauss[(y + mid_num)][x + mid_num];
}
}
}
cvmSet(pGrayMat, i, j, dFilter / dSum);
}
}
cvConvert(pGrayMat, GrayImage);
cvNamedWindow("Image_2", CV_WINDOW_AUTOSIZE);
cvShowImage("Image_2", GrayImage);
cvWaitKey(0);
cvReleaseImage(&GrayImage);
cvDestroyWindow("Image_2");


}
int _tmain(int argc, _TCHAR* argv[])
{
IplImage* Img_1;
CvMat*  Gray_array;

Img_1 = cvLoadImage("d://psb.jpg");
Gray_array = cvCreateMat(Img_1->height, Img_1->width, CV_32FC1);
if (Img_1 == NULL)
{
cout << "载入图片错误" << endl;
return 0;
}
//图像的灰度化
Gray_array=grey_img(Img_1);
//图像的简单高斯化
double sigma_float=0.9;
gauss_image(Gray_array, sigma_float);


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值