public static double matAvgDiff(Mat mat1,Mat mat2){
  assertFalse("genMat is empty",mat1.empty());
  assertFalse("gripMat is empty",mat2.empty());
  assertTrue("Mat size is not equal. gencols: " + mat1.cols() + " gripcols: "+ mat2.cols()+ " genrows: "+ mat1.rows()+ " griprows: "+ mat2.rows(),mat1.cols() == mat2.cols() && mat1.rows() == mat2.rows());
  assertTrue("Mats have different number of channels",mat1.channels() == mat2.channels());
  assertTrue("Mats have different Types. Mat1 is type: " + mat1.type() + " Mat 2 is type: "+ mat2.type(),mat1.type() == mat2.type());
  Mat diff=new Mat();
  Core.absdiff(mat1,mat2,diff);
  double matDiff=0;
  for (int row=0; row < diff.rows(); row++) {
    for (int col=0; col < diff.cols(); col++) {
      double[] pixVal=diff.get(row,col);
      double total=0;
      for (      double val : pixVal) {
        total+=val * val;
      }
      matDiff+=Math.sqrt(total);
    }
  }
  return matDiff / (diff.rows() * diff.cols());
}

public static double matAvgDiff ( Mat mat1 , Mat mat2 ) { assertFalse ( "genMat is empty" , mat1 . empty ( ) ) ; assertFalse ( "gripMat is empty" , mat2 . empty ( ) ) ; assertTrue ( "Mat size is not equal. gencols: " + mat1 . cols ( ) + " gripcols: " + mat2 . cols ( ) + " genrows: " + mat1 . rows ( ) + " griprows: " + mat2 . rows ( ) , mat1 . cols ( ) == mat2 . cols ( ) && mat1 . rows ( ) == mat2 . rows ( ) ) ; assertTrue ( "Mats have different number of channels" , mat1 . channels ( ) == mat2 . channels ( ) ) ; assertTrue ( "Mats have different Types. Mat1 is type: " + mat1 . type ( ) + " Mat 2 is type: " + mat2 . type ( ) , mat1 . type ( ) == mat2 . type ( ) ) ; Mat diff = new Mat ( ) ; Core . absdiff ( mat1 , mat2 , diff ) ; double matDiff = 0 ; for ( int row = 0 ; row < diff . rows ( ) ; row ++ ) { for ( int col = 0 ; col < diff . cols ( ) ; col ++ ) { double [ ] pixVal = diff . get ( row , col ) ; double total = 0 ; for ( double val : pixVal ) { total += val * val ; } matDiff += Math . sqrt ( total ) ; } } return matDiff / ( diff . rows ( ) * diff . cols ( ) ) ; }
calculates the average per pixel difference between two mats . if two mats are perfectly equal this will be 0 . less than 10 is a good value for similar images .
