Program: 22718
Project: 22718
Buggy source:
/** 
 * Calculate the Rand index between to clusters, as described by William M. Rand \cite{Rand71}. Note that this version of the Rand index treats the zero component (background) as another  object. BibTeX: <pre> &#64;article{Rand71, author    = {William M. Rand}, title     = {Objective criteria for the evaluation of clustering methods}, journal   = {Journal of the American Statistical Association}, year      = {1971}, volume    = {66}, number    = {336}, pages     = {846--850}, doi       = {10.2307/2284239) } </pre>
 * @param cluster1 2D segmented image (objects are labeled with different numbers) 
 * @param cluster2 2D segmented image (objects are labeled with different numbers)
 * @return Rand index
 */
public double classicRandIndex(ShortProcessor cluster1,ShortProcessor cluster2){
  final short[] pixels1=(short[])cluster1.getPixels();
  final short[] pixels2=(short[])cluster2.getPixels();
  double n=pixels1.length;
  int[][] cont=new int[(int)cluster1.getMax()][(int)cluster2.getMax()];
  for (int i=0; i < n; i++)   cont[pixels1[i]][pixels2[i]]++;
  double t2=0;
  double[] ni=new double[cont.length];
  for (int i=0; i < cont.length; i++)   for (int j=0; j < cont[i].length; j++)   ni[i]+=cont[i][j];
  double nis=0;
  for (int k=0; k < ni.length; k++)   nis+=ni[k] * ni[k];
  double[] nj=new double[cont.length];
  for (int j=0; j < cont[0].length; j++)   for (int i=0; i < cont.length; i++) {
    nj[j]+=cont[i][j];
    t2+=cont[i][j] * cont[i][j];
  }
  double njs=0;
  for (int k=0; k < nj.length; k++)   njs+=nj[k] * nj[k];
  double t1=n * (n - 1) / 2;
  double t3=0.5 * (nis + njs);
  double agreements=t1 + t2 - t3;
  return agreements / t1;
}

Fixed version (ground truth):
/** 
 * Calculate the Rand index between to clusters, as described by William M. Rand \cite{Rand71}. Note that this version of the Rand index treats the zero component (background) as another  object. BibTeX: <pre> &#64;article{Rand71, author    = {William M. Rand}, title     = {Objective criteria for the evaluation of clustering methods}, journal   = {Journal of the American Statistical Association}, year      = {1971}, volume    = {66}, number    = {336}, pages     = {846--850}, doi       = {10.2307/2284239) } </pre>
 * @param cluster1 2D segmented image (objects are labeled with different numbers) 
 * @param cluster2 2D segmented image (objects are labeled with different numbers)
 * @return Rand index
 */
public double classicRandIndex(ShortProcessor cluster1,ShortProcessor cluster2){
  final short[] pixels1=(short[])cluster1.getPixels();
  final short[] pixels2=(short[])cluster2.getPixels();
  double n=pixels1.length;
  int[][] cont=new int[(int)cluster1.getMax()][(int)cluster2.getMax()];
  for (int i=0; i < n; i++)   cont[pixels1[i] & 0xffff][pixels2[i] & 0xffff]++;
  double t2=0;
  double[] ni=new double[cont.length];
  for (int i=0; i < cont.length; i++)   for (int j=0; j < cont[i].length; j++)   ni[i]+=cont[i][j];
  double nis=0;
  for (int k=0; k < ni.length; k++)   nis+=ni[k] * ni[k];
  double[] nj=new double[cont.length];
  for (int j=0; j < cont[0].length; j++)   for (int i=0; i < cont.length; i++) {
    nj[j]+=cont[i][j];
    t2+=cont[i][j] * cont[i][j];
  }
  double njs=0;
  for (int k=0; k < nj.length; k++)   njs+=nj[k] * nj[k];
  double t1=n * (n - 1) / 2;
  double t3=0.5 * (nis + njs);
  double agreements=t1 + t2 - t3;
  return agreements / t1;
}