Reputation: 1
I am trying to create some Gaussian distributions and put them on an image. The Gaussians have randomly created parameter (amplitude, position and standard deviation). First I put the parameters into vectors or matrices, then I am using ngrid() function to get a 2d space to create the Gaussians, however i get an error (since possibly mathematical operations with ngrid values is not trivial...). The error is:
??? 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)));
the code is here:
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
Please tell me how fix this, plot 2d Gaussians with parameter vectors... Thanks in advance
Upvotes: 0
Views: 263
Reputation: 36940
Apart from the craziness about "drawing on an image", which I don't really understand, I think you are trying to add up a bunch of separate gaussian distributions on a grid. Here's what I did with your code. Note that your bivariate gaussians are not normalized properly and you were using the variance and not standard deviation before. I fixed the latter; however, I didn't bother with the normalization because you are multiplying each by an amplitude value anyway.
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);
Random output:
Upvotes: 1