# VisualWorksで画像処理 〜バイラテラルフィルタ〜

こちらのページを参考にいたしまして

VisualWorksでバイラテラルフィルタを実装してみました。

```filter: anImage w: w s1: sigma1 s2: sigma2

| newImage |
newImage := anImage copyEmpty.
w + 1to: anImage width - (w + 1)
do:
[:x |
w + 1to: anImage height - (w + 1)
do:
[:y |
| basePxColor checkPxColor newPxColor RedAcm GreenAcm BlueAcm weight weightAcm |
basePxColor := anImage valueAtPoint: x @ y.
weightAcm := 0.
RedAcm := GreenAcm := BlueAcm := 0.
-1 * w to: w
do:
[:n |
-1 * w to: w
do:
[:m |
| dist1 dist2 |
checkPxColor := anImage valueAtPoint: (x + n) @ (y + m).
dist1 := (m * m + (n * n)) sqrt.
dist2 := (   ((basePxColor red - checkPxColor red) raisedTo: 2)
+ ((basePxColor green - checkPxColor green) raisedTo: 2)
+ ((basePxColor blue - checkPxColor blue) raisedTo: 2))
sqrt.
weight := ((-0.5 * (dist1 / sigma1 raisedTo: 2)) exp) * ((-0.5 * (dist2 / sigma2 raisedTo: 2)) exp).
weightAcm := weightAcm + weight.
RedAcm := RedAcm + (checkPxColor red * weight).
GreenAcm := GreenAcm + (checkPxColor green * weight).
BlueAcm := BlueAcm + (checkPxColor blue * weight)]].
newPxColor := ColorValue
red: RedAcm / weightAcm
green: GreenAcm / weightAcm
blue: BlueAcm / weightAcm.
newImage atPoint: x @ y put: (anImage palette indexOfPaintNearest: newPxColor)]].
^newImage
```