我正在编写一个应用程序,在图像上应用离散小波变换然后反转它应用算法的类工作将在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);
答案 0 :(得分: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没有影响 所以相反,你可以制定你的方法:
public void ApplyHaarTransform(boolean Forward, Bitmap img)
返回图片,现在是
public Bitmap ApplyHaarTransform(boolean Forward, Bitmap img)
在关闭其parathesis之前添加一行代码:
return img;
现在在您的try catch代码中,您的代码将是:
try {
img2=trans.ApplyHaarTransform(true, true, img);
} catch(Exception e) {
Toast.makeText(this,e.getMessage(), Toast.LENGTH_LONG).show();
}