2015-04-22 47 views
1

我正在編寫一個應用程序來應用圖像上的離散小波變換,然後反演它應用算法的類將在java中工作,但是當我嘗試將它轉換爲android平臺的圖像劑量沒有出現我不知道爲什麼這裏是類和主要活動代碼:在android上實現離散小波變換

WTC使用哈爾類:

import android.graphics.Bitmap; 

/** 
* @author the-e_000 
*/ 
public class haar { 

    private final double w0 = 0.5; 
    private final double w1 = -0.5; 
    private final double s0 = 0.5; 
    private final double s1 = 0.5; 

    /// <summary> 
    /// Discrete Haar Wavelet Transform 
    /// </summary> 
    /// 
    public void FWT(double[] data) { 
     double[] temp = new double[data.length]; 

     int h = data.length >> 1; 
     for (int i = 0; i < h; i++) { 
      int k = (i << 1); 
      temp[i] = data[k] * s0 + data[k + 1] * s1; 
      temp[i + h] = data[k] * w0 + data[k + 1] * w1; 
     } 

     for (int i = 0; i < data.length; i++) 
      data[i] = temp[i]; 
    } 

    /// <summary> 
    /// Discrete Haar Wavelet 2D Transform 
    /// </summary> 
    /// 
    public void FWT(double[][] data, int iterations) { 
     int rows = data.length; 
     int cols = data[0].length; 
     double[] row = new double[cols]; 
     double[] col = new double[rows]; 
     for (int k = 0; k < iterations; k++) { 
      for (int i = 0; i < rows; i++) { 
       for (int j = 0; j < row.length; j++) 
        row[j] = data[i][j]; 
       FWT(row); 
       for (int j = 0; j < row.length; j++) 
        data[i][j] = row[j]; 
      } 
      for (int j = 0; j < cols; j++) { 
       for (int i = 0; i < col.length; i++) 
        col[i] = data[i][j]; 
       FWT(col); 
       for (int i = 0; i < col.length; i++) 
        data[i][j] = col[i]; 
      } 
     } 
    } 

    /// <summary> 
    /// Inverse Haar Wavelet Transform 
    /// </summary> 
    /// 
    public void IWT(double[] data) { 
     double[] temp = new double[data.length]; 

     int h = data.length >> 1; 
     for (int i = 0; i < h; i++) { 
      int k = (i << 1); 
      temp[k] = (data[i] * s0 + data[i + h] * w0)/w0; 
      temp[k + 1] = (data[i] * s1 + data[i + h] * w1)/s0; 
     } 

     for (int i = 0; i < data.length; i++) 
      data[i] = temp[i]; 
    } 

    /// <summary> 
    /// Inverse Haar Wavelet 2D Transform 
    /// </summary> 
    /// 
    public void IWT(double[][] data, int iterations) { 
     int rows = data.length; 
     int cols = data[0].length; 
     double[] col = new double[rows]; 
     double[] row = new double[cols]; 
     for (int l = 0; l < iterations; l++) { 
      for (int j = 0; j < cols; j++) { 
       for (int i = 0; i < row.length; i++) 
        col[i] = data[i][j]; 
       IWT(col); 
       for (int i = 0; i < col.length; i++) 
        data[i][j] = col[i]; 
      } 
      for (int i = 0; i < rows; i++) { 
       for (int j = 0; j < row.length; j++) 
        row[j] = data[i][j]; 
       IWT(row); 
       for (int j = 0; j < row.length; j++) 
        data[i][j] = row[j]; 
      } 
     } 
    } 

    public double Scale(double fromMin, double fromMax, double toMin, double toMax, double x) { 
     if (fromMax - fromMin == 0) { 
      return 0; 
     } 
     double value = (toMax - toMin) * (x - fromMin)/(fromMax - fromMin) + toMin; 
     if (value > toMax) { 
      value = toMax; 
     } 
     if (value < toMin) { 
      value = toMin; 
     } 
     return value; 
    } 

    public void ApplyHaarTransform(boolean Forward, Bitmap img) throws Exception { 

     int Iterations = 1; 
     double[][] Red = new double[img.getWidth()][img.getHeight()]; 
     double[][] Green = new double[img.getWidth()][img.getHeight()]; 
     double[][] Blue = new double[img.getWidth()][img.getHeight()]; 
     int c; 
     for (int j = 0; j < img.getHeight(); j++) { 
      for (int i = 0; i < img.getWidth(); i++) { 
       c = img.getPixel(i, j); 
       Red[i][j] = (double) Scale(0, 255, -1, 1, (c >> 16) & 0x000000FF); 
       Green[i][j] = (double) Scale(0, 255, -1, 1, (c >> 8) & 0x000000FF); 
       Blue[i][j] = (double) Scale(0, 255, -1, 1, (c) & 0x000000FF); 
      } 
     } 
     if (Forward) { 
      FWT(Red, Iterations); 
      FWT(Green, Iterations); 
      FWT(Blue, Iterations); 
     } else { 
      IWT(Red, Iterations); 
      IWT(Green, Iterations); 
      IWT(Blue, Iterations); 
     } 
     for (int j = 0; j < img.getHeight(); j++) { 
      for (int i = 0; i < img.getWidth(); i++) { 
       int r = (int) Scale(-1, 1, 0, 255, Red[i][j]);// red component 0...255 
       int g = (int) Scale(-1, 1, 0, 255, Green[i][j]);// green component 0...255 
       int b = (int) Scale(-1, 1, 0, 255, Blue[i][j]);// blue component 0...255 
       int col = (r << 16) | (g << 8) | b; 
       img.setPixel(i, j, col); 
      } 
     } 
    } 
} 

主要活動:

Bitmap img = BitmapFactory.decodeFile(imgDecodableString); 
ImageView imgView = (ImageView) findViewById(R.id.imgView); 
// take the image and add the transform 
haar trans = new haar(); 
Bitmap img2 = null; 
try { 
    trans.ApplyHaarTransform(true, true, img); 
} catch(Exception e) { 
    Toast.makeText(this,e.getMessage(), Toast.LENGTH_LONG).show(); 
} 
// Set the Image in ImageView after decoding the String 
imgView.setImageBitmap(img2); 

回答

1

在你的代碼ü把一個空的圖像中的ImageView:

Bitmap img2 = null; 

和改造你的代碼後

imgView.setImageBitmap(img2); 

try { 
    trans.ApplyHaarTransform(true, true, img); 
} catch(Exception e) { 
    Toast.makeText(this,e.getMessage(), Toast.LENGTH_LONG).show(); 
} 

對IMG2 沒有影響所以不是U可以讓你的方法:

public void ApplyHaarTransform(boolean Forward, Bitmap img) 

回報形象,現在是

public Bitmap ApplyHaarTransform(boolean Forward, Bitmap img) 

,並關閉其parathesis添加一行代碼之前:

return img; 

,現在在你嘗試捕捉代碼yourcode將是:

try { 
    img2=trans.ApplyHaarTransform(true, true, img); 
} catch(Exception e) { 
    Toast.makeText(this,e.getMessage(), Toast.LENGTH_LONG).show(); 
}