简体   繁体   English

在Android上实现离散小波变换

[英]implementing Discrete Wavelet Transformation on android

I'm writing an application to applying the Discrete Wavelet Transformation on an image and then inverse it the class for applying the algorithm works will in java but when i try to convert it to android platform the image dose not appear i don't know why here is the code for the class and the main activity : 我正在编写一个将离散小波变换应用到图像上的应用程序,然后将其逆转,用于应用算法的类将在Java中工作,但是当我尝试将其转换为android平台时,图像却没有出现,我不知道为什么这是该类和主要活动的代码:

wtc using haar class : wtc使用haar类:

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);
            }
        }
    }
}

main activity : 主要活动 :

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);

in your code u put a null image in imageview: 在您的代码中,在imageview中放置一个空图像:

Bitmap img2 = null;

and after transformation your code 然后转换您的代码

imgView.setImageBitmap(img2);

but

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

have no effect on img2 so instead u may make your method : 对img2没有影响,所以您可以使用您的方法:

public void ApplyHaarTransform(boolean Forward, Bitmap img)

returns image and now it is 返回图像,现在是

public Bitmap ApplyHaarTransform(boolean Forward, Bitmap img)

and before closing its parathesis add one line of code : 在关闭其特质之前,添加一行代码:

return img;

and now in your try catch code yourcode will be: 现在在您的尝试捕获代码中,您的代码将是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM