You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.2 KiB
Matlab

function [I,theta_rho]=image_dot_lines(sz,backgnd,pxRemove,numRange)
I=zeros(sz,sz,'single');
if nargin<3 || isempty(pxRemove)
pxRemove=2/3;
end
if nargin<4 || isempty(numRange)
numRange=[4,8];
end
num_lines=randi(numRange);
total_dots_orig=0;
%add lines
for k=1:num_lines
bar=zeros(sz,sz);
bar(randi([ceil(sz./9),sz-ceil(sz./9)]),10:sz-10)=1;
theta_rho(1,k)=-90+179*rand;
angle=90-theta_rho(1,k);
barI=imrotate(bar,angle,'nearest','crop');
total_dots_orig=total_dots_orig+sum(barI(:));
theta_rho(2,k)=find_rho(barI,theta_rho(1,k));
I=max(I,barI);
end
%binarize image
I(I>0.5)=1;
I(I<=0.5)=0;
%remove random pixels from lines
R=rand(sz,sz);
R(R>=pxRemove)=1;
R(R<pxRemove)=0;
I=I.*R;
dots_lines=sum(I(:));
%add random background pixels
B=rand(sz,sz);
B(B>1-backgnd)=1;
B(B<=1-backgnd)=0;
I=max(I,B);
dots_backgnd=sum(I(:))-dots_lines;
disp(['produced image of ',int2str(num_lines),' lines, containing ',int2str(dots_lines), ' pixels (',num2str(100*dots_lines./total_dots_orig,3),'% of original number), and ',int2str(dots_backgnd), ' noise pixels'])
theta_rho
function r=find_rho(I,t)
t=t*pi/180;
[y,x]=find(I>0.5);
y=y-1;
x=x-1;
r=(cos(t).*x+sin(t).*y)./(cos(t).^2+sin(t).^2);
r=mean(r);