Next: 5.2 メディアンフィルタ
Up: 5 画像処理
Previous: 5 画像処理
画像にノイズを加えたり,画素の近傍の画素に重みをかけて
足し合わせた画像を作ることを空間フィルタ処理と
いいます.重みの配列をいろいろ変えることによって,
画像を平滑化し,濃淡の違いを強調したりする
ことが可能となる.
表 7:
java.awt.image.MemoryImageSourceクラスのコンストラクタ
|
MemoryImageSource (int w, int h, byte[] pix, int off, int scan) |
|
MemoryImageSource (int w, int h, byte[] pix, int off, int scan,
Hashtable props) |
|
MemoryImageSource (int w, int h, ColorModel cm, byte[] pix, int off, int
scan) |
|
MemoryImageSource (int w, int h, ColorModel cm, byte[] pix, int off, int scan,
Hashtable props) |
Image画像配列を作る方法として,createImageに
MemoryImageSourceクラスのインスタンスを与えて生成する.
// FilterTest.java
import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.awt.image.*;
import java.lang.Math.*;
/*<applet code="FilterTest.class"
width="967" height="747">
<param name="img_" value="aiboakira.jpg">
</applet>*/
public class FilterTest extends Applet {
Image img_;
Image img;
int w, h;
MediaTracker mt;
Button btn_flt = new Button("平滑化 3x3");
Button btn_flt5 = new Button("平滑化 5x5");
Button btn_lap8 = new Button("ラプラシアン 8連結");
Button btn_lap4 = new Button("ラプラシアン 4連結");
Button btn_hfil = new Button("水平方向差分");
Button btn_vfil = new Button("垂直方向差分");
Button btn_noise = new Button("ノイズ付加");
Button btn_reset = new Button("リセット");
public void init(){
mt=new MediaTracker(this);
setBackground(Color.white);
setBackground(Color.white);
img_ = getImage(getDocumentBase(), getParameter("img_"));
mt.addImage(img_,0);
try {
mt.waitForID(0);
} catch(InterruptedException e) {
showStatus(" " + e);
}
w=img_.getWidth(this);
h=img_.getHeight(this);
final int[][] color= new int[h][w];
getColor(img_, color);
System.out.println("w=" + w + " h=" + h);
img=img_;
this.add(btn_flt);
this.add(btn_flt5);
this.add(btn_lap8);
this.add(btn_lap4);
this.add(btn_hfil);
this.add(btn_vfil);
this.add(btn_noise);
this.add(btn_reset);
btn_flt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
img_ =
convolution(color,
new int[][]{{1,1,1},
{1,1,1},
{1,1,1}});
repaint();
getColor(img_, color);
};
});
btn_flt5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
img_ =
convolution(color,
new int[][]{{1,1,1,1,1},
{1,1,1,1,1},
{1,1,1,1,1},
{1,1,1,1,1},
{1,1,1,1,1}});
repaint();
getColor(img_, color);
};
});
btn_lap8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
img_ =
convolution(color,
new int[][]{{-1,-1,-1},
{-1,8,-1},
{-1,-1,-1}});
repaint();
getColor(img_, color);
};
});
btn_lap4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
img_ =
convolution(color,
new int[][]{{ 0,-1, 0},
{-1, 4,-1},
{ 0,-1, 0}});
repaint();
getColor(img_, color);
};
});
btn_hfil.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
img_ =
convolution(color,
new int[][]{{-1,-1,-1},
{ 0, 0, 0},
{ 1, 1, 1}});
repaint();
getColor(img_, color);
};
});
btn_vfil.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
img_ =
convolution(color,
new int[][]{{-1,0, 1},
{-1,0, 1},
{-1,0, 1}});
repaint();
getColor(img_, color);
};
});
btn_noise.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
img_ = noise(color);
repaint();
getColor(img_, color);
}
});
btn_reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
img_ = reset();
repaint();
getColor(img_, color);
}
});
}
public void paint(Graphics g) {
g.drawImage(img_,0,0,this);
}
public void update(Graphics g) {
paint(g);
}
public Image convolution(int color[][],
int weight[][]) {
int h = color.length;
int w = color[0].length;
int wh = weight.length;
int ww = weight[0].length;
int[] pix= new int[w*h];
int v;
for(int i=0;i<h-wh;i++){
for(int j=0;j<w-ww;j++){
int r=0; int g=0; int b=0;
for (int wx=0; wx<ww; wx++)
for (int wy=0; wy<wh; wy++) {
v = color[i+wy][j+wx];
r += ((v >> 16)&0xff)* weight[wy][wx];
g += ((v >> 8) &0xff)* weight[wy][wx];
b += ( v &0xff)* weight[wy][wx];
}
pix[j + i*w]=
0xff000000 |
((r/ww/wh)<<16) |
((g/ww/wh)<<8) |
(b/ww/wh);
}
}
return createImage(new
MemoryImageSource(w,h,pix,0,w));
}
public Image reset() {
return img;
}
public Image noise(int color[][]) {
int h = color.length;
int w = color[0].length;
int[] pix2= new int[w*h];
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
pix2[j+i*w] = color[i][j];
if(Math.random()<0.05){
pix2[j+i*w]= 0xff000000 |
(((byte)(Math.random()*255))<<16)|
(((byte)(Math.random()*255))<<8)|
(byte)(Math.random()*255);
}
}
}
return createImage(new
MemoryImageSource(w,h,pix2,0,w));
}
void getColor(Image img_, int color[][]) {
int h = color.length;
int w = color[0].length;
int[] pix= new int[w*h];
PixelGrabber pg=
new PixelGrabber(img_,0,0,w,h,pix,0,w);
try{pg.grabPixels();}catch(Exception e){}
for(int y=0;y<h;y++){
for(int x=0;x<w;x++)
color[y][x] = pix[x+y*w];
}
}
}
Next: 5.2 メディアンフィルタ
Up: 5 画像処理
Previous: 5 画像処理
generated through LaTeX2HTML. M.Inaba 平成18年5月7日