% capture and threshold a binary image, assuming bimodality
function binimage=getbinimage(fig2,fig3,fig4,fig5)

  % capture and dsiplay a grey image
  Imagerbf = liveimagejpg('servo1');
  Imageg = rgb2gray(Imagerbf);
  if fig2 > 0
    figure(fig2)
    colormap(gray)
    imshow(Imageg)
  end

  % threshold to get binary
  hist = dohist(Imageg,fig3);                 % histogram image
  thresh = vsfindthresh(hist,4,fig4);          % find threshold
  binimage = ~myim2bw(Imageg,thresh/255);       % do thresholding
  if fig5 > 0
    figure(fig5)
    colormap(gray)
    imshow(binimage)
  end
