C++实现矩阵卷积

int kernel_x;
int kernel_y;
int mat_x;
int mat_y;
int **kernel;
int **mat;
int pos_archor_x;
int pos_archor_y;
void getValue(int pos_x,int pos_y)
{
	int temp_x_kernel_start,temp_x_kernel_end;
	int temp_y_kernel_start,temp_y_kernel_end;
	if(pos_x>=pos_archor_x)
		temp_x_kernel_start=0;
	else
		temp_x_kernel_start=pos_archor_x-pos_x;
	if(mat_x-pos_x>=kernel_x-pos_archor_x)
		temp_x_kernel_end=kernel_x-1;
	else
		temp_x_kernel_end=mat_x-1-pos_x+pos_archor_x;
	if(pos_y>=pos_archor_y)
		temp_y_kernel_start=0;
	else
		temp_y_kernel_start=pos_archor_y-pos_y;
	if(mat_y-pos_y>=kernel_y-pos_archor_y)
		temp_y_kernel_end=kernel_y-1;
	else
		temp_y_kernel_end=mat_y-1-pos_y+pos_archor_y;

	cout<<"start"<<"("<<temp_x_kernel_start<<","<<temp_y_kernel_start<<")"<<‘\t‘<<"end"<<"("<<temp_x_kernel_end<<","<<temp_y_kernel_end<<")"<<endl;

	int all=0;

	int x=0;

	for(int i=temp_x_kernel_start;i<=temp_x_kernel_end;++i)
	{
			int y=0;

		for(int j=temp_y_kernel_start;j<=temp_y_kernel_end;++j)
		{
			cout<<"("<<pos_x-(pos_archor_x-temp_x_kernel_start)+x<<‘,‘<<pos_y-(pos_archor_y-temp_y_kernel_start)+y<<")"<<endl;
			all+=kernel[i][j]*mat[pos_x-(pos_archor_x-temp_x_kernel_start)+x][pos_y-(pos_archor_y-temp_y_kernel_start)+y];
			cout<<all<<endl;
			y++;
		}
		x++;
	}
	mat[pos_x][pos_y]=all;

}

int main()
{
	cout<<"input the kernel size"<<endl;
	cout<<"input the kernel‘x lenth"<<endl;
	cin>>kernel_x;
	cout<<"input the kernel‘y lenth"<<endl;
	cin>>kernel_y;
	kernel=new int*[kernel_x];
	for(int i=0;i!=kernel_x;++i)
		kernel[i]=new int[kernel_y];
	cout<<"please input the kernel"<<endl;
	for(int i=0;i!=kernel_x;++i)
		for(int j=0;j!=kernel_y;++j)
		cin>>kernel[i][j];
	cout<<"input the mat size"<<endl;
	cout<<"input the mat‘x lenth"<<endl;
	cin>>mat_x;
	cout<<"input the mat‘y lenth"<<endl;
	cin>>mat_y;
	mat=new int*[mat_x];
	for(int i=0;i!=mat_x;++i)
		mat[i]=new int[mat_y];
	cout<<"please inpute the mat"<<endl;
	for(int i=0;i!=mat_x;++i)
	{
		for(int j=0;j!=mat_y;++j)
			cin>>mat[i][j];

	}
	pos_archor_x=0;
	pos_archor_y=1;
	for(int i=0;i!=mat_x;++i)
		for(int j=0;j!=mat_y;++j)
          getValue(i,j);

}

  

时间: 2024-08-08 09:24:45

C++实现矩阵卷积的相关文章

矩阵卷积Matlab(转载)

转载自:http://blog.csdn.net/anan1205/article/details/12313593 两个矩阵卷积转化为矩阵相乘形式--Matlab应用(这里考虑二维矩阵,在图像中对应)两个图像模糊(边缘)操作,假设矩阵A.B,A代表源图像,B代表卷积模板,那么B的取值决定最后运算的结果. Matlab中的应用函数--conv2(二维卷积,一维对应conv) 函数给出的公式定义为: 同一维数据卷积一样,它的实质在于将卷积模板图像翻转(旋转180),这里等同于一维信号的翻转,然后将

Matlab 矩阵卷积理解(转载)

转载自:http://blog.csdn.net/andrewseu/article/details/51783181 在图像处理的过程中,经常会看到矩阵卷积的概念,比如说用一个模板去和一张图片进行卷积,因此很有必要了解矩阵卷积到了做了什么,具体又是怎么计算的. 在matlab中有conv2函数对矩阵进行卷积运算,其中有一个shape参数,取值具体有三种: -full - (default) returns the full 2-D convolution, -'same' - returns

二维矩阵卷积运算实现

http://z.download.csdn.net/detail/wangfei0117/4408649 http://download.csdn.net/detail/wanwenliang2008/1767686 二维矩阵卷积运算实现,布布扣,bubuko.com

卷积神经网络CNN原理以及TensorFlow实现

在知乎上看到一段介绍卷积神经网络的文章,感觉讲的特别直观明了,我整理了一下.首先介绍原理部分. 通过一个图像分类问题介绍卷积神经网络是如何工作的.下面是卷积神经网络判断一个图片是否包含"儿童"的过程,包括四个步骤:图像输入(InputImage)→卷积(Convolution)→最大池化(MaxPooling)→全连接神经网络(Fully-ConnectedNeural Network)计算. 首先将图片分割成如下图的重叠的独立小块:下图中,这张照片被分割成了77张大小相同的小图片.

卷积神经网络(CNN)模型结构

卷积神经网络(CNN)模型结构 转载:http://www.cnblogs.com/pinard/p/6483207.html 看到的一片不错的文章,先转过来留着,怕以后博主删了.哈哈哈 在前面我们讲述了DNN的模型与前向反向传播算法.而在DNN大类中,卷积神经网络(Convolutional Neural Networks,以下简称CNN)是最为成功的DNN特例之一.CNN广泛的应用于图像识别,当然现在也应用于NLP等其他领域,本文我们就对CNN的模型结构做一个总结. 在学习CNN前,推荐大家

MATLAB卷积运算(conv、conv2、convn)解释

1 conv(向量卷积运算) 所谓两个向量卷积,说白了就是多项式乘法.比如:p=[1 2 3],q=[1 1]是两个向量,p和q的卷积如下:把p的元素作为一个多项式的系数,多项式按升幂(或降幂)排列,比如就按升幂吧,写出对应的多项式:1+2x+3x^2;同样的,把q的元素也作为多项式的系数按升幂排列,写出对应的多项式:1+x. 卷积就是"两个多项式相乘取系数".(1+2x+3x^2)×(1+x)=1+3x+5x^2+3x^3所以p和q卷积的结果就是[1 3 5 3]. 记住,当确定是用

卷积神经网络cnn的实现

卷积神经网络 代码:https://github.com/TimVerion/cat 卷积层 卷积层:通过在原始图像上平移来提取特征,每一个特征就是一个特征映射 原理:基于人脑的图片识别过程,我们可以认为图像的空间联系也是局部的像素联系比较紧密,而较远的像素相关性比较弱,所以每个神经元没有必要对全局图像进行感知,只要对局部进行感知,而在更高层次对局部的信息进行综合操作得出全局信息:即局部感知. 卷积分的知识 过程: 作用: 局部感知:在进行计算的时候,将图片划分为一个个的区域进行计算/考虑: 参

【转】MATLAB conv2函数的理解

另附:http://blog.csdn.net/anan1205/article/details/12313593 原文:http://blog.csdn.net/andrewseu/article/details/51783181 在图像处理的过程中,经常会看到矩阵卷积的概念,比如说用一个模板去和一张图片进行卷积,因此很有必要了解矩阵卷积到了做了什么,具体又是怎么计算的. 在matlab中有conv2函数对矩阵进行卷积运算,其中有一个shape参数,取值具体有三种: -full - (defa

[傅里叶变换及其应用学习笔记] 二十五. 线性系统,传递函数,特征值

矩阵卷积,离散有限维线性时不变系统 与上一节课连续无限维线性时不变系统有相同的描述:当且仅当线性算符是用卷积表达的,该系统才是线性时不变系统(LTI system). $\underline{w} = Av = \underline{h}* \underline{v}$ 上述等式表达了离散有限维的线性时不变系统,它能表达成脉冲响应与输入的矩阵乘积,也能表达成矩阵间的卷积. 下面我们通过一个例子加深对线性时不变系统的理解. 例,假设有LTI系统 $\underline{w} = Av = \und