我正在编写一个将离散小波变换应用到图像上的应用程序,然后将其逆转,用于应用算法的类将在Java中工作,但是当我尝试将其转换为android平台时,图像却没有出现,我不知道为什么这是该类和主要活动的代码:
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);
}
}
}
}
主要活动 :
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);
最佳答案
在您的代码中,在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没有影响
因此,您可以使用您的方法:
public void ApplyHaarTransform(boolean Forward, Bitmap img)
返回图像,现在是
public Bitmap ApplyHaarTransform(boolean Forward, Bitmap img)
在关闭其特质之前,添加一行代码:
return img;
现在在您的尝试捕获代码中,您的代码将是:
try {
img2=trans.ApplyHaarTransform(true, true, img);
} catch(Exception e) {
Toast.makeText(this,e.getMessage(), Toast.LENGTH_LONG).show();
}