有没有老哥能帮忙解答一下这些题,或是提供一些相关资料给我,万分感谢,实在是不会做。
二维的高斯滤波函数的 Java 代码实现:
public static BufferedImage GLPF(BufferedImage image, int width, int height, double sigma) {
int halfWidth = width/2;
int halfHeight = height/2;
// make gaussian core
double[][] core = new double[width][height];
double sum = 0;
for (int x = 0 - halfWidth; x <= 0 + halfWidth; x++) {
for (int y = 0 - halfHeight; y <= 0 + halfHeight; y++) {
core[halfWidth+x][halfHeight+y] = Math.exp(-(x*x+y*y)/(2*sigma*sigma))/(2*Math.PI*sigma*sigma);
sum += core[halfWidth+x][halfHeight+y];
System.out.println(String.format("(%2d, %2d) %8f", x, y, core[halfWidth+x][halfHeight+y]));
}
}
for (int i=0; i<core.length; i++) {
for (int j=0; j<core[i].length; j++) {
core[i][j] /= sum;
}
}
// get rgb data from image
int rgbWidth = image.getWidth() + width/2*2;
int rgbHeight = image.getHeight() + height/2*2;
int[][] rgbData = new int[rgbWidth][rgbHeight];
for (int x=0; x<rgbData.length; x++) {
for (int y=0; y<rgbData[x].length; y++) {
int a = x < halfWidth ?
halfWidth - x :
x >= rgbWidth - halfWidth ?
rgbWidth*2 - halfWidth*3 - 2 - x :
x - halfWidth;
int b = y < halfHeight ?
halfHeight - y :
y >= rgbHeight - halfHeight ?
rgbHeight*2 - halfHeight*3 - 2 - y:
y - halfHeight;
rgbData[x][y] = image.getRGB(a, b);
}
}
// create window data
BufferedImage out = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
for (int x=0; x<image.getWidth(); x++) {
for (int y=0; y<image.getHeight(); y++) {
int r = gaussianFilter(rgbData, x, y, core, width, height, rgb-> (rgb&0xFF0000)>>16);
int g = gaussianFilter(rgbData, x, y, core, width, height, rgb-> (rgb&0xFF00)>>8);
int b = gaussianFilter(rgbData, x, y, core, width, height, rgb-> rgb&0xFF);
int rgb = (rgbData[x][y] & 0xFF000000) | (r << 16) | (g << 8) | b;
out.setRGB(x, y, rgb);
}
}
return out;
}
private static int gaussianFilter(int[][] rgbData, int x, int y, final double[][] core, int width, int height, Function<Integer, Integer> converter) {
double value = 0;
// fill window
for (int a=0; a < width; a++) {
for (int b=0; b < height; b++) {
value += converter.apply(rgbData[x+a][y+b]) * core[a][b];
}
}
return (int)value;
}