来自:https://blog.csdn.net/yu734390853/article/details/77571897
#include<iostream>
#include<vector>
using namespace std;
int main()
{
//定义被卷积的矩阵(其实是一个数组,数组元素的个数8*8)
int const map = 8;
float A[map*map] =
{
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8
};
//定义卷积核矩阵(其实也是一个数组,数组元素的个数3*3)
int const kernel = 3;
float B[kernel*kernel] =
{
1, 1, 1,
1, 1, 1,
1, 1, 1
};
//计算卷积输出矩阵的维数(其实是输出数组元素个数的开根号)
int const outm = map - kernel + 1; //被卷积矩阵的维数-卷积核的维数+1 即8-3+1=6
//计算卷积过程中的被卷积矩阵的宽和高(就是把宽拉成和卷积核的高一样,这样才好对应相乘)
int const convAw = kernel*kernel;//3*3=9
int const convAh = map*map;//8*8=64
float A_convert[convAh*convAw] = { 0 };//定义一个卷积过程中的矩阵(也就是被拉长过后的矩阵)
for (int i = 0; i < outm; i++)
{
for (int j = 0; j < outm; j++)
{
int wh = i * outm * convAw + j * convAw;
int col1 = i * map + j;
A_convert[wh] = A[col1]; //第一次循环时把A[0] 的值赋给 A_convert[0]
A_convert[wh + 1] = A[col1 + 1];//第一次循环时把A[1] 的值赋给 A_convert[1]
A_convert[wh + 2] = A[col1 + 2];//第一次循环时把A[2] 的值赋给 A_convert[2]
int col2 = (i + 1) * map + j;
A_convert[wh + 3] = A[col2]; //第一次循环时把A[8] 的值赋给 A_convert[3]
A_convert[wh + 4] = A[col2 + 1];//第一次循环时把A[9] 的值赋给 A_convert[4]
A_convert[wh + 5] = A[col2 + 2];//第一次循环时把A[10] 的值赋给 A_convert[5]
int col3 = (i + 2) * map + j;
A_convert[wh + 6] = A[col3]; //第一次循环时把A[16] 的值赋给 A_convert[6]
A_convert[wh + 7] = A[col3 + 1]; //第一次循环时把A[17] 的值赋给 A_convert[7]
A_convert[wh + 8] = A[col3 + 2]; //第一次循环时把A[18] 的值赋给 A_convert[8]
}
}
vector<int> C;
for (int i = 0; i < outm; i++)
{
for (int j = 0; j < outm; j++)
{
int a = 0;
int wh = i * outm * convAw + j * convAw;
for (int m =0; m < convAw; m++)
{
a += A_convert[wh + m] * B[m] ;
}
C.push_back(a); //在C中添加数据a
}
}
//输出被卷积矩阵
cout << "被卷积矩阵 :" << endl;
for (int i = 0; i < map; i++)
{
for (int j = 0; j < map; j++)
{
cout << A[i*map + j] << " ";
}
cout << endl;
}
cout << endl;
//输出卷积核矩阵
cout << "卷积核矩阵:" << endl;
for (int i = 0; i < kernel; i++)
{
for (int j = 0; j < kernel; j++)
{
cout << B[i*kernel + j] << " ";
}
cout << endl;
}
cout << endl;
//输出卷积后输出矩阵
cout << "卷积后输出矩阵:" << endl;
for (int i = 0; i < outm; i++)
{
for (int j = 0; j < outm; j++)
{
cout << C[i*outm + j] << " ";
}
cout << endl;
}
system("pause");
return 0;
}