求matlab conv2图像卷积 same的函数代码

matlab(40)
matlab中conv2 函数在计算二维卷积应用实例如下:
格式:C=conv2(A,B)
&&&&&&C=conv2(Hcol,Hrow,A)
&&&&&&C=conv2(...,'shape')
说明:对于 C=conv2(A,B),conv2 计算矩阵A和B的卷积,若[Ma,Na]=size(A),[Mb,Nb]=size(B),
则 size(C)=[Ma+Mb-1,Na+Nb-1];
& & & 对于C=conv2(Hcol,Hrow,A),矩阵A分别与Hcol向量在列方向和Hrow向量在行方向上进行卷积;
& & & 对于C=conv2(...,'shape') 用来指定 conv2返回二维卷积结果部分,参数shape可取值如下:
&&&&&&&&1、full 为缺省值,返回二维卷积的全部结果;
&&&&&&&&2、same 返回二维卷积结果中与 A 大小相同的中间部分;
&&&&&&& 3、valid 返回在卷积过程中,未使用边缘补0部分进行计算的卷积结果部分,当 size(A)&size(B) 时,
& &size(C)=[Ma-Mb+1,Na-Nb+1]
对于C=conv2(...,'shape')可以用下面的图表示:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:47671次
积分:1110
积分:1110
排名:千里之外
原创:48篇
转载:104篇
评论:14条
(1)(5)(2)(7)(1)(4)(8)(16)(13)(4)(13)(12)(9)(43)(15)MATLAB中conv2函数具体是如何计算的?
conv22-D convolutionSyntaxC = conv2(A,B)C = conv2(hcol,hrow,A)C = conv2(...,'shape')DescriptionC = conv2(A,B) computes the two-dimensional convolution of matrices A and B. If one of these matrices describes a two-dimensional finite impulse response (FIR) filter, the other matrix is filtered in two dimensions.The size of C in each dimension is equal to the sum of the corresponding dimensions of the input matrices, minus one. That is, if the size of A is [ma,na] and the size of B is [mb,nb], then the size of C is [ma+mb-1,na+nb-1].The indices of the center element of B are defined as floor(([mb nb]+1)/2).C = conv2(hcol,hrow,A) convolves A first with the vector hcol along the rows and then with the vector hrow along the columns. If hcol is a column vector and hrow is a row vector, this case is the same as C = conv2(hcol*hrow,A).C = conv2(...,'shape') returns a subsection of the two-dimensional convolution, as specified by the shape parameter:full\x09Returns the full two-dimensional convolution (default).same\x09Returns the central part of the convolution of the same size as A.valid\x09Returns only those parts of the convolution that are computed without the zero-padded edges. Using this option, C has size [ma-mb+1,na-nb+1] when all(size(A) >= size(B)). Otherwise conv2 returns [].Algorithmsconv2 uses a straightforward formal implementation of the two-dimensional convolution equation in spatial form. If and are functions of two discrete variables, and , then the formula for the two-dimensional convolution of and isIn practice however, conv2 computes the convolution for finite intervals.Note that matrix indices in MATLAB software always start at 1 rather than 0. Therefore, matrix elements A(1,1), B(1,1), and C(1,1) correspond to mathematical quantities a (0,0), b (0,0), and c (0,0).ExamplesExample 1For the 'same' case, conv2 returns the central part of the convolution. If there are an odd number of rows or columns, the "center" leaves one more at the beginning than the end.This example first computes the convolution of A using the default ('full') shape, then computes the convolution using the 'same' shape. Note that the array returned using 'same' corresponds to the underlined elements of the array returned using the default shape.A = rand(3); B = rand(4); C = conv2(A,B)
% C is 6-by-6C =
1.3Cs = conv2(A,B,'same')
% Cs is the same size as A: 3-by-3Cs =
1.6364Example 2In image processing, the Sobel edge finding operation is a two-dimensional convolution of an input array with the special matrixs = [1 2 1; 0 0 0; -1 -2 -1];These commands extract the horizontal edges from a raised pedestal.A = zeros(10);A(3:7,3:7) = ones(5);H = conv2(A,s);mesh(H)Transposing the filter s extracts the vertical edges of A.V = conv2(A,s');figure, mesh(V)This figure combines both horizontal and vertical edges.figuremesh(sqrt(H.^2 + V.^2))
为您推荐:
其他类似问题
扫描下载二维码Matlab&几种卷积的实现与比较(conv与filter,conv2与filter2)
最近在做控制算法实现的时候,对于其中参杂的各种差分、卷积很头疼,就在网上搜集了些资料,汇总于此,以做备忘。
在MATLAB中,可以用函数y=filter(p,d,x)实现差分方程的仿真,也可以用函数&y=conv(x,h)计算卷积。
(1)即y=filter(p,d,x)用来实现差分方程,d表示差分方程输出y的系数,p表示输入x的系数,而x表示输入序列。输出结果长度数等于x的长度。
实现差分方程,先从简单的说起:
filter([1,2],1,[1,2,3,4,5]),实现y[k]=x[k]+2*x[k-1]
y[1]=x[1]+2*0=1&&&
(x[1]之前状态都用0)
y[2]=x[2]+2*x[1]=2+2*1=4
(2)是用来实现卷级的,对x序列和h序列进行卷积,输出的结果个数等于x的长度与h的长度之和减去1。
卷积公式:z(n)=x(n)*y(n)=
∫x(m)y(n-m)dm.
程序一:以下两个程序的结果一样
(1)h = [3 2 1 -2 1 0 -4 0 3];&%
impulse response
x = [1 -2 3 -4 3 2 1];&%
input sequence
y = conv(h,x);
subplot(2,1,1);
stem(n,y);
&&&&&&&&&xlabel('Time
index n'); ylabel('Amplitude');
title('Output Obtained by Convolution');
(2)x1 = [x zeros(1,8)];
y1 = filter(h,1,x1);
subplot(2,1,2);
stem(n,y1);
xlabel('Time index n'); ylabel('Amplitude');
&&&&&&&&&title('Output
Generated by Filtering');
程序二:filter和conv的不同
&&&&&&&&&&&&&&
x=[1,2,3,4,5];
&&&&&&&&&&&&&&&h=[1,1,1];
&&&&&&&&&&&&&&&y1=conv(h,x)
&&&&&&&&&&&&&&
y2=filter(h,1,x)
&&&&&&&&&&&&&&
y3=filter(x,1,h)
可见:filter函数y(n)是从n=1开始,认为所有n&1都为0;而conv是从卷积公式计算,包括n&1部分。
&&&&&&&&&&&&&&&
因此filter 和conv&的结果长短不同
程序三:滤波后信号幅度的变化
&&&&&&&&&&&&&&&
num=100; %总共1000个数&
&&&&&&&&&&&&&&&
x=rand(1,num); %生成0~1随机数序列&
&&&&&&&&&&&&&&&&x(x&0.5)=1;&
&&&&&&&&&&&&&&&
x(x&=0.5)=-1;
&&&&&&&&&&&&&&&&h1=[0.2,0.5,1,0.5,0.2];&
&&&&&&&&&&&&&&&
h2=[0,0,1,0,0];
&&&&&&&&&&&&&&&
y1=filter(h1,1,x);
&&&&&&&&&&&&&&&
y2=filter(h2,1,x);
&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&
subplot(2,1,1);
&&&&&&&&&&&&&&&
stem(n,y1);
&&&&&&&&&&&&&&&
subplot(2,1,2);&
&&&&&&&&&&&&&&&
stem(n,y2);
MATLAB中提供了卷积运算的函数命令conv2,其语法格式为:&
C = conv2(A,B)&
conv2(A,B)返回矩阵A和B的二维卷积C。若A为ma&na的矩阵,B为mb&nb的矩阵,则C的大小为(ma+mb-1)&(na+nb-1)。&
A=magic(5)&
17 24 1 8 15&
23 5 7 14 16&
4 6 13 20 22&
10 12 19 21 3&
11 18 25 2 9&
&& B=[1 2 1 ;0 2 0;3 1 3]&
&& C=conv2(A,B)&
17 58 66 34 32 38 15&
23 85 88 35 67 76 16&
55 149 117 163 159 135 67&
79 78 160 161 187 129 51&
23 82 153 199 205 108 75&
30 68 135 168 91 84 9&
33 65 126 85 104 15 27&
MATLAB图像处理工具箱提供了基于卷积的图象滤波函数filter2,filter2的语法格式为:&
Y = filter2(h,X)&
filter2(h,X)返回图像X经算子h滤波后的结果,默认返回图像Y与输入图像X大小相同。例如:&
其实filter2和conv2是等价的。MATLAB在计算filter2时先将卷积核旋转180度,再调用conv2函数进行计算。&
Fspecial函数用于创建预定义的滤波算子,其语法格式为:&
h = fspecial(type)&
h = fspecial(type,parameters)&
参数type制定算子类型,parameters指定相应的参数,具体格式为:&
type='average',为均值滤波,参数为n,代表模版尺寸,用向量表示,默认值为[3,3]。&
'gaussian',为高斯低通滤波器,参数有两个,n表示模版尺寸,默认值为[3,3],sigma表示滤波器的标准差,单位为像素,默认值为0.5
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。求matlab中的conv2卷积函数用c++怎么实现 - VC/MFC当前位置:& &&&求matlab中的conv2卷积函数用c++怎么实现求matlab中的conv2卷积函数用c++怎么实现&&网友分享于:&&浏览:317次求matlab中的conv2卷积函数用c++如何实现matlab中C = conv2(H1, H2, A, 'same');这条语句用c++如何实现呢?求大侠们帮帮忙!!!------解决方案--------------------
http://download.csdn.net/source/731440
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有matlab图像处理函数大全_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
matlab图像处理函数大全
上传于||暂无简介
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩15页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢}

我要回帖

更多关于 matlab conv2 same 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信