2D高斯与Matlab?

发布于 2025-01-21 22:07:50 字数 273 浏览 0 评论 0原文

我想用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信仰 2025-01-28 22:07:50

使用 meshgrid 相对琐碎。

实现是:

[X, Y] = meshgrid(1:size2, 1:size2);
G = A*exp(-1/(sigma^2)*((Y-size2/2).^2 + (X-size2/2).^2));

注意:我用round(size2/2)size2/2替换,因为圆形不是公式的一部分。


将1D高斯转换为2D高斯

为了使计算更快一些,我们可以创建1D高斯,并从中计算2D高斯:

x = 1:size2;
G1 = sqrt(A)*exp(-1/(sigma^2)*(x-size2/2).^2);  % Create 1D gaussian 
G2 = G1'*G1;  % Compute the 2D gaussian out of 1D gaussian. 

测试:testing:

size2 = 101;
A = 10;
sigma = 50;
gauss = zeros(size2);

for y = 1:size2
    for x = 1:size2
        gauss(y,x)=A*exp(-1/(sigma^2)*((y-size2/2)^2+(x-size2/2)^2)); %gaussiana
    end
end

[X, Y] = meshgrid(1:size2, 1:size2);
G = A*exp(-1/(sigma^2)*((Y-size2/2).^2 + (X-size2/2).^2));

disp(['sum abs diff (G, gauss) = ' num2str(sum(abs(G(:) - gauss(:))))])

x = 1:size2;
G1 = sqrt(A)*exp(-1/(sigma^2)*(x-size2/2).^2);

G2 = G1'*G1;

disp(['sum abs diff (G1, G2) = ' num2str(sum(abs(G2(:) - G(:))))])

output:output:
sum abs diff(g,gauss)= 0
sum abs diff(g1,g2)= 7.1436e-12


更新:

根据CRIS评论“不再需要Meshgrid”。
由于MATLAB R2016B,我们可以使用以下代码:

X = 1:size2;
Y = X';
G = A*exp(-1/(sigma^2)*((Y-size2/2).^2 + (X-size2/2).^2));

在名为“ Intrant Singleton扩展”功能上的实现继电器或隐式扩展

Vectorizing the gaussian computation using meshgrid is relatively trivial.

There is even an Octave implementation in Wikipedia.

The implementation is:

[X, Y] = meshgrid(1:size2, 1:size2);
G = A*exp(-1/(sigma^2)*((Y-size2/2).^2 + (X-size2/2).^2));

Note: I replaced the round(size2/2) with size2/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:

x = 1:size2;
G1 = sqrt(A)*exp(-1/(sigma^2)*(x-size2/2).^2);  % Create 1D gaussian 
G2 = G1'*G1;  % Compute the 2D gaussian out of 1D gaussian. 

Testing:

size2 = 101;
A = 10;
sigma = 50;
gauss = zeros(size2);

for y = 1:size2
    for x = 1:size2
        gauss(y,x)=A*exp(-1/(sigma^2)*((y-size2/2)^2+(x-size2/2)^2)); %gaussiana
    end
end

[X, Y] = meshgrid(1:size2, 1:size2);
G = A*exp(-1/(sigma^2)*((Y-size2/2).^2 + (X-size2/2).^2));

disp(['sum abs diff (G, gauss) = ' num2str(sum(abs(G(:) - gauss(:))))])

x = 1:size2;
G1 = sqrt(A)*exp(-1/(sigma^2)*(x-size2/2).^2);

G2 = G1'*G1;

disp(['sum abs diff (G1, G2) = ' num2str(sum(abs(G2(:) - G(:))))])

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:

X = 1:size2;
Y = X';
G = A*exp(-1/(sigma^2)*((Y-size2/2).^2 + (X-size2/2).^2));

The implementation relays on a feature named "implicit singleton expansion" or Implicit Expansion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文