vs2015+opencv3.3.1 实现 彩色高斯滤波器 包括边缘处理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vs2015+opencv3.3.1 实现 彩色高斯滤波器 包括边缘处理相关的知识,希望对你有一定的参考价值。

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

using namespace  cv;
using namespace  std;
void gaussianFilter2(vector<uchar> corrupted, vector<uchar> &smooth, int width, int height)
{
	int templates[25] = { 1, 4, 7, 4, 1,
		4, 16, 26, 16, 4,
		7, 26, 41, 26, 7,
		4, 16, 26, 16, 4,
		1, 4, 7, 4, 1 };        //滤波器模板  

	smooth = corrupted;  //复制像素  
	for (int j = 2; j<height - 2; j++)  //边缘不处理  
	{
		for (int i = 6; i<width - 7; i++)
		{
			int sum = 0;
			int index = 0;
			for (int m = j - 2; m<j + 3; m++)
			{
				for (int n = i - 6; n<i + 7; n+=3)
				{
					sum += corrupted[m*width + n] * templates[index++];
				}
			}
			sum /= 273;
			if (sum > 255)
				sum = 255;
			smooth[j*width + i] = sum;
		}
	}

	
	
	for (int j = 0; j< 2; j++)  //上边缘处理  
	{
		for (int i = 6; i<width-7 ; i++)
		{
			int sum = 0;
			int index = 0;
			for (int m = j - 2; m<j + 3; m++)
			{
				int mt = m; if (mt < 0)mt = -mt;

				for (int n = i - 6; n<i + 7; n += 3)
				{
					sum += corrupted[mt*width + n] * templates[index++];
				}
			}
			sum /= 273;
			if (sum > 255)
				sum = 255;
			smooth[j*width + i] = sum;
		}
	}
	
	for (int j = height-2; j< height; j++)  //下边缘处理  
	{
		for (int i = 6; i<width - 7; i++)
		{
			int sum = 0;
			int index = 0;
			for (int m = j - 2; m<j + 3; m++)
			{
				int mt = m; if (mt >height-1)mt =2*height-1-mt;

				for (int n = i - 6; n<i + 7; n += 3)
				{
					sum += corrupted[mt*width + n] * templates[index++];
				}
			}
			sum /= 273;
			if (sum > 255)
				sum = 255;
			smooth[j*width + i] = sum;
		}
	}

	
	
	for (int j = 0; j< height; j++)  //左边缘处理  
	{
		for (int i = 0; i<6; i++)
		{
			int sum = 0;
			int index = 0;
			for (int m = j - 2; m<j + 3; m++)
			{
				int mt = m; 
				if (mt >height - 1)mt = 2 * height - 1 - mt;
				if (mt < 0)mt = -mt;

				for (int n = i - 6; n<i + 7; n += 3)
				{
					int nt = n;
					if (nt < 0)nt = -nt;
					sum += corrupted[mt*width + nt] * templates[index++];
				}
			}
			sum /= 273;
			if (sum > 255)
				sum = 255;
			smooth[j*width + i] = sum;
		}
	}

	for (int j = 0; j< height; j++)  //右边缘处理  
	{
		for (int i = width - 7; i<width; i++)
		{
			int sum = 0;
			int index = 0;
			for (int m = j - 2; m<j + 3; m++)
			{
				int mt = m;
				if (mt >height - 1)mt = 2 * (height - 1) - mt;
				if (mt < 0)mt = -mt;

				for (int n = i - 6; n<i + 7; n += 3)
				{
					int nt = n;
					if (nt > width-1)nt = 2*(width-1)-nt;
					sum += corrupted[mt*width + nt] * templates[index++];
				}
			}
			sum /= 273;
			if (sum > 255)
				sum = 255;
			smooth[j*width + i] = sum;
		}
	}


	




 }









int main() {

	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, int(img.cols)*3, img.rows);




	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;

}

  

以上是关于vs2015+opencv3.3.1 实现 彩色高斯滤波器 包括边缘处理的主要内容,如果未能解决你的问题,请参考以下文章

vs2015+opencv3.3.1 实现 直方图均衡化

OpenCV 3:openCV 3.3.1 中的透视变换有啥问题?

HYF-Linux8 环境配置躺坑记录 cuda8 cudnn6 virtualenv tensorflow opencv3.3.1

VS Code Windows 7 中的彩色表情符号

安装VS2015和Xamarin For VS

iOS CABasicAnimation 彩色动画没有效果