画像をボカす

Javaでスムージングをかける方法を示します。

急な変化をなくし、平坦にするのがスムージングです。

画像がやわらかにすることができます。

変化をならすために、まわりのピクセルの平均値をとる処理を行います。

平均値をとるために、こんなクラスを定義すると便利です。

    class Sum
    {
	int red=0;
	int green=0;
	int blue=0;
	Sum()
	{
	}
	Sum(int red,int green,int blue)
	{
	    this.red=red;
	    this.blue=blue;
	    this.green=green;
	}
	int getRed()
	{
	    return red;
	}
	int getBlue()
	{
	    return blue;
	}
	int getGreen()
	{
	    return green;
	}
	void add(int p)
	{
	    red+=p>>16&0xff;
	    green+=p>>8&0xff;
	    blue+=p&0xff;
	}
	void divide(double count)
	{
	    red=(int)(((double)red)/count);
	    green=(int)(((double)green)/count);
	    blue=(int)(((double)blue)/count);
	}
	int getRGB()
	{
	    return (0xff<<24)|red<<16|green<<8|blue;
	}
    }


画像全てのピクセルを処理するメソッドを以下に示します。

    public int[] dither(int[] memory, int w, int h)
    {
	int pixels[]=new int[w*h];
	for(int i=0;i<h;i++)
	    for(int j=0;j<w;j++)
		{		    
		    int red=0;
		    int blue=0;
		    int green=0;
		    Sum color=new Sum();
		    int count=0;
		    
		    for(int x=i-1;i<=i+1;i++)
			for(int y=j-1;y<=j+1;j++)
			    if(0<=x && x<w && 0<=y && y<h)
				{
				    color.add(memory[x*w+y]);
				    count++;
				}

		    color.divide(count);
		    pixels[i*w+j]=color.getRGB();
		}
	return pixels;
    }

inserted by FC2 system