旋转高斯函数 - matlab

时间:2015-05-24 04:25:49

标签: matlab gaussian

美好的一天,

我的代码可以拉伸并旋转高斯2d pdf:

mu = [0 0];

Sigma = [1 0; 0 1];
Scale = [3 0; 0 1];
Theta = 10;
Rotate = [cosd(Theta) -sind(Theta); sind(Theta) cosd(Theta)];
Sigma = (Sigma*Scale)*Rotate

x1 = -100:1:100; x2 = -100:1:100;
[X1,X2] = meshgrid(x1,x2);
F = mvnpdf([X1(:) X2(:)],mu,Sigma);
F = reshape(F,length(x2),length(x1));

imshow(F*255)

不幸的是,当我将Theta更改为0以外的值时,它说 SIGMA必须是方形,对称,正定矩阵。我能知道发生了什么

2 个答案:

答案 0 :(得分:2)

如果您查阅维基百科上关于高斯2D PDF的一般椭圆版本的文章,它看起来并不像您正确地旋转它。一般来说,等式是:

来源:Wikipedia

其中:

通常A = 1我们会在这里采用它。角度theta将逆时针旋转PDF,因此我们可以使用等于mvnpdf的等式的原始形式。使用您的定义和常量,它将成为:

x1 = -100:1:100; x2 = -100:1:100;
[X1,X2] = meshgrid(X1, X2);
sigma1 = 1;
sigma2 = 1;
scale1 = 3;
scale2 = 1;
sigma1 = scale1*sigma1;
sigma2 = scale2*sigma2;
Theta = 10;

a = ((cosd(Theta)^2) / (2*sigma1^2)) + ((sind(Theta)^2) / (2*sigma2^2));
b = -((sind(2*Theta)) / (4*sigma1^2)) + ((sind(2*Theta)) / (4*sigma2^2));
c = ((sind(Theta)^2) / (2*sigma1^2)) + ((cosd(Theta)^2) / (2*sigma2^2));

mu = [0 0];
A = 1;
F = A*exp(-(a*(X1 - mu(1)).^2 + 2*b*(X1 - mu(1)).*(X2 - mu(2)) + c*(X2 - mu(2)).^2));
imshow(F*255);

答案 1 :(得分:1)

我看到你正试图在2D中旋转方差...你必须在它的两侧用你的变换来击中你的sigma矩阵,因为“sigma”矩阵本质上是一个张量。

mu = [0 0];

Sigma = [1 0; 0 1];
Scale = [10 0;0 1];
Theta = pi/3;
m = makehgtform('zrotate',Theta);
m = m(1:2,1:2);
Sigma = m*(Sigma*Scale)*m.';

x1 = -100:1:100; 
x2 = -100:1:100;
[X1,X2] = meshgrid(x1,x2);
F = mvnpdf([X1(:) X2(:)],mu,Sigma);
F = reshape(F,length(x2),length(x1));

imshow(F*255)