private static double similarity ( BufferedImage var , BufferedImage cont ) { double [ ] varArr = new double [ var . getWidth ( ) * var . getHeight ( ) * 3 ] ; double [ ] contArr = new double [ cont . getWidth ( ) * cont . getHeight ( ) * 3 ] ; if ( varArr . length != contArr . length ) throw new IllegalStateException ( "The pictures are different sizes!" ) ; //unroll pixels for ( int i = 0 ; i < var . getHeight ( ) ; i ++ ) { for ( int j = 0 ; j < var . getWidth ( ) ; j ++ ) { varArr [ i * var . getWidth ( ) + j + 0 ] = new Color ( var . getRGB ( j , i ) ) . getRed ( ) ; contArr [ i * cont . getWidth ( ) + j + 0 ] = new Color ( cont . getRGB ( j , i ) ) . getRed ( ) ; varArr [ i * var . getWidth ( ) + j + 1 ] = new Color ( var . getRGB ( j , i ) ) . getGreen ( ) ; contArr [ i * cont . getWidth ( ) + j + 1 ] = new Color ( cont . getRGB ( j , i ) ) . getGreen ( ) ; varArr [ i * var . getWidth ( ) + j + 2 ] = new Color ( var . getRGB ( j , i ) ) . getBlue ( ) ; contArr [ i * cont . getWidth ( ) + j + 2 ] = new Color ( cont . getRGB ( j , i ) ) . getBlue ( ) ; } } double mins = 0 ; double maxs = 0 ; for ( int i = 0 ; i != varArr . length ; i ++ ) { if ( varArr [ i ] > contArr [ i ] ) { mins += contArr [ i ] ; maxs += varArr [ i ] ; } else { mins += varArr [ i ] ; maxs += contArr [ i ] ; } } return mins / maxs ; }
Returns a double between 0 and 1 . 0