高斯模糊算法
1.高斯模糊算法效果(左边是原始图像,右边是处理后的图像)
2.算法原理
2.1"模糊"的算法有很多种,其中有一种叫做"高斯模糊"(Gaussian Blur)。它将正态分布(又名"高斯分布")用于图像处理。
2.2“高斯模糊“二维正态分布为权重,以K((2xK+1)*2)为半径,进行卷积,K越大模糊程度越大
2.3距离中心点越远的像素所获得权重越小
2.4权值计算:二维正态分布函数
2.5x,y坐标选取,其中б是方差,可以自己选取,方差越大,模糊效果越好,注意算得权值要进行归一化,因为算得权值之和不一定为1,所以要使得他们的值之和为1
2.6将算得的权值与图像中的每个像素进行卷积,算得新的图像,就是你需要的图像
2.7边缘处理:对边缘像素进行对称处理来代替没有像素的值,这里以3*3(半径K=1)模板为例,首先判断模板的X坐标在图像坐标中的位置,如果不在图像中,将其X坐标关于模板中心的X坐标对称,如果此时该点还在图像外面,再将其Y坐标关于模板中心的Y坐标对称,此时该点一定在图像上,以此点的坐标的像素值来代替模板在图像外面的像素值。
2.8.计算权重的函数
//获取核函数
float getWeight(int k, float sigma) {
int radius = 2 k + 1;
float filter = new float[radiusradius];
float sum = 0;
float e = 2.71828;
int i, j;
for (i = 1; i<radius + 1; i++)
for (j = 1; j<radius + 1; j++)
{
float cifang = -((i - k - 1)(i - k - 1) + (j - k - 1)(j - k - 1)) / (2 sigmasigma);
filter[(i - 1)radius + j - 1] = pow(e, cifang) / (2 3.14159sigmasigma);
sum = sum + filter[(i - 1)radius + j - 1];
}
//将权值归一化
for (i = 0; i<radius; i++)
{
for (int j = 0; j<radius; j++)
{
filter[iradius + j] = filter[i*radius + j] / sum;
}
}
return filter;
}
这里我读图片是用了opencv的函数,在VS中配置OpenCV(https://blog.csdn.net/yuanheng19930119/article/details/87972536)的步骤,见链接
include "highgui.h"
include "time.h"
include "math.h"
include "omp.h"
//获取核函数
float getWeight(int k, float sigma) {
int radius = 2 k + 1;
float filter = new float[radiusradius];
float sum = 0;
float e = 2.71828;
int i, j;
for (i = 1; i<radius + 1; i++)
for (j = 1; j<radius + 1; j++)
{
float cifang = -((i - k - 1)(i - k - 1) + (j - k - 1)(j - k - 1)) / (2 sigmasigma);
filter[(i - 1)radius + j - 1] = pow(e, cifang) / (2 3.14159sigmasigma);
sum = sum + filter[(i - 1)radius + j - 1];
}
//将权值归一化
for (i = 0; i<radius; i++)
{
for (int j = 0; j<radius; j++)
{
filter[iradius + j] = filter[iradius + j] / sum;
}
}
return filter;
}
float mgaussian(int height, int width, int k, float weight, float imagedata, int type) {
float newimage = new float[heightwidthtype];
//omp_get_num_threads();
//omp_set_num_threads(3);
//#pragma omp parallel for
for (int i = 0; i<height; i++)
{
for (int j = 0; j<width; j++) {
//对边缘像素的处理
if(i<k||j<k||i>=height-k||j>=width-k)
{
//for(int m=0;m<type;m++)
//newimage[3widthi+3j+m]=imagedata[3widthi+3j+m];
for(int mtype=0;mtype<type;mtype++)
{
float valuep=0;
for(int nh=0;nh<2k+1;nh++)
for(int nw=0;nw<2k+1;nw++)
{
//纵向是否超出范围
int mx=i+nh-k;
if(mx<0||mx>height-1)
mx=2i-mx;
//判断横向是否超出范围
int my=j+nw-k;
if(my<0||my>width-1)
my=2j-my;
valuep=valuep+weight[nh(2k+1)+nw]imagedata[typemxwidth+typemy+mtype];
}
newimage[typeiwidth+typej+mtype]=valuep;
}
continue;
}
//高斯核函数计算
for (int mtype2 = 0; mtype2<type; mtype2++)
{
float pixelval = 0;
for (int nh2 = 0; nh2<2 * k + 1; nh2++)
for (int nw2 = 0; nw2<2 * k + 1; nw2++)
{
pixelval = pixelval + weight[nh2*(2 * k + 1) + nw2] * imagedata[type*(i + nh2 - k)*width + type*(j + nw2 - k) + mtype2];
}
newimage[type*i*width + type*j + mtype2] = pixelval;
}
}
}
return newimage;
}
int main(int argc, char** argv)
{
IplImage* img = cvLoadImage("C:\\Users\\heng\\Desktop\\test.jpg");
int mwidth, mheight, mtype;
int i, j, k = 1;
float sigma = 10;
printf("请输入高斯核函数的半径:");
scanf("%d",&k);
printf("请输入高斯核函数的方差sigma:");
scanf("%f",&sigma);
//double start = clock();
mwidth = img->width;
mheight = img->height;
mtype = img->nChannels;
//CvMat *imageMat=cvCreateMat(mheight,mheight*mtype,CV_32FC1);
float *imageMat = new float[mwidth*mheight*mtype];
float *Gweight = getWeight(k, sigma);
//去除像素点放到imageMat中
for (i = 0; i<mheight; i++)
for (j = 0; j<mwidth; j++) {
CvScalar s;
s = cvGet2D(img, i, j);// get the (i,j) pixel value
for (int index = 0; index<mtype; index++) {
imageMat[3 * mwidth*i + 3 * j + index] = s.val[index];
}
}
float *imageMat2 = mgaussian(mheight, mwidth, k, Gweight, imageMat, mtype);
IplImage* img2 = cvCreateImage(cvSize(mwidth, mheight), IPL_DEPTH_8U, mtype);
//将处理后的图像值放入图像中显示
for (i = 0; i<mheight; i++)
for (j = 0; j<mwidth; j++) {
CvScalar s;
for (int mmtype = 0; mmtype<mtype; mmtype++) {
int p = int(imageMat2[mtype*mwidth*i + mtype*j + mmtype] + 0.5);
if (p>255)
p = 255;
s.val[mmtype] = p;
}
cvSet2D(img2, i, j, s);
}
cvNamedWindow("显示原图像", CV_WINDOW_AUTOSIZE);
cvShowImage("显示原图像", img);
cvNamedWindow("显示高斯滤波处理后的图像", CV_WINDOW_AUTOSIZE);
cvShowImage("显示高斯滤波处理后的图像", img2);
//double end = clock();
//printf("time=%f", end - start);
cvWaitKey(0);
cvReleaseImage(&img);
cvReleaseImage(&img2);
cvDestroyWindow("显示原图像");
cvDestroyWindow("显示高斯滤波处理后的图像");
delete[] imageMat;
delete[] imageMat2;
}
到此,高斯模糊处理完毕
原文链接:https://blog.csdn.net/yuanheng19930119/article/details/87972786
推荐阅读:
- 分享
- 举报
-
浏览量:2766次2022-02-08 09:00:21
-
浏览量:5509次2021-02-09 14:27:57
-
浏览量:933次2023-08-04 10:55:54
-
浏览量:609次2024-02-27 17:46:24
-
浏览量:5581次2021-06-15 11:49:53
-
浏览量:685次2023-12-14 17:05:19
-
浏览量:5630次2021-01-02 22:50:35
-
浏览量:10571次2020-12-14 13:26:01
-
浏览量:1717次2018-07-03 18:41:20
-
浏览量:5238次2020-12-29 19:37:20
-
浏览量:923次2023-12-11 11:01:31
-
浏览量:30692次2021-07-06 10:18:59
-
浏览量:1496次2023-07-14 14:36:03
-
浏览量:3304次2020-09-28 10:35:46
-
浏览量:2419次2020-12-27 08:54:47
-
浏览量:5790次2020-12-27 09:06:27
-
浏览量:3450次2019-09-18 22:22:32
-
浏览量:2319次2020-08-05 20:49:45
-
浏览量:4305次2022-02-03 09:00:20
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
TT_123456789
感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明