1. 程式人生 > >vs2015+opencv3.3.1 實現 灰度高斯濾波器

vs2015+opencv3.3.1 實現 灰度高斯濾波器

3.3 edwin ont eight end wid img pre i++

#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 = 2; i<width - 2; 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++)
				{
					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;

}

  

vs2015+opencv3.3.1 實現 灰度高斯濾波器