Java 中的离散小波变换在图像中创建白点
Discrete Wavelet Transform in Java creates white Spots in the Image
在我的Java程序中,图像被加载到程序中,然后使用离散小波变换进行变换,所得系数用作输出图像的图片数据。
该过程适用于自然图像:http://imgur.com/Pk3kUs7
但是,如果我对例如卡通图像进行变换,则在近似子带中的暗边缘上会出现白点:http://imgur.com/kLXyBvd
这是 forwardDWT 的代码:
private int[][] transformPixels(int[][] pixels, int widthHeight) {
double[][] temp_bank = new double[widthHeight][widthHeight];
double a1 = -1.586134342;
double a2 = -0.05298011854;
double a3 = 0.8829110762;
double a4 = 0.4435068522;
// Scale coeff:
double k1 = 0.81289306611596146; // 1/1.230174104914
double k2 = 0.61508705245700002;// 1.230174104914/2
for (int i = 0; i < 2; i++) {
for (int col = 0; col < widthHeight; col++) {
// Predict 1
for (int row = 1; row < widthHeight - 1; row += 2) {
pixels[row][col] += a1 * (pixels[row - 1][col] + pixels[row + 1][col]);
}
pixels[widthHeight - 1][col] += 2 * a1 * pixels[widthHeight - 2][col];
// Update 1
for (int row = 2; row < widthHeight; row += 2) {
pixels[row][col] += a2 * (pixels[row - 1][col] + pixels[row + 1][col]);
}
pixels[0][col] += 2 * a2 * pixels[1][col];
// Predict 2
for (int row = 1; row < widthHeight - 1; row += 2) {
pixels[row][col] += a3 * (pixels[row - 1][col] + pixels[row + 1][col]);
}
pixels[widthHeight - 1][col] += 2 * a3 * pixels[widthHeight - 2][col];
// Update 2
for (int row = 2; row < widthHeight; row += 2) {
pixels[row][col] += a4 * (pixels[row - 1][col] + pixels[row + 1][col]);
}
pixels[0][col] += 2 * a4 * pixels[1][col];
}
for (int row = 0; row < widthHeight; row++) {
for (int col = 0; col < widthHeight; col++) {
if (row % 2 == 0)
temp_bank[col][row / 2] = k1 * pixels[row][col];
else
temp_bank[col][row / 2 + widthHeight / 2] = k2 * pixels[row][col];
}
}
for (int row = 0; row < widthHeight; row++) {
for (int col = 0; col < widthHeight; col++) {
pixels[row][col] = (int) temp_bank[row][col];
}
}
}
return pixels;
}
这是使用提升方案实现的 CDF9/7 fitlerbanks 的 DWT,类似于 JPEG2000 中的 DWT。
该算法有两个限制:
- 只能处理灰度数据
- 图片的宽度和高度必须相同并且是2^n的乘积,例如256x256、512x512 等
因为也可能是灰度值计算错误,这里是加载图像的其他代码,开始转换,将 rgb 值转换为灰度值并转换回 rgb:
public BufferedImage openImage() throws InvalidWidthHeightException {
try {
int returnVal = fc.showOpenDialog(panel);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
BufferedImage temp = ImageIO.read(file);
if (temp == null)
return null;
int checkInt = temp.getWidth();
boolean check = (checkInt & (checkInt - 1)) == 0;
if (checkInt != temp.getHeight() & !check)
throw new InvalidWidthHeightException();
int widthandHeight = temp.getWidth();
image = new BufferedImage(widthandHeight, widthandHeight, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
g.drawImage(temp, 0, 0, null);
g.dispose();
return image;
}
} catch (IOException e) {
System.out.println("Failed to load image!");
}
return null;
}
public void transform(int count) {
int[][] pixels = getGrayValues(image);
int transformedPixels[][];
int width = pixels.length;
transformedPixels = transformPixels(pixels, width);
width/=2;
for (int i = 1; i < count + 1; i++) {
transformedPixels = transformPixels(transformedPixels, width);
width/=2;
}
width = pixels.length;
transformedImage = new BufferedImage(width, width, BufferedImage.TYPE_BYTE_GRAY);
for (int x = 0; x < width; x++) {
for (int y = 0; y < width; y++) {
transformedImage.setRGB(x, y, tranformToRGB(transformedPixels[x][y]));
}
}
}
private int tranformToRGB(double d) {
int value = (int) d;
if (d < 0)
d = 0;
if (d > 255)
d = 255;
return 0xffffffff << 24 | value << 16 | value << 8 | value;
}
private int[][] getGrayValues(BufferedImage image2) {
int[][] res = new int[image.getHeight()][image.getWidth()];
int r, g, b;
for (int i = 0; i < image.getWidth(); i++) {
for (int j = 0; j < image.getHeight(); j++) {
int value = image2.getRGB(i, j);
r = (value >> 16) & 0xFF;
g = (value >> 8) & 0xFF;
b = (value & 0xFF);
res[i][j] = (r + g + b) / 3;
}
}
return res;
}
注意:因为图像的宽度和高度应该是相同的,所以我有时只是将宽度也用作高度。
编辑:正如@stuhlo 所建议的,我在 forwardDWT 中添加了对近似子带值的检查:
for (int row = 0; row < widthHeight; row++) {
for (int col = 0; col < widthHeight; col++) {
if (row % 2 == 0) {
double value = k1 * pixels[row][col];
if (value > 255)
value = 255;
if (value < 0)
value = 0;
temp_bank[col][row / 2] = value;
} else {
temp_bank[col][row / 2 + widthHeight / 2] = k2 * pixels[row][col];
}
}
}
不幸的是,现在水平细节的子栏变成黑色了。
您的问题是由于子带样本需要比原始图像样本更多的比特来存储。
我建议使用更大的数据类型来存储子带样本并将它们归一化为 8 位值以供显示。
在我的Java程序中,图像被加载到程序中,然后使用离散小波变换进行变换,所得系数用作输出图像的图片数据。
该过程适用于自然图像:http://imgur.com/Pk3kUs7
但是,如果我对例如卡通图像进行变换,则在近似子带中的暗边缘上会出现白点:http://imgur.com/kLXyBvd
这是 forwardDWT 的代码:
private int[][] transformPixels(int[][] pixels, int widthHeight) {
double[][] temp_bank = new double[widthHeight][widthHeight];
double a1 = -1.586134342;
double a2 = -0.05298011854;
double a3 = 0.8829110762;
double a4 = 0.4435068522;
// Scale coeff:
double k1 = 0.81289306611596146; // 1/1.230174104914
double k2 = 0.61508705245700002;// 1.230174104914/2
for (int i = 0; i < 2; i++) {
for (int col = 0; col < widthHeight; col++) {
// Predict 1
for (int row = 1; row < widthHeight - 1; row += 2) {
pixels[row][col] += a1 * (pixels[row - 1][col] + pixels[row + 1][col]);
}
pixels[widthHeight - 1][col] += 2 * a1 * pixels[widthHeight - 2][col];
// Update 1
for (int row = 2; row < widthHeight; row += 2) {
pixels[row][col] += a2 * (pixels[row - 1][col] + pixels[row + 1][col]);
}
pixels[0][col] += 2 * a2 * pixels[1][col];
// Predict 2
for (int row = 1; row < widthHeight - 1; row += 2) {
pixels[row][col] += a3 * (pixels[row - 1][col] + pixels[row + 1][col]);
}
pixels[widthHeight - 1][col] += 2 * a3 * pixels[widthHeight - 2][col];
// Update 2
for (int row = 2; row < widthHeight; row += 2) {
pixels[row][col] += a4 * (pixels[row - 1][col] + pixels[row + 1][col]);
}
pixels[0][col] += 2 * a4 * pixels[1][col];
}
for (int row = 0; row < widthHeight; row++) {
for (int col = 0; col < widthHeight; col++) {
if (row % 2 == 0)
temp_bank[col][row / 2] = k1 * pixels[row][col];
else
temp_bank[col][row / 2 + widthHeight / 2] = k2 * pixels[row][col];
}
}
for (int row = 0; row < widthHeight; row++) {
for (int col = 0; col < widthHeight; col++) {
pixels[row][col] = (int) temp_bank[row][col];
}
}
}
return pixels;
}
这是使用提升方案实现的 CDF9/7 fitlerbanks 的 DWT,类似于 JPEG2000 中的 DWT。
该算法有两个限制:
- 只能处理灰度数据
- 图片的宽度和高度必须相同并且是2^n的乘积,例如256x256、512x512 等
因为也可能是灰度值计算错误,这里是加载图像的其他代码,开始转换,将 rgb 值转换为灰度值并转换回 rgb:
public BufferedImage openImage() throws InvalidWidthHeightException {
try {
int returnVal = fc.showOpenDialog(panel);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
BufferedImage temp = ImageIO.read(file);
if (temp == null)
return null;
int checkInt = temp.getWidth();
boolean check = (checkInt & (checkInt - 1)) == 0;
if (checkInt != temp.getHeight() & !check)
throw new InvalidWidthHeightException();
int widthandHeight = temp.getWidth();
image = new BufferedImage(widthandHeight, widthandHeight, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
g.drawImage(temp, 0, 0, null);
g.dispose();
return image;
}
} catch (IOException e) {
System.out.println("Failed to load image!");
}
return null;
}
public void transform(int count) {
int[][] pixels = getGrayValues(image);
int transformedPixels[][];
int width = pixels.length;
transformedPixels = transformPixels(pixels, width);
width/=2;
for (int i = 1; i < count + 1; i++) {
transformedPixels = transformPixels(transformedPixels, width);
width/=2;
}
width = pixels.length;
transformedImage = new BufferedImage(width, width, BufferedImage.TYPE_BYTE_GRAY);
for (int x = 0; x < width; x++) {
for (int y = 0; y < width; y++) {
transformedImage.setRGB(x, y, tranformToRGB(transformedPixels[x][y]));
}
}
}
private int tranformToRGB(double d) {
int value = (int) d;
if (d < 0)
d = 0;
if (d > 255)
d = 255;
return 0xffffffff << 24 | value << 16 | value << 8 | value;
}
private int[][] getGrayValues(BufferedImage image2) {
int[][] res = new int[image.getHeight()][image.getWidth()];
int r, g, b;
for (int i = 0; i < image.getWidth(); i++) {
for (int j = 0; j < image.getHeight(); j++) {
int value = image2.getRGB(i, j);
r = (value >> 16) & 0xFF;
g = (value >> 8) & 0xFF;
b = (value & 0xFF);
res[i][j] = (r + g + b) / 3;
}
}
return res;
}
注意:因为图像的宽度和高度应该是相同的,所以我有时只是将宽度也用作高度。
编辑:正如@stuhlo 所建议的,我在 forwardDWT 中添加了对近似子带值的检查:
for (int row = 0; row < widthHeight; row++) {
for (int col = 0; col < widthHeight; col++) {
if (row % 2 == 0) {
double value = k1 * pixels[row][col];
if (value > 255)
value = 255;
if (value < 0)
value = 0;
temp_bank[col][row / 2] = value;
} else {
temp_bank[col][row / 2 + widthHeight / 2] = k2 * pixels[row][col];
}
}
}
不幸的是,现在水平细节的子栏变成黑色了。
您的问题是由于子带样本需要比原始图像样本更多的比特来存储。
我建议使用更大的数据类型来存储子带样本并将它们归一化为 8 位值以供显示。