【问题标题】:creatig random gaussians on an image在图像上创建随机高斯
【发布时间】:2013-02-20 11:56:38
【问题描述】:

我正在尝试创建一些高斯分布并将它们放在图像上。高斯随机创建参数(幅度、位置和标准偏差)。首先,我将参数放入向量或矩阵中,然后我使用 ngrid() 函数来获取 2d 空间来创建高斯,但是我得到一个错误(因为可能使用 ngrid 值的数学运算不是微不足道的......)。错误是:

     ??? Error using ==> minus
Integers can only be combined
with integers of the same class,
or scalar doubles.
Error in ==> ss_gauss_fit at 23
  gauss = amp(i)*
  exp(-((x-xc).^2 +
  (y-yc).^2)./(2*std(i)));

代码在这里:

clear all;
image = uint8(zeros([300 300]));
imsize=size(image);

noOfGauss=10;
maxAmpGauss=160;
stdMax=15;
stdMin=3;

for i=1:noOfGauss
    posn(:,:,i)=[  uint8(imsize(1)*rand())   uint8(imsize(2)*rand())  ];
    std(i)=stdMin+uint8((stdMax-stdMin)*rand());
    amp(i)= uint8(rand()* maxAmpGauss);
end

% draw the gaussians on blank image
for i=1:noOfGauss
    [x,y] = ndgrid(1:imsize(1), 1:imsize(2));
    xc = posn(1,1,i);
    yc = posn(1,2,i);
    gauss = amp(i)* exp(-((x-xc).^2 + (y-yc).^2)./(2*std(i)));

    image = image + gauss;
end 

请告诉我如何解决这个问题,用参数向量绘制二维高斯... 提前致谢

【问题讨论】:

    标签: matlab random 2d gaussian


    【解决方案1】:

    除了我不太理解的“在图像上绘图”的疯狂之外,我认为您正在尝试在网格上添加一堆单独的高斯分布。这是我对您的代码所做的。请注意,您的二元高斯未正确归一化,并且您之前使用的是方差而不是标准差。我修复了后者;但是,我没有费心进行归一化,因为无论如何你都是将每个值乘以一个幅度值。

    clear all;
    xmax = 50;
    ymax = 50;
    
    noOfGauss=10;
    maxAmpGauss=160;
    stdMax=10;
    stdMin=3;
    
    posn = zeros(noOfGauss, 2);
    std = zeros(noOfGauss, 1);
    amp = zeros(noOfGauss, 1);
    
    for i=1:noOfGauss
        posn(i,:)=[ xmax*rand()  ymax*rand() ];
        std(i)=stdMin+(stdMax-stdMin)*rand();
        amp(i)= rand()* maxAmpGauss;
    end
    
    % draw the gaussians
    [x,y] = ndgrid(1:xmax, 1:ymax);
    z = zeros(xmax, ymax);
    
    for i=1:noOfGauss    
        xc = posn(i,1);
        yc = posn(i,2);    
        z = z + amp(i)* exp(-((x-xc).^2 + (y-yc).^2)./(2*std(i)^2));
    end 
    
    surf(x, y, z);
    

    随机输出:

    【讨论】:

    • 谢谢,这正是我想要帮助我让一些 python 代码工作的东西。
    猜你喜欢
    • 2012-10-17
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 2010-09-18
    • 1970-01-01
    • 2019-07-03
    相关资源
    最近更新 更多