Image analysis: has anybody experience in generating images with controlled background/foreground? Thanks.

2 views (last 30 days)
Dear all,
I need to generate a grey scale image containing bright circles or ellipses (i.e. the cells) laid on a noisy background (e.g. with some bright smaller bodies, or Gaussian with sigma) that has an inhomogeneous lighting. The image histogram should have the two peaks (for the foreground and background) partially overlapped.
The text of the request given to me is not very clear, and it is as follows: "Have a background and foreground (i.e. mean pixel value of objects), maintaining roughly the same difference between them, but that their overall value varies across the image. Let m_fg be the mean of the foreground, then m_fg=m_fg0+kx*x+ky*y, where kx and ky are the components of the gradient and x, y are the coordinates in the figure. The same applies for the background. Later add noise (e.g. Gaussian with sigma) to both the foreground and background".
I do not understand well the method suggested by the requester. So far, until now I could only generate a kind of lighting but it does not look very much a typical cell image background... Furthermore, the objects seem not affected by the 'foreground' generation...
I tried to solve the problem, but I am stuck on it. Has anyone any idea or suggestion? To help you understanding better my problem, I place below the code I generated. Just run it and you will see the result. The piece of code where I have problems starts from line 63.
Thanks in advance.
Cheers
Davide
%%%%%%%%% CODE
%dimensions of the image
rows=400;
cols=600;
%parameters to generate the ellipsoidal bodies
Npt=50;
a_min=5;
a_max=15;
b_min=16;
b_max=40;
angle_min=0;
angle_max=360;
N=1000; %number of points to build the elipse: at least 1000 to guarantee proper filling of bodies
t=linspace(0, 360, N); %to consider all angles from 0 to 360 deg
B=zeros(rows, cols);
%generation of ellipsoidal bodies
j=0;
while j<Npt
j=j+1;
A=zeros(rows, cols);
h=randi([1,rows-100],1,1); %generate h,k (coordinates for ellipse centres)
k=randi([1,cols-100],1,1);
a=randi([a_min,a_max],1,1); %generate a axis
b=randi([b_min,b_max],1,1); %generate b axis
angle=randi([angle_min,angle_max],1,1); %generate an orientation
x=h+a*cosd(t)*cosd(angle)-b*sind(t)*sind(angle); %generate x coordinate for each point composing the ellipse
y=k+a*cosd(t)*sind(angle)+b*sind(t)*cosd(angle); %generate y coordinate for each point of the ellipse
minimum=min(min(x),min(y));
x=round(x+abs(minimum)); % subtract minimum to get rid of negative numbers and round to nearest integer (this is to have the possibility to save the coordinates in the matrix B
y=round(y+abs(minimum));
x(x==0)=1;
y(y==0)=1;
x(x>rows)=rows;
y(y>cols)=cols;
for i=1:N
A(x(i),y(i))=1; %perimeter of ellipse is 1, but inside is 0
end
A=imfill(A); %inside the ellipse it will get 1
if B(A==1)==0 %if the pixels in B, which have coordinates of white points in A, are background, they turn into white
B(A==1)=1;
else
j=j-1; %otherwise counter is set back and another attempting ellipse is built
end
end
%here the 'problematic' code starts
back=find(B==0); %coordinates points belonging to background (stored as linear indices)
fore=find(B==1); %coordinates points belonging to background (stored as linear indices)
%dimension back vector, and extract coordinates x,y in two separated vectors
[bg_x bg_y]=find(B==0);
bg_dimension=size(bg_x);
[fg_x fg_y]=find(B==1);
fg_dimension=size(fg_x);
%mean of background
m_bg=130/255;
%difference background/foreground means (to keep it stable as requested)
diff=0.3;
m_fg=m_bg+diff;
%components x and y of the gradient for the background and foreground
back_gx=-0.008;
back_gy=0.006;
fore_gx=0.005;
fore_gy=0.001;
%noise_bg=0.5*randn(size(bg_x)); %I tried to add noise, but did not work
%noise_fg=0.5*randn(size(fg_x));
for i=1:bg_dimension
B(bg_x(i),bg_y(i))=m_bg+bg_x(i)*back_gx+bg_y(i)*back_gy;
%B(bg_x(i),bg_y(i))=m_bg+bg_x(i)*back_gx+bg_y(i)*back_gy+noise_bg(i);
end
for i=1:fg_dimension
B(fg_x(i),fg_y(i))=m_fg+fg_x(i)*fore_gx+fg_y(i)*fore_gy;
end
B=mat2gray(B); %conversion for histogram
figure, imshow(B, []);
figure, imhist(B);
end
  2 Comments
Jan
Jan on 13 Apr 2013
Please learn how to format code properly in the forum. Blank lines after each line of code are not useful. The details are explained, when you follow the "? Help" link. Thanks.
Davide
Davide on 14 Apr 2013
Dear Jan, thank you for your observation. It was the first time for me to use the forum, and I thought that leaving blank lines would have improved the readability of the code. I have re-formatted the code according to the 'help' guidelines. I hope now it is suitable for the forum. Thanks. Davide

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!