Instantly share code, notes, and snippets.

@heidisu /App.java
Last active Jul 24, 2018

Embed
What would you like to do?
Haar transform
package haar;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.io.IOException;
public class App
{
public static void main( String[] args )
{
try {
BufferedImage image = ImageIO.read(App.class.getClassLoader().getResourceAsStream("Lenna.png"));
Image grayScale = convertToGrayScale(image);
BufferedImage bufferedImage = toBufferedImage(grayScale);
displayImage(bufferedImage);
double[][] intmatrix = imageToMatrix(bufferedImage);
double[][] result = haarTransform.transform(intmatrix);
BufferedImage updated = updateImage(bufferedImage, result);
displayImage(updated);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void displayImage(Image image){
JLabel lblimage = new JLabel(new ImageIcon(image));
JFrame frame = new JFrame();
frame.getContentPane().add(lblimage, BorderLayout.CENTER);
frame.setSize(512, 512);
frame.setVisible(true);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(lblimage);
frame.add(mainPanel);
frame.setVisible(true);
}
private static Image convertToGrayScale(BufferedImage colorImage){
ImageFilter filter = new GrayFilter(true, 10);
ImageProducer producer = new FilteredImageSource(colorImage.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(producer);
}
private static BufferedImage toBufferedImage(Image img)
{
if (img instanceof BufferedImage)
{
return (BufferedImage) img;
}
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
return bimage;
}
private static double[][] imageToMatrix(BufferedImage image){
double[][] matrix = new double[image.getWidth()][image.getHeight()];
for(int i = 0; i < image.getWidth(); i++){
for(int j = 0; j < image.getHeight(); j++){
matrix[i][j] = getPixel(image, i, j);
}
}
return matrix;
}
private static int getPixel(BufferedImage image, int x, int y){
int clr = image.getRGB(x, y);
return clr & 0x000000ff;
}
private static BufferedImage updateImage(BufferedImage image, double[][] transform){
for(int i = 0; i< transform.length; i++){
for(int j = 0; j < transform.length; j++){
int value = (int) transform[i][j];
value = value < 0 ? 255 + value : value;
//value = 128 + value;
Color color = new Color(value, value, value);
image.setRGB(i, j, color.getRGB());
}
}
return image;
}
}
package haar;
public class HaarTransform {
public double[][] transform(double[][] input){
return transformRows(transformColumns(input));
}
private double[][] transformColumns(double[][] input){
int size = input.length;
double[][] result = new double[size][size];
for (int j = 0; j < size; j++) {
for (int i = 0; i < size / 2; i++) {
result[i][j] = (input[2*i][j] + input[2*i + 1][j])/2.0;
result[size/2 + i][j] = (input[2*i][j] - input[2*i + 1][j])/2.0;
}
}
return result;
}
private double[][] transformRows(double[][] input){
int size = input.length;
double[][] result = new double[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size / 2; j++) {
result[i][j] = (input[i][2*j] + input[i][2*j + 1])/2.0;
result[i][size/2 + j] = (input[i][2*j] - input[i][2*j + 1])/2.0;
}
}
return result;
}
}
@Brixomatic

This comment has been minimized.

Show comment
Hide comment
@Brixomatic

Brixomatic Jul 24, 2018

Hi, thanks for the great and easy to understand example!
I've forked this Gist and fixed some flaws, maybe you like to have a look:
https://gist.github.com/Brixomatic/e0961c41219a82aeed3a175c1385f9df

Kind regards,
Wanja

Brixomatic commented Jul 24, 2018

Hi, thanks for the great and easy to understand example!
I've forked this Gist and fixed some flaws, maybe you like to have a look:
https://gist.github.com/Brixomatic/e0961c41219a82aeed3a175c1385f9df

Kind regards,
Wanja

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment