Created
December 24, 2019 18:46
-
-
Save mcihad/664bbd86d733b8cd9de9fc67308f2af4 to your computer and use it in GitHub Desktop.
Genel Amaçlı Convolution Filter
This file contains hidden or 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
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
public class ConvolutionFilter { | |
public static int[][][] rgbToGray(int[][][] image) { | |
int w = image.length; | |
int h = image[0].length; | |
int[][][] retImage = new int[w][h][3]; | |
for (int x = 0; x < w; x++) { | |
for (int y = 0; y < h; y++) { | |
int grayColor = (image[x][y][0] + image[x][y][1] + image[x][y][2]) / 3; | |
retImage[x][y][0] = grayColor; | |
retImage[x][y][1] = grayColor; | |
retImage[x][y][2] = grayColor; | |
} | |
} | |
return retImage; | |
} | |
public static int[][][] imageToMatrix(BufferedImage image) { | |
int w = image.getWidth(); | |
int h = image.getHeight(); | |
int[][][] matrix = new int[w][h][3]; | |
Color pixColor = null; | |
for (int x = 0; x < w; x++) { | |
for (int y = 0; y < h; y++) { | |
int color = image.getRGB(x, y); | |
matrix[x][y][0] = color >> 16 & 0xff; | |
matrix[x][y][1] = color >> 8 & 0xff; | |
matrix[x][y][2] = color & 0xff; | |
} | |
} | |
return matrix; | |
} | |
public static BufferedImage matrixToImage(int[][][] matrix) { | |
int w = matrix.length; | |
int h = matrix[0].length; | |
BufferedImage im = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); | |
Color pixelColor = null; | |
for (int x = 0; x < w; x++) { | |
for (int y = 0; y < h; y++) { | |
int color = matrix[x][y][0] << 16 | matrix[x][y][1] << 8 | matrix[x][y][2]; | |
im.setRGB(x, y, color); | |
} | |
} | |
return im; | |
} | |
public static int[][][] processConvolution(int[][][] im, double[][] matrix, double factor, double bias) { | |
int w = im.length; | |
int h = im[0].length; | |
if (factor == 0) { | |
factor = 1.0; | |
} | |
int offset = (matrix.length - 1) / 2; | |
int[][][] retImage = new int[w][h][3]; | |
for (int x = offset; x < (w - offset); x++) { | |
for (int y = offset; y < (h - offset); y++) { | |
int r = 0; | |
int g = 0; | |
int b = 0; | |
for (int m = -offset; m <= offset; m++) { | |
for (int n = -offset; n <= offset; n++) { | |
double val = matrix[m + offset][n + offset]; | |
r += im[x + m][y + n][0] * val; | |
g += im[x + m][y + n][1] * val; | |
b += im[x + m][y + n][2] * val; | |
} | |
} | |
r = (int) (r * factor + bias); | |
g = (int) (g * factor + bias); | |
b = (int) (b * factor + bias); | |
if (r > 255) r = 255; | |
if (r < 0) r = 0; | |
if (g > 255) g = 255; | |
if (g < 0) g = 0; | |
if (b > 255) b = 255; | |
if (b < 0) b = 0; | |
retImage[x][y][0] = r; | |
retImage[x][y][1] = g; | |
retImage[x][y][2] = b; | |
} | |
} | |
return retImage; | |
} | |
public static int[][][] processConvolutionTwoMatrix(int[][][] im, double[][] matrix1, double[][] matrix2) { | |
int w = im.length; | |
int h = im[0].length; | |
int offset = (matrix1.length - 1) / 2; | |
int[][][] retImage = new int[w][h][3]; | |
for (int x = offset; x < (w - offset); x++) { | |
for (int y = offset; y < (h - offset); y++) { | |
int rx = 0; | |
int gx = 0; | |
int bx = 0; | |
int ry = 0; | |
int gy = 0; | |
int by = 0; | |
for (int m = -offset; m <= offset; m++) { | |
for (int n = -offset; n <= offset; n++) { | |
double valx = matrix1[m + offset][n + offset]; | |
rx += im[x + m][y + n][0] * valx; | |
gx += im[x + m][y + n][1] * valx; | |
bx += im[x + m][y + n][2] * valx; | |
double valy = matrix2[m + offset][n + offset]; | |
ry += im[x + m][y + n][0] * valy; | |
gy += im[x + m][y + n][1] * valy; | |
by += im[x + m][y + n][2] * valy; | |
} | |
} | |
int r = (int) Math.sqrt(rx * rx + ry * ry); | |
int g = (int) Math.sqrt(gx * gx + gy * gy); | |
int b = (int) Math.sqrt(bx * bx + by * by); | |
if (r > 255) r = 255; | |
if (g > 255) g = 255; | |
if (b > 255) b = 255; | |
retImage[x][y][0] = r; | |
retImage[x][y][1] = g; | |
retImage[x][y][2] = b; | |
} | |
} | |
return retImage; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment