private void sizeTemplate() {
    Gray8Image template = (Gray8Image) (bestfit.getTemplate().createCopy());
    double maxsim = 1.0 - templateXOR(img, bestfit.getX(), bestfit.getY(), template, false);
    for (double outerdiam = bestfit.getApproxCircleOuterX() - 1; outerdiam > 0; outerdiam--) {
      fillTemplate(template, outerdiam, bestfit.getApproxCircleInnerX(), bestfit.getAspectScale());
      double currsim = 1.0 - templateXOR(img, bestfit.getX(), bestfit.getY(), template, false);
      if (currsim < maxsim) {
        break;
      } else {
        // System.out.println("--outerdiam = " + outerdiam
        // + ":" + currsim);
        bestfit.setTemplate(template);
        bestfit.setApproxCircleOuterX(outerdiam);
        bestfit.setSim(currsim);
        template = (Gray8Image) (bestfit.getTemplate().createCopy());
        maxsim = currsim;
      }
    }

    for (double innerdiam = bestfit.approxCircleInnerX - 1; innerdiam > 0; innerdiam--) {
      fillTemplate(template, bestfit.getApproxCircleOuterX(), innerdiam, bestfit.getAspectScale());
      double currsim = 1.0 - templateXOR(img, bestfit.getX(), bestfit.getY(), template, false);
      if (currsim < maxsim) {
        break;
      } else {
        // System.out.println("--innerdiam = " + innerdiam
        // + ":" + currsim);
        bestfit.setTemplate(template);
        bestfit.setApproxCircleInnerX(innerdiam);
        bestfit.setSim(currsim);
        template = (Gray8Image) (bestfit.getTemplate().createCopy());
        maxsim = currsim;
      }
    }
  }
 private void aspectTemplate() {
   Gray8Image template = (Gray8Image) (bestfit.getTemplate().createCopy());
   double maxsim = 1.0 - templateXOR(img, bestfit.getX(), bestfit.getY(), template, false);
   // System.out.println("maxsim = " + maxsim + ":" + bestfit.getSim());
   double oldaspectscale = bestfit.getAspectScale();
   for (double aspectscale = oldaspectscale - 0.05;
       aspectscale <= oldaspectscale + 0.05;
       aspectscale += 0.0025) {
     fillTemplate(
         template, bestfit.getApproxCircleOuterX(), bestfit.getApproxCircleInnerX(), aspectscale);
     double currsim = 1.0 - templateXOR(img, bestfit.getX(), bestfit.getY(), template, false);
     if (currsim > maxsim) {
       // System.out.println("--aspectscale = " + aspectscale
       // + ":" + currsim);
       bestfit.setTemplate(template);
       bestfit.setAspectScale(aspectscale);
       bestfit.setSim(currsim);
       template = (Gray8Image) (bestfit.getTemplate().createCopy());
       maxsim = currsim;
     }
   }
 }