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

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




int main() {

	Mat img = imread("123.jpg", 0);
	cout << img.rows*img.cols;
	//	namedWindow("MyWindow");
	//	imshow("MyWindow", img);


	vector<uchar> array(img.rows*img.cols);
	if (img.isContinuous()) { array.assign(img.datastart, img.dataend); }	cout << array.size();

	vector<uchar> no(img.rows*img.cols);

	gaussianFilter2(array, no,img.cols ,img.rows );




	Mat now((int)img.rows, (int)img.cols, 0); 
	for (int i = 0; i < img.rows; i++)
		for (int j = 0; j < img.cols; j++)
			now.at<uchar>(i, j) = no[i*img.cols + j];

	//	namedWindow("MyWindow1");
	//	imshow("MyWindow1", now);
	//	waitKey(0);
	imwrite("1123.jpg", now);
	system("pause");

	return
		0;

}

  

posted @ 2017-11-27 23:30  ff_d  阅读(671)  评论(0编辑  收藏  举报