Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中为BufferredImage添加模糊效果_Java_Image Processing - Fatal编程技术网

在Java中为BufferredImage添加模糊效果

在Java中为BufferredImage添加模糊效果,java,image-processing,Java,Image Processing,我正在写一个图像处理项目。 除了模糊效果,一切都完成了。 常见的简单算法是: 以像素和像素周围的8个像素为例 平均所有9个像素的RGB值,并将其粘贴到当前像素位置 对每个像素重复此操作 下面是我为添加模糊效果而实现的 BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), img.getType()); int height = img.getHeight();

我正在写一个图像处理项目。 除了模糊效果,一切都完成了。 常见的简单算法是:

  • 以像素和像素周围的8个像素为例
  • 平均所有9个像素的RGB值,并将其粘贴到当前像素位置
  • 对每个像素重复此操作
  • 下面是我为添加模糊效果而实现的

    BufferedImage result = new BufferedImage(img.getWidth(),
                img.getHeight(), img.getType());
    
        int height = img.getHeight();
        int width = img.getWidth();
    
        for (int x = 0; x < width; x++)
            for (int y = 0; y < height; y++) {
    
                int pixel1 = (x == 0 || y == 0) ? 0 : img.getRGB(x - 1, y - 1);
                int pixel2 = (y == 0) ? 0 : img.getRGB(x, y - 1);
                int pixel3 = (y == 0 || x >= width-1) ? 0 : img.getRGB(x + 1, y - 1);
                int pixel4 = (x == 0) ? 0 :img.getRGB(x - 1, y);
                int pixel5 = img.getRGB(x, y);
                int pixel6 = (x >= height -1) ? 0 :img.getRGB(x + 1, y);
                int pixel7 = (x == 0 || y >= height -1) ? 0 :img.getRGB(x - 1, y + 1);
                int pixel8 = (y >= height -1) ? 0 :img.getRGB(x, y + 1);
                int pixel9 = (x >= width-1 || y >= height - 1) ? 0 :img.getRGB(x + 1, y + 1);
    
                int newPixel = pixel1 + pixel2 + pixel3 + pixel4 + pixel5
                        + pixel6 + pixel7 + pixel8 + pixel9;
    
                newPixel = newPixel/9;
    
                int redAmount = (newPixel >> 16) & 0xff;
                int greenAmount = (newPixel >> 8) & 0xff;
                int blueAmount = (newPixel >> 0) & 0xff;
    
                newPixel = (redAmount<< 16) | (greenAmount << 8) | blueAmount ;
                result.setRGB(x, y, newPixel);
            }
    
    BuffereImage结果=新的BuffereImage(img.getWidth(),
    img.getHeight(),img.getType();
    int height=img.getHeight();
    int width=img.getWidth();
    对于(int x=0;x=width-1)?0:img.getRGB(x+1,y-1);
    int pixel4=(x==0)?0:img.getRGB(x-1,y);
    int pixel5=img.getRGB(x,y);
    int pixel6=(x>=高度-1)?0:img.getRGB(x+1,y);
    int pixel7=(x==0 | | y>=height-1)?0:img.getRGB(x-1,y+1);
    int pixel8=(y>=高度-1)?0:img.getRGB(x,y+1);
    int pixel9=(x>=width-1 | | y>=height-1)?0:img.getRGB(x+1,y+1);
    int newPixel=pixel1+pixel2+pixel3+pixel4+pixel5
    +像素6+像素7+像素8+像素9;
    newPixel=newPixel/9;
    int redAmount=(新像素>>16)&0xff;
    int greenAmount=(新像素>>8)&0xff;
    int blueAmount=(新像素>>0)和0xff;
    
    newPixel=(redAmount您不能像您那样使用原始RGB整数进行计算。这肯定会导致错误的结果。(例如,整数溢出) 您必须自己处理每个颜色组件

    编辑只是给你一个例子:想想计算两个像素00FFFF和0000CC的“平均值”意味着什么


    您实际想要的结果是007FE5,但使用原始整数计算将导致“蓝色”部分转入黄色部分,结果为008065。

    正如我所说,已证明了编写的算法。只是忘了分别计算红色、绿色和蓝色的平均值。 答案是这样的

    BufferedImage result = new BufferedImage(img.getWidth(),
                img.getHeight(), img.getType());
    
        int height = img.getHeight();
        int width = img.getWidth();
    
        for (int x = 1; x < width-1; x++)
            for (int y = 1; y < height-1; y++) {
    
                int redAmount = 0;
                int greenAmount = 0;
                int blueAmount = 0;
    
                int pixel1 = img.getRGB(x - 1, y - 1);
                redAmount += (pixel1 >> 16) & 0xff;
                greenAmount += (pixel1 >> 8) & 0xff;
                blueAmount += (pixel1 >> 0) & 0xff;
    
                int pixel2 = img.getRGB(x, y - 1);
                redAmount += (pixel2 >> 16) & 0xff;
                greenAmount += (pixel2 >> 8) & 0xff;
                blueAmount += (pixel2 >> 0) & 0xff;
    
                int pixel3 = img.getRGB(x + 1, y - 1);
                redAmount += (pixel3 >> 16) & 0xff;
                greenAmount += (pixel3 >> 8) & 0xff;
                blueAmount += (pixel3 >> 0) & 0xff;
    
                int pixel4 = img.getRGB(x - 1, y);
                redAmount += (pixel4 >> 16) & 0xff;
                greenAmount += (pixel4 >> 8) & 0xff;
                blueAmount += (pixel4 >> 0) & 0xff;
    
                int pixel5 = img.getRGB(x, y);
                redAmount += (pixel5 >> 16) & 0xff;
                greenAmount += (pixel5 >> 8) & 0xff;
                blueAmount += (pixel5 >> 0) & 0xff;
    
                int pixel6 = img.getRGB(x + 1, y);
                redAmount += (pixel6 >> 16) & 0xff;
                greenAmount += (pixel6 >> 8) & 0xff;
                blueAmount += (pixel6 >> 0) & 0xff;
    
                int pixel7 = img.getRGB(x - 1, y + 1);
                redAmount += (pixel7 >> 16) & 0xff;
                greenAmount += (pixel7 >> 8) & 0xff;
                blueAmount += (pixel7 >> 0) & 0xff;
    
                int pixel8 = img.getRGB(x, y + 1);
                redAmount += (pixel8 >> 16) & 0xff;
                greenAmount += (pixel8 >> 8) & 0xff;
                blueAmount += (pixel8 >> 0) & 0xff;
    
                int pixel9 = img.getRGB(x + 1, y + 1);
                redAmount += (pixel9 >> 16) & 0xff;
                greenAmount += (pixel9 >> 8) & 0xff;
                blueAmount += (pixel9 >> 0) & 0xff;
    
                redAmount /= 9;
                greenAmount /= 9;
                blueAmount /= 9;
    
                int newPixel = (redAmount << 16) | (greenAmount << 8) | blueAmount;
                result.setRGB(x, y, newPixel);
            }
    
    BuffereImage结果=新的BuffereImage(img.getWidth(),
    img.getHeight(),img.getType();
    int height=img.getHeight();
    int width=img.getWidth();
    对于(int x=1;x>16)&0xff;
    绿量+=(像素1>>8)&0xff;
    blueAmount+=(像素1>>0)和0xff;
    int pixel2=img.getRGB(x,y-1);
    redAmount+=(像素2>>16)&0xff;
    绿量+=(像素2>>8)&0xff;
    blueAmount+=(像素2>>0)和0xff;
    int pixel3=img.getRGB(x+1,y-1);
    redAmount+=(像素3>>16)&0xff;
    绿量+=(像素3>>8)&0xff;
    blueAmount+=(像素3>>0)和0xff;
    int pixel4=img.getRGB(x-1,y);
    redAmount+=(像素4>>16)&0xff;
    绿量+=(像素4>>8)&0xff;
    blueAmount+=(像素4>>0)&0xff;
    int pixel5=img.getRGB(x,y);
    redAmount+=(像素5>>16)&0xff;
    绿量+=(像素5>>8)&0xff;
    blueAmount+=(像素5>>0)和0xff;
    int pixel6=img.getRGB(x+1,y);
    redAmount+=(像素6>>16)&0xff;
    绿量+=(像素6>>8)&0xff;
    blueAmount+=(像素6>>0)和0xff;
    int pixel7=img.getRGB(x-1,y+1);
    redAmount+=(像素7>>16)&0xff;
    绿量+=(像素7>>8)&0xff;
    blueAmount+=(像素7>>0)和0xff;
    int pixel8=img.getRGB(x,y+1);
    redAmount+=(像素8>>16)&0xff;
    绿量+=(像素8>>8)&0xff;
    blueAmount+=(像素8>>0)&0xff;
    int pixel9=img.getRGB(x+1,y+1);
    redAmount+=(像素9>>16)&0xff;
    绿量+=(像素9>>8)&0xff;
    blueAmount+=(像素9>>0)&0xff;
    redAmount/=9;
    绿额/=9;
    蓝量/=9;
    
    int newPixel=(redAmount这是一个仅包含循环的版本:

    BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), img.getType()) ;
    final int H = img.getHeight() - 1 ;
    final int W = img.getWidth() - 1 ;
    
    for (int c=0 ; c < img.getRaster().getNumBands() ; c++) // for all the channels/bands
        for (int x=1 ; x < W ; x++) // For all the image
            for (int y=1; y < H ; y++)
                {
                int newPixel = 0 ;
                for (int i=-1 ; i <= 1 ; i++) // For the neighborhood
                    for (int j=-1 ; j <= 1 ; j++)
                        newPixel += img.getRaster().getSample(x+i, y+j, c) ;
                newPixel = (int)(newPixel/9.0 + 0.5) ;
                result.getRaster().setSample(x, y, c, newPixel) ;
                }
    
    buffereImage结果=新的buffereImage(img.getWidth()、img.getHeight()、img.getType());
    final int H=img.getHeight()-1;
    final int W=img.getWidth()-1;
    对于(int c=0;c对于(int i=-1;我让我计算所有红色、蓝色和绿色的平均值,然后设置。然后看看它是否有效。耶,酷。请接受我的回答,然后结束这个问题。我修改了循环,从1开始到宽度-1/高度-1,这删除了代码中不必要的三元运算符。你能告诉我为什么你在newPixel/9.0中添加了0.5吗?我对循环进行了四舍五入将结果值转换为最接近的整数。如果结果为1.9,则您的方法将其四舍五入为1,使用我的方法将其四舍五入为2。这是从double转换为整数的最常见方法。这也是由Math.round()完成的。我还包括其他7个筛选器。我们学习和实现的是使用getRGB()方法。我喜欢您的was,因为它是非常优化的代码。但我现在必须使用getRGB()。是的,我将实现两个相邻的内部循环。最优化的解决方案是直接访问DataBuffer,然后直接在数组上工作。我不知道为什么getRGB()是强制的,但访问像素值是最糟糕的解决方案。可能是因为它的ju