我正在尝试应用
[-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<height-1;y++)
for(int x=1;x<width-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;
我一直试图找到这个问题几个小时,但我认为代码没有任何问题......任何见解将不胜感激。提前致谢!
慕森卡
相关分类