Android Open Source - CompareImageData_Android C I Comparator






From Project

Back to project page CompareImageData_Android.

License

The source code is released under:

GNU General Public License

If you think the Android project CompareImageData_Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.blogspot.techzealous.utils;
//  w  w w .  j a  v a2  s. co m
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Color;
import android.util.Log;

public class CIComparator extends Object {

  private Bitmap original;
  private Bitmap other;
  
  private int originalWhole [][];
  private int otherWhole [][];
  
  private int originalRed;
  private int originalGreen;
  private int originalBlue;
  private int otherRed;
  private int otherGreen;
  private int otherBlue;
  
  private int avgOriginalR;
  private int avgOriginalG;
  private int avgOriginalB;
  private int avgOtherR;
  private int avgOtherG;
  private int avgOtherB;
  
  private int totalOR;
  private int totalOG;
  private int totalOB;
  private int totalOdR;
  private int totalOdG;
  private int totalOdB;
  
  private int resultOfCompare;
  private int resultOfCompareVectorDiff;
  private double vectorDiff;
  
  public CIComparator() {
    super();
  }
  
  public void setImages(String aImage1, String aImage2, int aSampleRate1, int aSampleRate2) {
    //original = BitmapFactory.decodeResource(res, R.drawable.other3);
    //other = BitmapFactory.decodeResource(res, R.drawable.other6);
    Options opts = new Options();
    opts.inSampleSize = aSampleRate1;//8;
    original = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(aImage1, opts), 200, 200, false);
    opts.inSampleSize = aSampleRate2;
    other = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(aImage2, opts), 200, 200, false);
//    original = Bitmap.createScaledBitmap(original, 100, 100, false);
//    other = Bitmap.createScaledBitmap(other, 100, 100, false);
    
    Log.i("CIComparator", "imageWidth=" + original.getWidth() + ", imageHeight=" + original.getHeight());
    Log.i("CIComparator", "Other imageWidth=" + other.getWidth() + ", Other imageHeight=" + other.getHeight());
  }
  
  public void compareSimple() {
    
    int divideBy = 0;
    int rows = original.getHeight() / 10;
    int cols = original.getWidth() / 10;
    Log.i("CIComparator", "rows=" + rows + ", cols=" + cols);
    for(int row = 0; row < rows; row++) {
      for(int col = 0; col < cols; col++) {
        avgOriginal(row, col);
        avgOther(row, col);
        
        divideBy++;
        
        int resultR = avgOriginalR - avgOtherR;
        int resultG = avgOriginalG - avgOtherG;
        int resultB = avgOriginalB - avgOtherB;
        //threshold 15%
        if((resultR > -40 && resultR < 40) && (resultG > -40 && resultG < 40) && (resultB > - 40 && resultB < 40)) {
          resultOfCompare++;
        }
      }
    }
    
    //compareSimpleAVG
    int avgOR = totalOR / divideBy;
    int avgOG = totalOG / divideBy;
    int avgOB = totalOB / divideBy;
    int avgOdR = totalOdR / divideBy;
    int avgOdG = totalOdG / divideBy;
    int avgOdB = totalOdB / divideBy;
    
    int alikeTotal = (avgOR - avgOdR) + (avgOG - avgOdG) + (avgOB - avgOdB);
    int percent = (alikeTotal * 100) / 255;
    percent = 100 - percent;
    if(percent > 100) { percent = percent - ((percent - 100) * 2);}
    //compareSimpleAVG end
    
    //Log.i("CIComparator", "OR=" + avgOR + ", OG=" + avgOG + ", OB=" + avgOB);
    //Log.i("CIComparator", "OdR=" + avgOdR + ", OdG=" + avgOdG + ", OdB=" + avgOdB);
    
//    Log.i("CIComparator", "compareSimpleAVG alike=" + percent);
//    Log.i("CICOmparator", "compareSimple alike = " + (resultOfCompare * 100) / (rows * cols) + "%");
    
    //set textView text with the results
    CIConstants.RESULT_COMPARESIMPLE = "compareSimple \t\t" + (resultOfCompare * 100) / (rows * cols) + "%";
    CIConstants.RESULT_COMPARESIMPLEAVG = "compareSimpleAVG \t" + percent + "%";
    
    resultOfCompare = 0;
  }
  
  public void compareSimpleAvg() {
    
    int divideBy = 0;
    int rows = original.getHeight() / 10;
    int cols = original.getWidth() / 10;
    Log.i("CIComparator", "rows=" + rows + ", cols=" + cols);
    for(int row = 0; row < rows; row++) {
      for(int col = 0; col < cols; col++) {
        
        avgOriginal(row, col);
        avgOther(row, col);
        
        divideBy++;
      }
    }
    
    int avgOR = totalOR / divideBy;
    int avgOG = totalOG / divideBy;
    int avgOB = totalOB / divideBy;
    int avgOdR = totalOdR / divideBy;
    int avgOdG = totalOdG / divideBy;
    int avgOdB = totalOdB / divideBy;
    
    int alikeTotal = (avgOR - avgOdR) + (avgOG - avgOdG) + (avgOB - avgOdB);
    int percent = (alikeTotal * 100) / 255;
    percent = 100 - percent;
    if(percent > 100) { 
      percent = percent - ((percent - 100) * 2);
    }
    
//    Log.i("CIComparator", "avgPercent=" + percent);
//    Log.i("CIComparator", "OR=" + avgOR + ", OG=" + avgOG + ", OB=" + avgOB);
//    Log.i("CIComparator", "OdR=" + avgOdR + ", OdG=" + avgOdG + ", OdB=" + avgOdB);
  }
  /* Compare 3D vector difference. Smaller difference = more similar */
  public void compareVectorDiffAVG() {
    vectorDiff = 0;
    int rows = original.getHeight() / 10;
    int cols = original.getWidth() / 10;

    for(int row = 0; row < rows; row++) {
      for(int col = 0; col < cols; col++) {
        avgOriginal(row, col);
        avgOther(row, col);
        
        vectorDiff += Math.sqrt((avgOriginalR - avgOtherR) * (avgOriginalR - avgOtherR) + (avgOriginalG - avgOtherG) * (avgOriginalG - avgOtherG) 
            + (avgOriginalB - avgOtherB) * (avgOriginalB - avgOtherB));
        
        //vectorDiff
        double diff = Math.sqrt((avgOriginalR - avgOtherR) * (avgOriginalR - avgOtherR) + (avgOriginalG - avgOtherG) * (avgOriginalG - avgOtherG) 
            + (avgOriginalB - avgOtherB) * (avgOriginalB - avgOtherB));
        //threshold 16%
        if(diff < (CIConstants.MAX_DIFF_VALUE / 6)) {
          resultOfCompareVectorDiff++;
        }
        //end vectorDiff
      }
    }
    Log.i("CIComparator", "vectorDiff=" + vectorDiff);
    //the result as a percent of the maximum vectorDiff
    int result = (int) (vectorDiff * 100 / (CIConstants.MAX_DIFF_VALUE * (rows * cols)));
    result = 100 - result;
    CIConstants.RESULT_COMPAREVECTORDIFF_AVG = "compareVectorDiffAVG \t" + String.valueOf(result) + "%";
    
    CIConstants.RESULT_COMPAREVECTORDIFF = "compareVectorDiff \t" + (resultOfCompareVectorDiff * 100) / (rows * cols) + "%";
    resultOfCompareVectorDiff = 0;
  }
  
  private int avgColor(int aRow, int aCol) {
    //calculate for one square 10x10 pixels
    originalRed = 0;
    originalGreen = 0;
    originalBlue = 0;
    
    for(int y = aRow * 10 ; y < (aRow * 10) + 10; y++) {
      for(int x = aCol * 10; x < (aCol * 10) + 10; x++) {
        int pixel = original.getPixel(x, y);
        originalRed += Color.red(pixel);
        originalGreen += Color.green(pixel);
        originalBlue += Color.blue(pixel);
//        Log.i("CIComparator", "avgColor()-" + "verticalRegion = " + y + ", horizontalRegion = " + x);
      }
    }
    int avgR = originalRed / 100;
    int avgG = originalGreen / 100;
    int avgB = originalBlue / 100;
    int color = Color.rgb(avgR, avgG, avgB);
//    Log.i("CIComparator", "avgR=" + avgR + ", avgG=" + avgG + ", avgB=" + avgB);
    return color;
  }
  
  private void avgOriginal(int aRow, int aCol) {
    //calculate for one square 10x10 pixels
        originalRed = 0;
        originalGreen = 0;
        originalBlue = 0;
        
        for(int y = aRow * 10 ; y < (aRow * 10) + 10; y++) {
          for(int x = aCol * 10; x < (aCol * 10) + 10; x++) {
            int pixel = original.getPixel(x, y);
            originalRed += Color.red(pixel);
            originalGreen += Color.green(pixel);
            originalBlue += Color.blue(pixel);
          }
        }
        int avgR = originalRed / 100;
        int avgG = originalGreen / 100;
        int avgB = originalBlue / 100;
//        Log.i("CIComparator", "avgOriginalR=" + avgR + ", avgOriginalG=" + avgG + ", avgOriginalB=" + avgB);
        
        avgOriginalR = avgR;
        avgOriginalG = avgG;
        avgOriginalB = avgB;
        
        totalOR += avgR;
        totalOG += avgG;
        totalOB += avgB;
  }
  
  private void avgOther(int aRow, int aCol) {
    //calculate for one square 10x10 pixels
        otherRed = 0;
        otherGreen = 0;
        otherBlue = 0;
        
        for(int y = aRow * 10 ; y < (aRow * 10) + 10; y++) {
          for(int x = aCol * 10; x < (aCol * 10) + 10; x++) {
            int pixel = other.getPixel(x, y);
            otherRed += Color.red(pixel);
            otherGreen += Color.green(pixel);
            otherBlue += Color.blue(pixel);
          }
        }
        int avgR = otherRed / 100;
        int avgG = otherGreen / 100;
        int avgB = otherBlue / 100;
//        Log.i("CIComparator", "avgOtherR=" + avgR + ", avgOtherG=" + avgG + ", avgOtherB=" + avgB);
        
        avgOtherR = avgR;
        avgOtherG = avgG;
        avgOtherB = avgB;
        
        totalOdR += avgR;
        totalOdG += avgG;
        totalOdB += avgB;
  }
  
  private void calcVectors(int aRow, int aCol) {
    originalWhole = new int [10][10];
    otherWhole = new int [10][10];
        
    int rows = original.getHeight() / 10;
    int cols = original.getWidth() / 10;
    for(int row = 0; row < rows; row++) {
      for(int col = 0; col < cols; col++) {
        originalWhole[row][col] = avgColor(row, col); 
        otherWhole[row][col] = avgColor(row, col);
        Log.i("CIComparator", "row=" + row + ", col=" + col + ", color=" + originalWhole[row][col]);
      }
    }
  }
  
}




Java Source Code List

com.blogspot.techzealous.compareimages.CIMainActivity.java
com.blogspot.techzealous.utils.CIComparator.java
com.blogspot.techzealous.utils.CIConstants.java