Program: 25434
Project: 25434
Buggy source:
void Otsu(ImagePlus imp,int radius,double par1,double par2,boolean doIwhite){
  int[] data;
  int w=imp.getWidth();
  int h=imp.getHeight();
  int position;
  int radiusx2=radius * 2;
  ImageProcessor ip=imp.getProcessor();
  byte[] pixels=(byte[])ip.getPixels();
  byte[] pixelsOut=new byte[pixels.length];
  byte object;
  byte backg;
  if (doIwhite) {
    object=(byte)0xff;
    backg=(byte)0;
  }
 else {
    object=(byte)0;
    backg=(byte)0xff;
  }
  int k, kStar;
  int N1, N;
  double BCV, BCVmax;
  double num, denom;
  int Sk;
  int S, L=256;
  int roiy;
  Roi roi=new OvalRoi(0,0,radiusx2,radiusx2);
  for (int y=0; y < h; y++) {
    IJ.showProgress((double)(y) / (h - 1));
    roiy=y - radius;
    for (int x=0; x < w; x++) {
      roi.setLocation(x - radius,roiy);
      ip.setRoi(roi);
      position=x + y * w;
      data=ip.getHistogram();
      S=N=0;
      for (k=0; k < L; k++) {
        S+=k * data[k];
        N+=data[k];
      }
      Sk=0;
      N1=data[0];
      BCV=0;
      BCVmax=0;
      kStar=0;
      for (k=1; k < L - 1; k++) {
        Sk+=k * data[k];
        N1+=data[k];
        denom=(double)(N1) * (N - N1);
        if (denom != 0) {
          num=((double)N1 / N) * S - Sk;
          BCV=(num * num) / denom;
        }
 else         BCV=0;
        if (BCV >= BCVmax) {
          BCVmax=BCV;
          kStar=k;
        }
      }
      pixelsOut[position]=((int)(pixels[position] & 0xff) > kStar) ? object : backg;
    }
  }
  for (position=0; position < w * h; position++)   pixels[position]=pixelsOut[position];
}

Fixed version (ground truth):
void Otsu(ImagePlus imp,int radius,double par1,double par2,boolean doIwhite){
  int[] data;
  int w=imp.getWidth();
  int h=imp.getHeight();
  int position;
  int radiusx2=radius * 2;
  ImageProcessor ip=imp.getProcessor();
  byte[] pixels=(byte[])ip.getPixels();
  byte[] pixelsOut=new byte[pixels.length];
  byte object;
  byte backg;
  if (doIwhite) {
    object=(byte)0xff;
    backg=(byte)0;
  }
 else {
    object=(byte)0;
    backg=(byte)0xff;
  }
  int ih, roiy, L=256;
  int threshold;
  int num_pixels;
  double total_mean;
  double bcv, term;
  double max_bcv;
  double[] cnh=new double[L];
  double[] mean=new double[L];
  double[] histo=new double[L];
  Roi roi=new OvalRoi(0,0,radiusx2,radiusx2);
  for (int y=0; y < h; y++) {
    IJ.showProgress((double)(y) / (h - 1));
    roiy=y - radius;
    for (int x=0; x < w; x++) {
      roi.setLocation(x - radius,roiy);
      ip.setRoi(roi);
      position=x + y * w;
      data=ip.getHistogram();
      num_pixels=0;
      for (ih=0; ih < L; ih++)       num_pixels=num_pixels + data[ih];
      term=1.0 / (double)num_pixels;
      for (ih=0; ih < L; ih++) {
        histo[ih]=term * data[ih];
      }
      cnh[0]=histo[0];
      for (ih=1; ih < L; ih++) {
        cnh[ih]=cnh[ih - 1] + histo[ih];
      }
      mean[0]=0.0;
      for (ih=0 + 1; ih < L; ih++) {
        mean[ih]=mean[ih - 1] + ih * histo[ih];
      }
      total_mean=mean[L - 1];
      threshold=Integer.MIN_VALUE;
      max_bcv=0.0;
      for (ih=0; ih < L; ih++) {
        bcv=total_mean * cnh[ih] - mean[ih];
        bcv*=bcv / (cnh[ih] * (1.0 - cnh[ih]));
        if (max_bcv < bcv) {
          max_bcv=bcv;
          threshold=ih;
        }
      }
      pixelsOut[position]=((int)(pixels[position] & 0xff) > threshold) ? object : backg;
    }
  }
  for (position=0; position < w * h; position++)   pixels[position]=pixelsOut[position];
}