猿问

拉普拉斯滤波器产生奇怪的结果(Java)

我正在尝试应用


[-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;

如果我运行该代码,结果是这样的

http://img.mukewang.com/617a670f0001a63702890319.jpg

这显然是错误的。图像中突然出现奇怪的粗线。


我可以确保灰度版本是正确的,因为当我在应用过滤器之前运行以下代码时,输出会提供完美的灰度图像。


private BufferedImage measureContrast(BufferedImage image) {

        BufferedImage grayScale = createGrayscaleImage(image);

        BufferedImage copy = copyImage(grayScale); /*rest of the code is commented*/

return copy;

http://img3.mukewang.com/617a671b000113ce02800317.jpg

我一直试图找到这个问题几个小时,但我认为代码没有任何问题......任何见解将不胜感激。提前致谢!



喵喔喔
浏览 128回答 1
1回答

慕森卡

private BufferedImage measureContrast(BufferedImage image) {&nbsp; &nbsp; &nbsp; &nbsp; BufferedImage grayScale = createGrayscaleImage(image);&nbsp; &nbsp; &nbsp; &nbsp; BufferedImage copy = copyImage(grayScale);&nbsp; &nbsp; &nbsp; &nbsp; int width = image.getWidth();&nbsp; &nbsp; &nbsp; &nbsp; int height = image.getHeight();&nbsp; &nbsp; &nbsp; &nbsp; int sum=0;&nbsp; &nbsp; &nbsp; &nbsp; int a;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//3*3 Laplacian filter (-1,-1,-1), (-1,8,-1), (-1,-1,-1)&nbsp; &nbsp; &nbsp; &nbsp; for(int y=1;y<height-1;y++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int x=1;x<width-1;x++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = (-1*(grayScale.getRGB(x-1, y-1)&0xff)) + (-1*(grayScale.getRGB(x, y-1)&0xff)) + (-1*(grayScale.getRGB(x+1, y-1)&0xff))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + (-1*(grayScale.getRGB(x-1, y)&0xff)) + (8*(grayScale.getRGB(x,y)&0xff)) + (-1*(grayScale.getRGB(x+1, y)&0xff)) +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (-1*(grayScale.getRGB(x-1, y+1)&0xff)) + (-1*(grayScale.getRGB(x, y+1)&0xff)) + (-1*(grayScale.getRGB(x+1, y+1)&0xff));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a = ((grayScale.getRGB(x, y)>>24)&0xff);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; copy.setRGB(x, y, ((a<<24)|(sum<<16)|(sum<<8)|(sum)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }return copy;这个方法是错误的。我应该考虑 sum 何时为负值。所以我sum = (sum>0)?sum:0;在嵌套的 for 循环中添加,以确保在这些情况下像素值为 0。
随时随地看视频慕课网APP

相关分类

Java
我要回答