C语言实现卷积算法

该算法的kernal=3*3

#include

int main(int, void*)
{
	int array[18][18] = { 0 };
	int kernal[3][3] = { {2,-1,2},{-1,1,-1},{2,1,2} };
	int out[16][16] = { 0 };
	int temp;

	for (int row = 1; row <= 16; row++)
	{
		for (int col = 1; col <= 16; col++)
		{
			array[row][col] = rand() % 256;
		}
	}
	
	for (int row = 0; row < 18; row++)
	{
		for (int col = 0; col < 18; col++)
		{
			printf("%d ", array[row][col]);
		}
		printf("\n");
	}
	printf("\n\n\n");

	
	for (int a = 0; a < 3; a++)
	{
		for (int b = 0; b < 3; b++)
		{
			printf("%d ", kernal[a][b]);
		}
		printf("\n");
	}

	int row = 0;
	int col = 0;
	for (int ax = 1; ax <= 16; ax++)
	{
		for (int ay = 1; ay <= 16; ay++)
		{
			temp = array[ax - 1][ay - 1] * kernal[0][0] + array[ax - 1][ay] * kernal[0][1] + array[ax - 1][ay + 1] * kernal[0][2] +
				array[ax][ay - 1] * kernal[1][0] + array[ax][ay] * kernal[1][1] + array[ax][ay + 1] * kernal[1][2] +
				array[ax + 1][ay - 1] * kernal[2][0] + array[ax + 1][ay] * kernal[2][1] + array[ax + 1][ay + 1] * kernal[2][2];
			out[row][col] = temp;
			col++;
			if (col == 16)
			{
				col = 0;
				row++;
				if (row == 16)
				{
					row = 0;
				}
			}
		}
	}

	printf("\n\n\n");

	for (int row = 0; row < 16; row++)
	{
		for (int col = 0; col < 16; col++)
		{
			printf("%d ", out[row][col]);
		}
		printf("\n");
	}

	while (1);
}

 

你可能感兴趣的:(算法)