Java图像过滤器

时间:2018-11-17 17:16:11

标签: java image filter kernel

简而言之,我尝试制作一个图像过滤器(可以使图像模糊),但是以某种方式将内核应用于sharpen({{0,-1,0},{-1,5,-1},{0,-1,0}})edge({{-1,-1,-1},{-1,8,-1},{-1,-1,-1}})时,我得到的图像看起来很奇怪(图像的边界)增加了1px)。

代码如下:

public Color[][] applyFilter(Color[][] pixels, float[][] filter) {
    Color[][] new_pixels = new Color[pixels.length-2][pixels.length-2];
    double sum_red= 0.0f;
    double sum_green= 0.0f;
    double sum_blue= 0.0f;
    for (int i =1 ; i < pixels.length-1 ; i++){

        for(int j = 1; j < pixels[i].length-1 ; j++){
            int k = i-1;
            int l = j-1;

            sum_red = 0.0f;
            sum_green = 0.0f;
            sum_blue = 0.0f;

            for(int x =0 ; x<filter.length ; x++){

                for(int y =0; y<filter[x].length;y++){

                    double red = pixels[k+x][l+y].getRed();
                    double green = pixels[k+x][l+y].getGreen();
                    double blue = pixels[k+x][l+y].getBlue();

                    double red_new = red * filter[x][y];
                    double green_new = green * filter[x][y];
                    double blue_new = blue * filter[x][y];

                    sum_red += red_new;
                    sum_green += green_new;
                    sum_blue += blue_new;
                }
            }

            if(sum_red > 1.00000000000f){
                sum_red = 1.0f;
            }
            else if(sum_red < 0.000000000000000000000f){
                sum_red = 0.0f;
            }
            if(sum_green > 1.00000000000f){
                 sum_green = 1.0f;
            }
            else if(sum_green < 0.000000000000000000000f){
                sum_green = 0.0f;
            }
            if(sum_blue > 1.00000000000f){
                sum_blue = 1.0f;
            }
            else if (sum_blue < 0.00000000000000000000f){
                sum_blue = 0.0f;
            } 

            Color C = new Color(sum_red,sum_green,sum_blue,1.0);
            pixels[i][j] = C;
        }
    }
}

它返回pixels[][]。 这是输出的图片:outputinput相比 任何评论都对样式很有帮助。
谢谢大家!

0 个答案:

没有答案