影象處理之Zhang Suen細化演算法
在二值影象處理特別是OCR識別與匹配中,都要通過對字元進行細化以便獲得影象的骨架,通過zhang-suen細化演算法獲得影象,作為影象的特徵之一,常用來作為識別或者模式匹配。
一:演算法介紹
Zhang-Suen細化演算法通常是一個迭代演算法,整個迭代過程分為兩步:
Step One:迴圈所有前景畫素點,對符合如下條件的畫素點標記為刪除:
1. 2 <= N(p1) <=6
2. S(P1) = 1
3. P2 * P4 * P6 = 0
4. P4 * P6 * P8 = 0
其中N(p1)表示跟P1相鄰的8個畫素點中,為前景畫素點的個數
S(P1)表示從P2 ~ P9 ~ P2畫素中出現0~1的累計次數,其中0表示背景,1表示前景
完整的P1 ~P9的畫素位置與舉例如下:
其中 N(p1) = 4, S(P1) = 3, P2*P4*P6=0*0*0=0, P4*P6*P8=0*0*1=0, 不符合條件,無需標記為刪除。
Step Two:跟Step One很類似,條件1、2完全一致,只是條件3、4稍微不同,滿足如下條件的畫素P1則標記為刪除,條件如下:
1. 2 <= N(p1) <=6
2. S(P1) = 1
3. P2 * P4 * P8 = 0
4. P2 * P6 * P8 = 0
迴圈上述兩步驟,直到兩步中都沒有畫素被標記為刪除為止,輸出的結果即為二值影象細化後的骨架。
二:程式碼實現步驟
1. 二值化輸入影象,初始化影象畫素對應的標記對映陣列
BufferedImage binaryImage = super.process(image); int width = binaryImage.getWidth(); int height = binaryImage.getHeight(); int[] pixels = new int[width*height]; int[] flagmap = new int[width*height]; getRGB(binaryImage, 0, 0, width, height, pixels); Arrays.fill(flagmap, 0);
2. 迭代細化演算法(Zhang-Suen)
a. Step One
private boolean step1Scan(int[] input, int[] flagmap, int width, int height) {
boolean stop = true;
int bc = 255 - fcolor;
int p1=0, p2=0, p3=0;
int p4=0, p5=0, p6=0;
int p7=0, p8=0, p9=0;
int offset = 0;
for(int row=1; row<height-1; row++) {
offset = row*width;
for(int col=1; col<width-1; col++) {
p1 = (input[offset+col]>>16)&0xff;
if(p1 == bc) continue;
p2 = (input[offset-width+col]>>16)&0xff;
p3 = (input[offset-width+col+1]>>16)&0xff;
p4 = (input[offset+col+1]>>16)&0xff;
p5 = (input[offset+width+col+1]>>16)&0xff;
p6 = (input[offset+width+col]>>16)&0xff;
p7 = (input[offset+width+col-1]>>16)&0xff;
p8 = (input[offset+col-1]>>16)&0xff;
p9 = (input[offset-width+col-1]>>16)&0xff;
// match 1 - 前景畫素 0 - 背景畫素
p1 = (p1 == fcolor) ? 1 : 0;
p2 = (p2 == fcolor) ? 1 : 0;
p3 = (p3 == fcolor) ? 1 : 0;
p4 = (p4 == fcolor) ? 1 : 0;
p5 = (p5 == fcolor) ? 1 : 0;
p6 = (p6 == fcolor) ? 1 : 0;
p7 = (p7 == fcolor) ? 1 : 0;
p8 = (p8 == fcolor) ? 1 : 0;
p9 = (p9 == fcolor) ? 1 : 0;
int con1 = p2+p3+p4+p5+p6+p7+p8+p9;
String sequence = "" + String.valueOf(p2) + String.valueOf(p3) + String.valueOf(p4) + String.valueOf(p5) +
String.valueOf(p6) + String.valueOf(p7) + String.valueOf(p8) + String.valueOf(p9) + String.valueOf(p2);
int index1 = sequence.indexOf("01");
int index2 = sequence.lastIndexOf("01");
int con3 = p2*p4*p6;
int con4 = p4*p6*p8;
if((con1 >= 2 && con1 <= 6) && (index1 == index2) && con3 == 0 && con4 == 0) {
flagmap[offset+col] = 1;
stop = false;
}
}
}
return stop;
}
b. Step Two private boolean step2Scan(int[] input, int[] flagmap, int width, int height) {
boolean stop = true;
int bc = 255 - fcolor;
int p1=0, p2=0, p3=0;
int p4=0, p5=0, p6=0;
int p7=0, p8=0, p9=0;
int offset = 0;
for(int row=1; row<height-1; row++) {
offset = row*width;
for(int col=1; col<width-1; col++) {
p1 = (input[offset+col]>>16)&0xff;
if(p1 == bc) continue;
p2 = (input[offset-width+col]>>16)&0xff;
p3 = (input[offset-width+col+1]>>16)&0xff;
p4 = (input[offset+col+1]>>16)&0xff;
p5 = (input[offset+width+col+1]>>16)&0xff;
p6 = (input[offset+width+col]>>16)&0xff;
p7 = (input[offset+width+col-1]>>16)&0xff;
p8 = (input[offset+col-1]>>16)&0xff;
p9 = (input[offset-width+col-1]>>16)&0xff;
// match 1 - 前景畫素 0 - 背景畫素
p1 = (p1 == fcolor) ? 1 : 0;
p2 = (p2 == fcolor) ? 1 : 0;
p3 = (p3 == fcolor) ? 1 : 0;
p4 = (p4 == fcolor) ? 1 : 0;
p5 = (p5 == fcolor) ? 1 : 0;
p6 = (p6 == fcolor) ? 1 : 0;
p7 = (p7 == fcolor) ? 1 : 0;
p8 = (p8 == fcolor) ? 1 : 0;
p9 = (p9 == fcolor) ? 1 : 0;
int con1 = p2+p3+p4+p5+p6+p7+p8+p9;
String sequence = "" + String.valueOf(p2) + String.valueOf(p3) + String.valueOf(p4) + String.valueOf(p5) +
String.valueOf(p6) + String.valueOf(p7) + String.valueOf(p8) + String.valueOf(p9) + String.valueOf(p2);
int index1 = sequence.indexOf("01");
int index2 = sequence.lastIndexOf("01");
int con3 = p2*p4*p8;
int con4 = p2*p6*p8;
if((con1 >= 2 && con1 <= 6) && (index1 == index2) && con3 == 0 && con4 == 0) {
flagmap[offset+col] = 1;
stop = false;
}
}
}
return stop;
}
c. 檢查如果上述兩部沒有任何畫素被標記,則停止迭代,否則繼續執行a, b3. 返回細化後的影象,並顯示
三:執行效果
四:完整的Zhang-suen演算法程式碼實現:
import java.awt.image.BufferedImage;
import java.util.Arrays;
public class ZhangSuenThinFilter extends BinaryFilter {
private int fcolor;
public ZhangSuenThinFilter() {
fcolor = 0;
}
public int getFcolor() {
return fcolor;
}
public void setFcolor(int fcolor) {
this.fcolor = fcolor;
}
@Override
public BufferedImage process(BufferedImage image) {
BufferedImage binaryImage = super.process(image);
int width = binaryImage.getWidth();
int height = binaryImage.getHeight();
int[] pixels = new int[width*height];
int[] flagmap = new int[width*height];
getRGB(binaryImage, 0, 0, width, height, pixels);
Arrays.fill(flagmap, 0);
// 距離變化
boolean stop = false;
while(!stop) {
// step one
boolean s1 = step1Scan(pixels, flagmap, width, height);
deletewithFlag(pixels, flagmap);
Arrays.fill(flagmap, 0);
// step two
boolean s2 = step2Scan(pixels, flagmap, width, height);
deletewithFlag(pixels, flagmap);
Arrays.fill(flagmap, 0);
if(s1 && s2) {
stop = true;
}
}
// 結果
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
setRGB(bi, 0, 0, width, height, pixels);
return bi;
}
private void deletewithFlag(int[] pixels, int[] flagmap) {
int bc = 255 - fcolor;
for(int i=0; i<pixels.length; i++) {
if(flagmap[i] == 1) {
pixels[i] = (0xff << 24) | ((bc&0xff) << 16) | ((bc&0xff) << 8) | (bc&0xff);
}
}
}
private boolean step1Scan(int[] input, int[] flagmap, int width, int height) {
boolean stop = true;
int bc = 255 - fcolor;
int p1=0, p2=0, p3=0;
int p4=0, p5=0, p6=0;
int p7=0, p8=0, p9=0;
int offset = 0;
for(int row=1; row<height-1; row++) {
offset = row*width;
for(int col=1; col<width-1; col++) {
p1 = (input[offset+col]>>16)&0xff;
if(p1 == bc) continue;
p2 = (input[offset-width+col]>>16)&0xff;
p3 = (input[offset-width+col+1]>>16)&0xff;
p4 = (input[offset+col+1]>>16)&0xff;
p5 = (input[offset+width+col+1]>>16)&0xff;
p6 = (input[offset+width+col]>>16)&0xff;
p7 = (input[offset+width+col-1]>>16)&0xff;
p8 = (input[offset+col-1]>>16)&0xff;
p9 = (input[offset-width+col-1]>>16)&0xff;
// match 1 - 前景畫素 0 - 背景畫素
p1 = (p1 == fcolor) ? 1 : 0;
p2 = (p2 == fcolor) ? 1 : 0;
p3 = (p3 == fcolor) ? 1 : 0;
p4 = (p4 == fcolor) ? 1 : 0;
p5 = (p5 == fcolor) ? 1 : 0;
p6 = (p6 == fcolor) ? 1 : 0;
p7 = (p7 == fcolor) ? 1 : 0;
p8 = (p8 == fcolor) ? 1 : 0;
p9 = (p9 == fcolor) ? 1 : 0;
int con1 = p2+p3+p4+p5+p6+p7+p8+p9;
String sequence = "" + String.valueOf(p2) + String.valueOf(p3) + String.valueOf(p4) + String.valueOf(p5) +
String.valueOf(p6) + String.valueOf(p7) + String.valueOf(p8) + String.valueOf(p9) + String.valueOf(p2);
int index1 = sequence.indexOf("01");
int index2 = sequence.lastIndexOf("01");
int con3 = p2*p4*p6;
int con4 = p4*p6*p8;
if((con1 >= 2 && con1 <= 6) && (index1 == index2) && con3 == 0 && con4 == 0) {
flagmap[offset+col] = 1;
stop = false;
}
}
}
return stop;
}
private boolean step2Scan(int[] input, int[] flagmap, int width, int height) {
boolean stop = true;
int bc = 255 - fcolor;
int p1=0, p2=0, p3=0;
int p4=0, p5=0, p6=0;
int p7=0, p8=0, p9=0;
int offset = 0;
for(int row=1; row<height-1; row++) {
offset = row*width;
for(int col=1; col<width-1; col++) {
p1 = (input[offset+col]>>16)&0xff;
if(p1 == bc) continue;
p2 = (input[offset-width+col]>>16)&0xff;
p3 = (input[offset-width+col+1]>>16)&0xff;
p4 = (input[offset+col+1]>>16)&0xff;
p5 = (input[offset+width+col+1]>>16)&0xff;
p6 = (input[offset+width+col]>>16)&0xff;
p7 = (input[offset+width+col-1]>>16)&0xff;
p8 = (input[offset+col-1]>>16)&0xff;
p9 = (input[offset-width+col-1]>>16)&0xff;
// match 1 - 前景畫素 0 - 背景畫素
p1 = (p1 == fcolor) ? 1 : 0;
p2 = (p2 == fcolor) ? 1 : 0;
p3 = (p3 == fcolor) ? 1 : 0;
p4 = (p4 == fcolor) ? 1 : 0;
p5 = (p5 == fcolor) ? 1 : 0;
p6 = (p6 == fcolor) ? 1 : 0;
p7 = (p7 == fcolor) ? 1 : 0;
p8 = (p8 == fcolor) ? 1 : 0;
p9 = (p9 == fcolor) ? 1 : 0;
int con1 = p2+p3+p4+p5+p6+p7+p8+p9;
String sequence = "" + String.valueOf(p2) + String.valueOf(p3) + String.valueOf(p4) + String.valueOf(p5) +
String.valueOf(p6) + String.valueOf(p7) + String.valueOf(p8) + String.valueOf(p9) + String.valueOf(p2);
int index1 = sequence.indexOf("01");
int index2 = sequence.lastIndexOf("01");
int con3 = p2*p4*p8;
int con4 = p2*p6*p8;
if((con1 >= 2 && con1 <= 6) && (index1 == index2) && con3 == 0 && con4 == 0) {
flagmap[offset+col] = 1;
stop = false;
}
}
}
return stop;
}
}
覺得不錯請支援一下!