function [segii ledii seglut segx segdx segy segdy rx ry Nl cy]=getledclkparm(aimg,box,bd,exfn,pt,sc)

if(~exist('pt','var') || isempty(pt)) 
	pt=0;
end
if(~exist('sc','var') || isempty(sc)) 
	sc=3*140;
end

gg=sc/3.3/140;

ss=15;
[xx yy]=ndgrid(-ss:ss,-ss:ss);
rr=xx+i*yy; %#ok<*IJCL>
[Nl lx ly]=findpeaks(1./chiimg(aimg(:,box(4):end)/sc,ipf(abs(rr),6,1.2),[]),1,10,0);
[lx ii]=sort(lx);
ly=ly(ii);

ledii=zeros((2*5+1)^2,8);
[xx yy]=ndgrid(-5:5,-5:5);
for n=1:Nl
	ledii(:,n)=flat(sub2ind(size(aimg),xx+lx(n)-ss,box(4)+ly(n)+yy-ss-1));
end

if(pt>0)
	im=double(imread(exfn));
	simage(im(reshape(ledii,11,11*8)));
	pause(pt);
end

%% 7-segment

segx1=lx(end)+find(diff(mean(aimg(lx(end):end,box(4)+3*bd:end),2)>15)==1,1);
segx2=lx(end)+find(diff(mean(aimg(lx(end):end,box(4)+3*bd:end),2)>15)==-1,1,'last');
segx=fix(mean([segx1 segx2],2));
segdx=fix(abs(segx1-segx2)/4*1.11)*4;

segy1=box(4)+3*bd+find(diff(mean(aimg(lx(end):end,box(4)+3*bd:end))>20)==1,1);
segy2=box(4)+3*bd+find(diff(mean(aimg(lx(end):end,box(4)+3*bd:end))>20)==-1,1,'last');
segy=fix(mean([segy1 segy2],2));
segdy=fix(abs(segy1-segy2)/4*1.2)*4+10;

ccy=zeros(2,4);
cpx=[-3/8,-1/8,1/8,3/8];
for nc=1:4
	llx=segx+fix(cpx(nc)*segdx)+fix(-segdx/8:segdx/8);
	lly=segy+fix(-segdy/2:segdy/2);
	ccy(1,nc)=find(diff(mean(aimg(llx,lly,1))>20*gg)==1);
	ccy(2,nc)=find(diff(mean(aimg(llx,lly,1))>20*gg)==-1);
end
cy=mean(ccy)-segdy/2-1;
segdy=mean(diff(ccy));

ry=segdy*[...
	    .4*[1 1 1 1];...   % A
	    .23*[1 1 1 1];...   % B
     -.23*[1 1 1 1];...   % C
     -.4*[1 1 1 1];...   % D
     -.23*[1 1 1 1];...   % E
      .23*[1 1 1 1];...   % F
         [0 0 0 0]];     % G
ry=fix(ry+repmat(cy,7,1));			 

rx=fix(segdx*[...
	[-3/8 -1/8 1/8 3/8]+1/64;...
	[-3/8 -1/8 1/8 3/8]+1/128+1/18;...
	[-3/8 -1/8 1/8 3/8]-1/128+1/18;...				
	[-3/8 -1/8 1/8 3/8]-1/64;...
	[-3/8 -1/8 1/8 3/8]-1/128-1/18;...
	[-3/8 -1/8 1/8 3/8]+1/128-1/18;...				
	[-3/8 -1/8 1/8 3/8]]);

segii=zeros((2*4+1)*(2*8+1),7,4);
[xx yy]=ndgrid(-8:8,-4:4);
for m=[1 4 7]
	for n=1:4
		segii(:,m,n)=...
			flat(sub2ind(size(aimg),xx+rx(m,n)+segx,yy+ry(m,n)+segy));
	end
end
[xx yy]=ndgrid(-4:4,-8:8);
for m=[2 3 5 6]
	for n=1:4
		segii(:,m,n)=...
			flat(sub2ind(size(aimg),xx+rx(m,n)+segx,yy+ry(m,n)+segy));
	end
end

seglut=[...
	[1 0 0 0 0 0 0];...
	[1 1 1 1 0 0 1];...
	[0 1 0 0 1 0 0];...
	[0 1 1 0 0 0 0];...
	[0 0 1 1 0 0 1];...
  [0 0 1 0 0 1 0];...
  [0 0 0 0 0 1 0];...
  [1 1 1 1 0 0 0];...
  [0 0 0 0 0 0 0];...
  [0 0 1 0 0 0 0];...
	[0 0 0 1 0 0 0];...
	[0 0 0 0 0 1 1];...
	[1 0 0 0 1 1 0];...
	[0 1 0 0 0 0 1];...
	[0 0 0 0 1 1 0];...
	[0 0 0 1 1 1 0]];

seglut=fliplr(1-seglut);

if(pt>0)
	simage(im(:,:,1));
	axis([segy+1.2*segdy/2*[-1 1],segx+segdx/2*[-1 1]]);
	hold on;
	plot(segy+ry,segx+rx,'*');
	hold off;
	pause(pt);
end
end
