码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-11-28 01:31:11      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:turn   names   read   stream   sig   clu   smooth   void   imwrite   

#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 实现 彩色高斯滤波器 包括边缘处理

标签:turn   names   read   stream   sig   clu   smooth   void   imwrite   

原文地址:http://www.cnblogs.com/goudanli/p/7906718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!