1 膨胀
package binimage.dilation;
import binimage.binary.BinaryFilter;
import java.awt.image.BufferedImage;
public class DilationFilter extends BinaryFilter {
private int fcolor;
public DilationFilter() {
fcolor = 0;
}
@Override
public BufferedImage process(BufferedImage image) {
BufferedImage binImage = super.process(image);
int width = binImage.getWidth();
int height = binImage.getHeight();
int[] pixels = new int[width * height];
int[] output = new int[width * height];
getRGB(binImage, 0, 0, width, height, pixels);
System.arraycopy(pixels, 0, output, 0, pixels.length);
int p1 = 0, p2 = 0, p3 = 0;
int p4 = 0, p5 = 0, p6 = 0;
int p7 = 0, p8 = 0, p9 = 0;
int offset = 0;
for (int row = 1; row < height - 1; row++) {
offset = row * width;
for (int col = 1; col < width - 1; col++) {
p1 = (pixels[offset - width + col - 1] >> 16) & 0xff;
p2 = (pixels[offset - width + col] >> 16) & 0xff;
p3 = (pixels[offset - width + col + 1] >> 16) & 0xff;
p4 = (pixels[offset + col - 1] >> 16) & 0xff;
p5 = (pixels[offset + col] >> 16) & 0xff;
p6 = (pixels[offset + col + 1] >> 16) & 0xff;
p7 = (pixels[offset + width + col - 1] >> 16) & 0xff;
p8 = (pixels[offset + width + col] >> 16) & 0xff;
p9 = (pixels[offset + width + col + 1] >> 16) & 0xff;
int sum = p1 + p2 + p3 + p4 + p6 + p7 + p8 + p9;
int bc = 255 - fcolor;
int total = bc * 8;
if (p5 == bc && sum != total) {
output[offset + col] = (0xff << 24) | ((fcolor & 0xff) << 16) | ((fcolor & 0xff) << 8) | (fcolor & 0xff);
}
}
}
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
setRGB(bi, 0, 0, width, height, output);
return bi;
}
public int getFcolor() {
return fcolor;
}
public void setFcolor(int fcolor) {
this.fcolor = fcolor;
}
}
public void process() {
DilationFilter filter = new DilationFilter();
BufferedImage tempImage = null;
filter.setFcolor(0);
tempImage = filter.process(image);
for (int i = 0; i < 5; i++) {
tempImage = filter.process(tempImage);
}
resultImage = tempImage;
}