## 算法

### 数据初始化

``````I[x,y] == 1 and (
I[x,y]!= I[x-1,y] or
I[x,y]!= I[x+1,y] or
I[x,y]!= I[x,y-1] or
I[x,y]!= I[x,y+1] )``````

### 两次遍历

``````// 正向遍历
for y in 1 to height {
for x in 1 to width {
curr_d = d[x,y];
curr_p = p[x,y];
// 检查左上像素
if d[x-1,y-1] + 1.414 < curr_d {
curr_p = p[x-1,y-1];
curr_d = distance((x,y), curr_p);
}
// 检查正上方像素
if d[x,y-1] + 1 < curr_d {
curr_p = p[x,y-1];
curr_d = distance((x,y), curr_p);
}
// 检查右上像素
if d[x+1,y-1] + 1.414 < curr_d {
curr_p = p[x+1,y-1];
curr_d = distance((x,y), curr_p);
}
// 检查正左方像素
if d[x-1,y] + 1 < curr_d {
curr_p = p[x-1,y];
curr_d = distance((x,y), curr_p);
}
d[x,y] = curr_d;
p[x,y] = curr_p;
}
}

// 反向遍历
for y in height to 1 {
for x in width to 1 {
curr_d = d[x,y];
curr_p = p[x,y];
// 检查左下像素
if d[x-1,y+1] + 1.414 < curr_d {
curr_p = p[x-1,y+1];
curr_d = distance((x,y), curr_p);
}
// 检查正下方像素
if d[x,y+1] + 1 < curr_d {
curr_p = p[x,y+1];
curr_d = distance((x,y), curr_p);
}
// 检查右下像素
if d[x+1,y+1] + 1.414 < curr_d {
curr_p = p[x+1,y+1];
curr_d = distance((x,y), curr_p);
}
// 检查正右方像素
if d[x+1,y] + 1 < curr_d {
curr_p = p[x+1,y];
curr_d = distance((x,y), curr_p);
}
d[x,y] = curr_d;
p[x,y] = curr_p;
}
}``````

41 声望
1 粉丝
0 条评论