2D高斯与Matlab?
我想用MATLAB在矩阵中间的二维中画一个高斯。现在我正在做,
for i = 1:size2
for j = 1:size2
gauss(i,j)=A*exp(-1/(sigma^2)*((i-round(size2/2))^2+(j-round(size2/2))^2)); %gaussiana
end
end
但这可能非常慢。
在MATLAB中,我没有找到任何功能,最快的方法是什么? MEX文件是有价值的选择吗?
I want to draw a gaussian in two dimension centered in the middle of the matrix with matlab. Right now I am doing with
for i = 1:size2
for j = 1:size2
gauss(i,j)=A*exp(-1/(sigma^2)*((i-round(size2/2))^2+(j-round(size2/2))^2)); %gaussiana
end
end
but it can be extremely slow.
In matlab I have not found any function, what is the fastest way to do it? Is a mex file a valuable option?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 meshgrid 相对琐碎。
在
实现是:
注意:我用
round(size2/2)
用size2/2
替换,因为圆形不是公式的一部分。将1D高斯转换为2D高斯:
为了使计算更快一些,我们可以创建1D高斯,并从中计算2D高斯:
测试:testing:
output:output:
sum abs diff(g,gauss)= 0
sum abs diff(g1,g2)= 7.1436e-12
更新:
根据CRIS评论“不再需要Meshgrid”。
由于MATLAB R2016B,我们可以使用以下代码:
在名为“ Intrant Singleton扩展”功能上的实现继电器或隐式扩展。
Vectorizing the gaussian computation using meshgrid is relatively trivial.
There is even an Octave implementation in Wikipedia.
The implementation is:
Note: I replaced the
round(size2/2)
withsize2/2
because the rounding is not part of the formula.Trick for converting 1D gaussian into 2D gaussian:
For making the computation a little bit faster we can create 1D gaussian, and compute the 2D gaussian out of it:
Testing:
Output:
sum abs diff (G, gauss) = 0
sum abs diff (G1, G2) = 7.1436e-12
Update:
According to Cris comment "meshgrid is no longer needed".
Since MATLAB r2016b we can use the following code:
The implementation relays on a feature named "implicit singleton expansion" or Implicit Expansion.