% resolution_distortion.m
% returns the diameter of the star and a correction factor (distortion
% correction) for each segment
% distortion correction just works in comercial version IE-Analyzer

function [ds,segfactor]=resolution_distortion(STARCAL,centerx,centery,segments);

segments=24;
[h,w,d]=size(STARCAL);
ds=round((7874/8504)*h);     
rs=round(ds/2);


rect=[centerx+rs-round(.1*w) centery-round(.05*h) round(0.2*w) round(0.1*h)];
image_work = imcrop(STARCAL,rect);
image_work = imadjust(image_work);
level      = graythresh(image_work);
image_bw   = im2bw(image_work,level);
se         = strel('line',round(0.02*ds),90);
image_bw   = imdilate(image_bw,se);
image_vec  = abs(diff(mean(image_bw,1)));
[tmp,posmax]= max(image_vec);
    
if posmax<=3 || posmax > .9*numel(image_vec)

    po=figure('Name','Click into the edge and press Enter');
    imshow(image_work);
    [posx,posy] = getpts(po);
    close (po);
    rs=round( sqrt( (posx+rect(1)-centerx)^2 + (posy+rect(2)-centery)^2));
    ds=rs*2;
    
else
    
    rs=posmax+rect(1)-centerx;
    ds=rs*2;                    
    
end


segfactor=ones(1,segments);
