Last active
February 19, 2018 23:37
-
-
Save omaraflak/905705d759d839b8016730eff884ca44 to your computer and use it in GitHub Desktop.
Image convolution with kernel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Tool { | |
static double[][][] loadImage(String path){ | |
BufferedImage image; | |
try { | |
image = ImageIO.read(new File(path)); | |
double[][][] imageMatrix = new double[3][image.getHeight()][image.getWidth()]; | |
int color, red, green, blue; | |
for (int i=0 ; i<image.getHeight() ; i++){ | |
for (int j=0 ; j<image.getWidth() ; j++){ | |
color = image.getRGB(j, i); | |
red = (color & 0x00ff0000) >> 16; | |
green = (color & 0x0000ff00) >> 8; | |
blue = color & 0x000000ff; | |
imageMatrix[0][i][j] = red; | |
imageMatrix[1][i][j] = green; | |
imageMatrix[2][i][j] = blue; | |
} | |
} | |
return imageMatrix; | |
} catch (IOException e) { | |
return null; | |
} | |
} | |
static double[][][] applyFilter(double[][][] image, double[][] filter){ | |
int height = image[0].length; | |
int width = image[0][0].length; | |
int filterHeight = filter.length; | |
int filterWidth = filter[0].length; | |
int newImageHeight = height-filterHeight+1; | |
int newImageWidth = width-filterWidth+1; | |
double[][][] newImage = new double[3][newImageHeight][newImageWidth]; | |
int d,i,j,h,w; | |
for (d=0 ; d<3 ; d++) { | |
for (i=0 ; i<newImageHeight ; i++) { | |
for (j=0 ; j<newImageWidth ; j++) { | |
for (h=i ; h<i+filterHeight ; h++) | |
{ | |
for (w=j ; w<j+filterWidth ; w++) | |
{ | |
newImage[d][i][j] += image[d][h][w]*filter[h-i][w-j]; | |
} | |
} | |
} | |
} | |
} | |
return newImage; | |
} | |
static boolean saveImage(double[][][] image, String name){ | |
int height = image[0].length; | |
int width = image[0][0].length; | |
int rgb; | |
BufferedImage imageToSave = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | |
for (int y=0 ; y<height ; y++) { | |
for (int x=0 ; x<width ; x++) { | |
rgb = (int) image[0][y][x]; | |
rgb = (rgb << 8) + (int) image[1][y][x]; | |
rgb = (rgb << 8) + (int) image[2][y][x]; | |
imageToSave.setRGB(x, y, rgb); | |
} | |
} | |
String extension = name.substring(name.lastIndexOf(".")+1); | |
try { | |
ImageIO.write(imageToSave, extension, new File(name)); | |
return true; | |
} catch (IOException e) { | |
return false; | |
} | |
} | |
static double[][] getGaussian(int height, int width, double sigma) | |
{ | |
double[][] kernel = new double[height][width]; | |
double sum=0.0; | |
int i,j; | |
for (i=0 ; i<height ; i++) { | |
for (j=0 ; j<width ; j++) { | |
kernel[i][j] = Math.exp(-(i*i+j*j)/(2*sigma*sigma))/(2*Math.PI*sigma*sigma); | |
sum += kernel[i][j]; | |
} | |
} | |
for (i=0 ; i<height ; i++) { | |
for (j=0 ; j<width ; j++) { | |
kernel[i][j] /= sum; | |
} | |
} | |
return kernel; | |
} | |
} | |
/* | |
... how to use | |
*/ | |
double[][] filter = Tool.getGaussian(13,13, 10); | |
double[][][] image = Tool.loadImage("image.png"); | |
double[][][] newImage = Tool.applyFilter(image, filter); | |
Tool.saveImage(newImage, "newImage.png"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment