#include <opencv2\highgui\highgui.hpp>
#include <iostream>
#include<vector>
using namespace cv;
using namespace std; void GetGaussianKernel(double*& gaus_1, const int size, const double sigma_s);
void gaussianFilter2(const vector<uchar>& corrupted, vector<uchar> &smooth, double*& templates,const int width,const int height, const double sigma_r, const int size_m); int main() { const double sigma_r = 30; //值域的sigma
const double PI = 4.0*atan(1.0); //圆周率π赋值
const int size_m = 5; //模板大小
const double sigma_s = 10; //空间域的sigma
double *templates; templates = new double[size_m*size_m];
GetGaussianKernel(templates, size_m, sigma_s);
Mat img = imread("123.jpg", 3); // namedWindow("MyWindow");
// imshow("MyWindow", img); vector<uchar> array(img.rows*img.cols*3);
if (img.isContinuous()) { array.assign(img.datastart, img.dataend); } vector<uchar> no(img.rows*img.cols*3); gaussianFilter2(array, no, templates ,int(img.cols)*3, img.rows,sigma_r,size_m); Mat now((int)img.rows, (int)img.cols, CV_8UC3 ); for (int i = 0; i < img.rows; i++)
for (int j = 0; j < img.cols; j++) {
now.at<Vec3b>(i, j)[0] = no[i*img.cols*3 + j*3];
now.at<Vec3b>(i, j)[1] = no[i*img.cols*3 + j*3 + 1];
now.at<Vec3b>(i, j)[2] =no[i*img.cols*3 + j*3 + 2];
} imwrite("1123.jpg", now);
namedWindow("MyWindow1");
imshow("MyWindow1", now);
waitKey(0);
return 0;
} void GetGaussianKernel(double*& gaus_1, const int size, const double sigma_s)
{
double **gaus = new double*[size];
for (int i = 0; i<size; i++)gaus[i] = new double[size]; double sum = 0;
for (int i = -size / 2; i<size / 2 + 1; i++) {
for (int j = -size / 2; j<size / 2 + 1; j++) {
gaus[i + size / 2][j + size / 2] = exp(-((i*i) + (j*j)) / (2 * sigma_s*sigma_s));
sum += gaus[i + size / 2][j + size / 2];
}
} for (int i = 0; i<size; i++) {
for (int j = 0; j<size; j++) {
gaus[i][j] /= sum;
gaus_1[i*size + j] = gaus[i][j]; //使用一维更简单
}
}
return;
} void gaussianFilter2(const vector<uchar>& corrupted, vector<uchar> &smooth,double*& templates,const int width,const int height,const double sigma_r, const int size_m) {
int len = size_m / 2;
smooth = corrupted; //复制像素 for (int j = 0; j<height ; j++) { //边缘不处理
for (int i = 0; i<width ; i++) {
double sum = 0;
int index = 0;
double sum_c = 0;
double temp = 0;
for (int m = j - len; m<j + len + 1; m++) {
for (int n = i - 3 * len; n<i + 3 * len + 1; n += 3) {
if (m<0 || n<0 || m>height - 1 || n>width - 1)continue;  //边缘处理
temp = templates[index++] * exp(-(corrupted[m*width + n] - corrupted[j*width + i])*(corrupted[m*width + n] - corrupted[j*width + i]) / (2.0*sigma_r*sigma_r));
sum += corrupted[m*width + n] * temp;
sum_c += temp;
}
}
sum /= sum_c;
if (sum > 255)
sum = 255;
if (sum < 0)
sum = 0;
smooth[j*width + i] = sum;
}
}
}

  

vs2015+opencv3.3.1 实现 c++ 双边滤波器(Bilateral Filter)的更多相关文章

  1. 【VS开发】【图像处理】双边滤波器bilateral filter

    目录(?)[-] 简介 原理 代码实现 1 Spatial Weight 2 Similarity Weight 3 Color Filtering 在SSAO中的使用 1. 简介 图像平滑是一个重要 ...

  2. win7下VS2015+opencv3.1.0配置

    由于opencv与vs的适配版本不同,本人在官网下载opencv3.1.0,其可以和VS2013.VS2015适配,文中以VS2015为例 opencv2.4.13-----vc11;vc12 ope ...

  3. [转]VS2015+OpenCV3.3 GPU模块和opencv_contrib模块的编译以及采用CMake编译opencv_contrib时提示“No extra modules found in folder”问题的解决方案

    据官方说法,目前还不是太稳定的算法模块都在opencv_contrib里边,由于不稳定,所以不能在release版本里发行,只有在稳定以后才会放进release里边.但是这里边有很多我们经常要用的算法 ...

  4. Win10 64位+VS2015+Opencv3.3.0安装配置

    Win10 64位+VS2015+Opencv3.3.0安装配置 1.我们首先下载VS2015.OpenCV3.3.0. 1.1 VS2015下载 在官网https://visualstudio.mi ...

  5. 【C++】双边滤波器(bilateral filter)

    Bilateral Filtering for Gray and Color Images 双边滤波器:保留边界的平滑滤波器. 在局部上,就是在灰度值差异不大的区域平滑,在灰度值差异比较大的边界地区保 ...

  6. win10+VS2015+opencv3.4.0配置方法

    win10+VS2015+opencv3.4.0配置方法 操作环境: windows10 64位opencv 3.4.0:https://opencv.org/releases.html(选择open ...

  7. vs2015+opencv3.3.1 实现 c++ 彩色高斯滤波器(Gaussian Smoothing, Gaussian Blur, Gaussian Filter)

    //高斯滤波器 https://github.com/scutlzk#include <opencv2\highgui\highgui.hpp> #include <iostream ...

  8. vs2015+opencv3.3.1 实现 c++ 灰度高斯滤波器

    #include <opencv2\highgui\highgui.hpp> #include <iostream> #include<vector> using ...

  9. win10+vs2015+opencv3.0 x86/x64配置(debug+release)

    最近做一些图像识别的项目,用到了opencv,opencv3.1没有x86版本,所以只能用opencv3.0来完成,下面介绍一下在window10下vs2015 配置opencv3.0的过程(x86和 ...

随机推荐

  1. 文件操作方法大全以及文件打开的其他一些模式sys.stdout.write()就是标准输出到你当前的屏幕 sys.stdout.flush()把内存立即显示到您当前的屏幕

    read()会让你读取的光标变成最后.tell()把你现在文件的句柄的指针打印出来.文件的开头指针位置是0f.read(5)只读取5个字符串个数如果你想把光标移回去,移动到首位f.seek(0)f.d ...

  2. thinkphp中配置信息的二维数组设置与使用

    有时候配置信息是二维数组 1.配置 <?php return array ( // 阿里大鱼短信配置 'dayu_appkey'=>'xxx', 'dayu_secretKey'=> ...

  3. Spring Cloud与分布式系统

    本文不是讲解如何使用spring Cloud的教程,而是探讨Spring Cloud是什么,以及它诞生的背景和意义. 背景 2008年以后,国内互联网行业飞速发展,我们对软件系统的需求已经不再是过去” ...

  4. Py修行路 python基础 (十六)面向对象编程的 继承 多态与多态性 封装

    一.继承顺序: 多继承情况下,有两种方式:深度优先和广度优先 1.py3/py2 新式类的继承:在查找属性时遵循:广度优先 继承顺序是多条分支,按照从左往右的顺序,进行一步一步查找,一个分支走完会走另 ...

  5. spring-boot restful put方式提交表单

    使用spring-boot 做接口,如果按restful的路由形式想使用put方式进行表单提交,第一个参数应该为文件参数,代码如下: @PutMapping("/http-put" ...

  6. Java面向对象-方法的定义及简单使用

    Java面向对象之方法 方法是对象的一部分,也称为行为: 先来一个简单实例: package com.java1234.chap03.sec03; public class Person { void ...

  7. QR 码的位置检测符

    QR码的位置检测符由三个同心正方形叠加而成.分别为: 7*7 modules的黑色正方形: 5*5 modules的白色正方形 : 3*3modules的黑色正方形. 三个用于定位检测的“回”形符号应 ...

  8. js判断页面加载完毕方法

    判断页面加载完成这个方法是很常见的,下面有三个常用的方法,各有利弊. 一.纯js方法 // (1).页面所有内容加载完成执行 window.onload = function(){ } // (2). ...

  9. libevent源码深度剖析十二

    libevent源码深度剖析十二 ——让libevent支持多线程 张亮 Libevent本身不是多线程安全的,在多核的时代,如何能充分利用CPU的能力呢,这一节来说说如何在多线程环境中使用libev ...

  10. 对输入字符进行HTML转义 OR  去HTML标签

    /** * 对输入字符进行HTML转义 * @param mixed $data */ public static function escape($data) { if(is_array($data ...