📄 convolve.java
字号:
package imageapp;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
public class Convolve {
public static BufferedImage convolve(BufferedImage srcImage, float kernel[]) {
int width = srcImage.getWidth();
int height = srcImage.getHeight();
int srcRGBs[] = new int[(height+2)*(width+2)];
srcImage.getRGB(0, 0, width, height, srcRGBs, width+3, width+2);
for(int i=1; i<width; i++) {
srcRGBs[i] = srcRGBs[i+width+2];
srcRGBs[i+(height+1)*(width+2)] = srcRGBs[i+height*(width+2)];
}
for(int i=0; i<height+2; i++) {
srcRGBs[i*(width+2)] = srcRGBs[i*(width+2)+1];
srcRGBs[i*(width+2)+width+1] = srcRGBs[i*(width+2)+width];
}
BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
float frgb[] = new float[3];
int rgb[] = new int[3];
int rgbs[][] = new int[9][];
for(int i=0; i<9; i++) rgbs[i] = new int[3];
int matrix[] = new int[9];
for(int j=1; j<=height; j++) {
for(int i=1; i<=width; i++) {
ImageUtil.decodeColor(srcRGBs[(j-1)*(width+2)+i-1], rgbs[0]);
ImageUtil.decodeColor(srcRGBs[(j-1)*(width+2)+i], rgbs[1]);
ImageUtil.decodeColor(srcRGBs[(j-1)*(width+2)+i+1], rgbs[2]);
ImageUtil.decodeColor(srcRGBs[(j)*(width+2)+i-1], rgbs[3]);
ImageUtil.decodeColor(srcRGBs[(j)*(width+2)+i], rgbs[4]);
ImageUtil.decodeColor(srcRGBs[(j)*(width+2)+i+1], rgbs[5]);
ImageUtil.decodeColor(srcRGBs[(j+1)*(width+2)+i-1], rgbs[6]);
ImageUtil.decodeColor(srcRGBs[(j+1)*(width+2)+i], rgbs[7]);
ImageUtil.decodeColor(srcRGBs[(j+1)*(width+2)+i+1], rgbs[8]);
frgb[0] = frgb[1] = frgb[2] = 0;
for(int k=0; k<9; k++) {
frgb[0] += kernel[k]*rgbs[k][0];
frgb[1] += kernel[k]*rgbs[k][1];
frgb[2] += kernel[k]*rgbs[k][2];
}
rgb[0] = (int)frgb[0];
rgb[0] = rgb[0] < 0 ? 0 : rgb[0];
rgb[0] = rgb[0] > 255 ? 255 : rgb[0];
rgb[1] = (int)frgb[1];
rgb[1] = rgb[1] < 0 ? 0 : rgb[1];
rgb[1] = rgb[1] > 255 ? 255 : rgb[1];
rgb[2] = (int)frgb[2];
rgb[2] = rgb[2] < 0 ? 0 : rgb[2];
rgb[2] = rgb[2] > 255 ? 255 : rgb[2];
destImage.setRGB(i-1, j-1, ImageUtil.encodeColor(rgb));
}
}
return destImage;
}
public static BufferedImage laplacian(BufferedImage srcImage) {
float kernel[] = new float[] {0, -1, 0, -1, 4, -1, 0, -1, 0};
return convolve(srcImage, kernel);
}
public static BufferedImage sharp(BufferedImage srcImage) {
float kernel[] = new float[] {-1, -1, -1, -1, 9, -1, -1, -1, -1};
return convolve(srcImage, kernel);
}
public static BufferedImage blur(BufferedImage srcImage) {
float kernel[] = new float[] {1.0f/10, 1.0f/10, 1.0f/10,
1.0f/10, 1.0f/10, 1.0f/10,
1.0f/10, 1.0f/10, 1.0f/10};
return convolve(srcImage, kernel);
}
public static BufferedImage MedianFilt(BufferedImage srcImage) {
int width = srcImage.getWidth();
int height = srcImage.getHeight();
int srcRGBs[] = new int[(height+2)*(width+2)];
srcImage.getRGB(0, 0, width, height, srcRGBs, width+3, width+2);
for(int i=1; i<width; i++) {
srcRGBs[i] = srcRGBs[i+width+2];
srcRGBs[i+(height+1)*(width+2)] = srcRGBs[i+height*(width+2)];
}
for(int i=0; i<height+2; i++) {
srcRGBs[i*(width+2)] = srcRGBs[i*(width+2)+1];
srcRGBs[i*(width+2)+width+1] = srcRGBs[i*(width+2)+width];
}
BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int rgb[] = new int[3];
int rs[] = new int[9];
int gs[] = new int[9];
int bs[] = new int[9];
for(int j=1; j<=height; j++) {
for(int i=1; i<=width; i++) {
ImageUtil.decodeColor(srcRGBs[(j-1)*(width+2)+i-1], rgb);
rs[0] = rgb[0]; gs[0] = rgb[1]; bs[0] = rgb[2];
ImageUtil.decodeColor(srcRGBs[(j-1)*(width+2)+i], rgb);
rs[1] = rgb[0]; gs[1] = rgb[1]; bs[1] = rgb[2];
ImageUtil.decodeColor(srcRGBs[(j-1)*(width+2)+i+1], rgb);
rs[2] = rgb[0]; gs[2] = rgb[1]; bs[2] = rgb[2];
ImageUtil.decodeColor(srcRGBs[(j)*(width+2)+i-1], rgb);
rs[3] = rgb[0]; gs[3] = rgb[1]; bs[3] = rgb[2];
ImageUtil.decodeColor(srcRGBs[(j)*(width+2)+i], rgb);
rs[4] = rgb[0]; gs[4] = rgb[1]; bs[4] = rgb[2];
ImageUtil.decodeColor(srcRGBs[(j)*(width+2)+i+1], rgb);
rs[5] = rgb[0]; gs[5] = rgb[1]; bs[5] = rgb[2];
ImageUtil.decodeColor(srcRGBs[(j+1)*(width+2)+i-1], rgb);
rs[6] = rgb[0]; gs[6] = rgb[1]; bs[6] = rgb[2];
ImageUtil.decodeColor(srcRGBs[(j+1)*(width+2)+i], rgb);
rs[7] = rgb[0]; gs[7] = rgb[1]; bs[7] = rgb[2];
ImageUtil.decodeColor(srcRGBs[(j+1)*(width+2)+i+1], rgb);
rs[8] = rgb[0]; gs[8] = rgb[1]; bs[8] = rgb[2];
Arrays.sort(rs); Arrays.sort(gs); Arrays.sort(bs);
rgb[0] = rs[4]; rgb[1] = gs[4]; rgb[2] = bs[4];
destImage.setRGB(i-1, j-1, ImageUtil.encodeColor(rgb));
}
}
return destImage;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -