细化算法的参考http://blog.sina.com.cn/s/blog_6f611c3001017y5m.html
直接上案例,对手写数字进行细化:
activity:
//二值图像细化
public void remove(View v) {
Bitmap bim=BitmapFactory.decodeResource(getResources(),R.drawable.i);
int w = bim.getWidth();
int h = bim.getHeight();
int[] neighbor = new int[8];
int[] imgBuf = new int[w * h];
// memset(mark,0,w*h);
bim.getPixels(imgBuf, 0, w, 0, 0, w, h);
int[] mark = new int[w * h];
boolean loop = true;
int x, y, k;
int markNum = 0;
while (loop) {
loop = false;
// 第一步
markNum = 0;
for (y = 1; y < h - 1; y++) {
for (x = 1; x < w - 1; x++) {
// 条件1:p必须是前景点
if (imgBuf[y * w + x] == Color.WHITE)
continue;
neighbor[0] = (imgBuf[y * w + x + 1] >> 8 & 0xff);
neighbor[1] = (imgBuf[(y - 1) * w + x + 1] >> 16 & 0xff);
neighbor[2] = (imgBuf[(y - 1) * w + x] >> 16 & 0xff);
neighbor[3] = (imgBuf[(y - 1) * w + x - 1] >> 16 & 0xff);
neighbor[4] = (imgBuf[y * w + x - 1] >> 16 & 0xff);
neighbor[5] = (imgBuf[(y + 1) * w + x - 1] >> 16 & 0xff);
neighbor[6] = (imgBuf[(y + 1) * w + x] >> 16 & 0xff);
neighbor[7] = (imgBuf[(y + 1) * w + x + 1] >> 16 & 0xff);
// 条件2:2<=N(p)<=6
int np = (neighbor[0] + neighbor[1] + neighbor[2]
+ neighbor[3] + neighbor[4] + neighbor[5]
+ neighbor[6] + neighbor[7]) / 255;
if ((8 - np) < 2 || (8 - np) > 6)
continue;
// 条件3:S(p)=1
int sp = 0;
for (int i = 1; i < 8; i++) {
if (neighbor[i] - neighbor[i - 1] == 255)
sp++;
}
if (neighbor[0] - neighbor[7] == 255)
sp++;
if (sp != 1)
continue;
// 条件4:p2*p0*p6=0
if ((neighbor[2] | neighbor[0] | neighbor[6]) == 0)
continue;
// 条件5:p0*p6*p4=0
if ((neighbor[0] | neighbor[6] | neighbor[4]) == 0)
continue;
// 标记删除
mark[w * y + x] = 1;
markNum++;
loop = true;
}
}
// 将标记删除的点置为背景色
if (markNum > 0) {
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
k = y * w + x;
if (mark[k] == 1) {
imgBuf[k] = Color.WHITE;
}
}
}
}
// 第二步
markNum = 0;
for (y = 1; y < h - 1; y++) {
for (x = 1; x < w - 1; x++) {
// 条件1:p必须是前景点
if (imgBuf[y * w + x] == Color.WHITE)
continue;
neighbor[0] = (imgBuf[y * w + x + 1] >> 16 & 0xff);
neighbor[1] = (imgBuf[(y - 1) * w + x + 1] >> 16 & 0xff);
neighbor[2] = (imgBuf[(y - 1) * w + x] >> 16 & 0xff);
neighbor[3] = (imgBuf[(y - 1) * w + x - 1] >> 16 & 0xff);
neighbor[4] = (imgBuf[y * w + x - 1] >> 16 & 0xff);
neighbor[5] = (imgBuf[(y + 1) * w + x - 1] >> 16 & 0xff);
neighbor[6] = (imgBuf[(y + 1) * w + x] >> 16 & 0xff);
neighbor[7] = (imgBuf[(y + 1) * w + x + 1] >> 16 & 0xff);
// 条件2:<=N(p)<=6
int np = (neighbor[0] + neighbor[1] + neighbor[2]
+ neighbor[3] + neighbor[4] + neighbor[5]
+ neighbor[6] + neighbor[7]) / 255;
if ((8 - np) < 2 || (8 - np) > 6)
continue;
// 条件3:S(p)=1
int sp = 0;
for (int i = 1; i < 8; i++) {
if (neighbor[i] - neighbor[i - 1] == 255)
sp++;
}
if (neighbor[0] - neighbor[7] == 255)
sp++;
if (sp != 1)
continue;
// 条件4:p2*p0*p4==0
if ((neighbor[2] | neighbor[0] | neighbor[4]) == 0)
continue;
// 条件5:p2*p6*p4==0
if ((neighbor[2] | neighbor[6] | neighbor[4]) == 0)
continue;
// 标记删除
mark[w * y + x] = 1;
markNum++;
loop = true;
}
}
// 将标记删除的点置为背景色
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
k = y * w + x;
if (mark[k] == 1) {
imgBuf[k] = Color.WHITE;
}
}
}
}
Bitmap bitmap = Bitmap.createBitmap(w, h, Config.RGB_565);
bitmap.setPixels(imgBuf, 0, w, 0, 0, w, h);
destinationImage.setImageBitmap(bitmap);
}
最后得到的图像效果
Zhang并行快速细化算法的实现,不仅要达到细化的目的,并且保证图像轮廓的连通性。