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

二维卷积c代码

时间:2015-02-10 15:24:34      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

二维信号的卷积原理请参考另外一篇文章:http://blog.csdn.net/carson2005/article/details/43702241

这里直接给出参考代码:


void Conv2(int** filter, int** arr, int** res, int filterW, int filterH, int arrW, int arrH)
{
	int temp;

	for (int i=0; i<filterH+arrH-1; i++)
	{
		for (int j=0; j<filterW+arrW-1; j++)
		{
			temp = 0;
			for (int m=0; m<filterH; m++)
			{
				for (int n=0; n<filterW; n++)
				{
					if ((i-m)>=0 && (i-m)<arrH && (j-n)>=0 && (j-n)<arrW)
					{
						temp += filter[m][n]*arr[i-m][j-n];
					}
				}
			}
			res[i][j] = temp;
		}
	}
}

void Conv2Test()
{
	int filterW = 3;
	int filterH = 3;
	int arrW = 5;
	int arrH = 5;
	int resW = filterW + arrW - 1;
	int resH = filterH + arrH - 1;

	int** pFilter = new int*[filterH];
	for (int i=0; i<filterH; i++)
	{
		pFilter[i] = new int[filterW];
	}

	int** arr = new int*[arrH];
	for (int i=0; i<arrH; i++)
	{
		arr[i] = new int[arrW];
	}

	int** res = new int*[resH];
	for (int i=0; i<resH; i++)
	{
		res[i] = new int[resW];
	}

	pFilter[0][0] = 1;
	pFilter[0][1] = 3;
	pFilter[0][2] = 1;
	pFilter[1][0] = 0;
	pFilter[1][1] = 5;
	pFilter[1][2] = 0;
	pFilter[2][0] = 2;
	pFilter[2][1] = 1;
	pFilter[2][2] = 2;

	arr[0][0] = 17;
	arr[0][1] = 24;
	arr[0][2] = 1;
	arr[0][3] = 8;
	arr[0][4] = 15;
	arr[1][0] = 23;
	arr[1][1] = 5;
	arr[1][2] = 7;
	arr[1][3] = 14;
	arr[1][4] = 16;
	arr[2][0] = 4;
	arr[2][1] = 6;
	arr[2][2] = 13;
	arr[2][3] = 20;
	arr[2][4] = 22;
	arr[3][0] = 10;
	arr[3][1] = 12;
	arr[3][2] = 19;
	arr[3][3] = 21;
	arr[3][4] = 3;
	arr[4][0] = 11;
	arr[4][1] = 18;
	arr[4][2] = 25;
	arr[4][3] = 2;
	arr[4][4] = 9;

	printf("pFilter: \n");
	for (int i=0; i<filterH; i++)
	{
		for (int j=0; j<filterW; j++)
		{
			printf("%d ", pFilter[i][j]);
		}
		printf("\n");
	}
	printf("*********************************************** \n");

	printf("arr: \n");
	for (int i=0; i<arrH; i++)
	{
		for (int j=0; j<arrW; j++)
		{
			printf("%d ", arr[i][j]);
		}
		printf("\n");
	}
	printf("*********************************************** \n");


	Conv2(pFilter, arr, res, filterW, filterH, arrW, arrH);

	printf("res: \n");
	for (int i=0; i<resH; i++)
	{
		for (int j=0; j<resW; j++)
		{
			printf("%d ", res[i][j]);
		}
		printf("\n");
	}
	printf("*********************************************** \n");
}


二维卷积c代码

标签:

原文地址:http://blog.csdn.net/carson2005/article/details/43702895

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