[-1][-1][-1]
[-1][8][-1]
[-1][-1][-1]
3*3拉普拉斯过滤到灰度版的著名照片(png扩展)在这里。
BufferedImage
类处理图像。这是拉普拉斯滤波法。
private BufferedImage measureContrast(BufferedImage image) {
BufferedImage grayScale = createGrayscaleImage(image);
BufferedImage copy = copyImage(grayScale);
int width = image.getWidth();
int height = image.getHeight();
int sum=0;
int a;
//3*3 Laplacian filter (-1,-1,-1), (-1,8,-1), (-1,-1,-1)
for(int y=1;y
for(int x=1;x
sum = (-1*(grayScale.getRGB(x-1, y-1)&0xff)) + (-1*(grayScale.getRGB(x, y-1)&0xff)) + (-1*(grayScale.getRGB(x+1, y-1)&0xff))
+ (-1*(grayScale.getRGB(x-1, y)&0xff)) + (8*(grayScale.getRGB(x,y)&0xff)) + (-1*(grayScale.getRGB(x+1, y)&0xff)) +
(-1*(grayScale.getRGB(x-1, y+1)&0xff)) + (-1*(grayScale.getRGB(x, y+1)&0xff)) + (-1*(grayScale.getRGB(x+1, y+1)&0xff));
a = ((grayScale.getRGB(x, y)>>24)&0xff);
copy.setRGB(x, y, ((a<<24)|(sum<<16)|(sum<<8)|(sum)));
}
return copy;
如果我运行代码,结果会是这样的
我可以保证灰度版本是正确的,因为当我在应用过滤器之前运行以下代码时,输出会给出一个完美的灰度图像。
private BufferedImage measureContrast(BufferedImage image) {
BufferedImage grayScale = createGrayscaleImage(image);
BufferedImage copy = copyImage(grayScale); /*rest of the code is commented*/
return copy;
我已经试着找出这个问题几个小时了,但我不认为代码有什么问题。。。任何洞察都将不胜感激。提前谢谢!
BufferedImage copyImage(BufferedImage bi) {
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.copyData(null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
而且,结果图像是这样打印的
BufferedImage Contrast = measureContrast(image);
//write image
try {
ImageIO.write(Contrast, "png", new File(outputPath));
System.out.println("Printing complete");
}catch(IOException e) {
System.out.println("File Printing Error: "+e);
}
以防万一,这里是灰度图像的生成方法。
private BufferedImage createGrayscaleImage(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage copy = copyImage(image);
int p=0, a=0, r=0, g=0, b=0, avg=0;
for(int y=0;y
for(int x=0;x
p=image.getRGB(x, y);
a=(p>>24)&0xff;
r=(p>>16)&0xff;
g=(p>>8)&0xff;
b=p&0xff;
avg = (r+g+b)/3;
p = (a<<24) | (avg<<16) | (avg<<8) | avg;
copy.setRGB(x, y, p);
}
return copy;
}