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);
	

}

  

posted @ 2015-06-18 11:43  你猜你猜啊  阅读(1442)  评论(1编辑  收藏  举报