import java.awt.image.BufferedImage; import java.io.BufferedWriter; import java.io.File;
import java.io.FileWriter; import java.io.IOException;
import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Image {
int pixel[][];// 像素
File fin = new File(\BufferedImage bi;
File fout = new File(\int width; int height; int minx; int miny;
void init() throws IOException { bi = ImageIO.read(fin); width = bi.getWidth(); height = bi.getHeight(); minx = bi.getMinX(); miny = bi.getMinY(); System.out.println(\ System.out.println(\ pixel = new int[width][height]; for (int i = minx; i < width; i++) { for (int j = miny; j < height; j++) { }
}
}
pixel[i][j] = (bi.getRGB(i, j) == -1) ? 1 : 0;// -1为白色
void ZhangThinning() throws IOException { init(); int neighbor[] = new int[8];// 8领域 int markNum = 0;
int mark[][] = new int[width][height]; boolean loop = true; while (loop) { loop = false; markNum = 0; for (int y = miny + 1; y + 1 < height; y++) { for (int x = minx + 1; x + 1 < width; x++) { if (pixel[x][y] == 0) continue; neighbor[0] = pixel[x + 1][y];
neighbor[1] = pixel[x + 1][y - 1]; neighbor[2] = pixel[x][y - 1]; neighbor[3] = pixel[x - 1][y - 1]; neighbor[4] = pixel[x - 1][y]; neighbor[5] = pixel[x - 1][y + 1]; neighbor[6] = pixel[x][y + 1]; neighbor[7] = pixel[x + 1][y + 1]; // 条件2:2<=N(p)<=6
int np = neighbor[0] + neighbor[1] + neighbor[2] + neighbor[3] + neighbor[4] + neighbor[5] + neighbor[6] + neighbor[7]; if (np < 2 || np > 6) continue; // 条件3:S(p)=1 int sp = 0;
for (int i = 1; i < 8; i++) {
if (neighbor[i] - neighbor[i - 1] == 1)
}
}
sp++;
if (neighbor[0] - neighbor[7] == 1)
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[x][y] = 1; markNum++; loop = true;
}
if (markNum > 0) {
for (int y = miny; y + 1 < height; y++) { for (int x = minx; x + 1 < width; x++) { //删除标记点 if (mark[x][y] == 1)
{
}
}
}
}
pixel[x][y] = 0; mark[x][y] = 0; bi.setRGB(x, y, 0);
markNum = 0;
for (int y = miny + 1; y + 1 < height; y++) { for (int x = minx + 1; x + 1 < width; x++) { if (pixel[x][y] == 0) continue; neighbor[0] = pixel[x + 1][y];
neighbor[1] = pixel[x + 1][y - 1]; neighbor[2] = pixel[x][y - 1]; neighbor[3] = pixel[x - 1][y - 1]; neighbor[4] = pixel[x - 1][y]; neighbor[5] = pixel[x - 1][y + 1]; neighbor[6] = pixel[x][y + 1]; neighbor[7] = pixel[x + 1][y + 1]; // 条件2:2<=N(p)<=6
int np = neighbor[0] + neighbor[1] + neighbor[2] + neighbor[3] + neighbor[4] + neighbor[5] + neighbor[6] + neighbor[7]; if (np < 2 || np > 6) continue; // 条件3:S(p)=1 int sp = 0;
for (int i = 1; i < 8; i++)
}
{ }
if (neighbor[0] - neighbor[7] == 1)
sp++;
if (neighbor[i] - neighbor[i - 1] == 1)
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[x][y] = 1; markNum++; loop = true;
}
if (markNum > 0) { for (int y = miny; y < height; y++) { for (int x = minx; x < width; x++) { //删除标记点
}
}
if (mark[x][y] == 1) { }
pixel[x][y] = 0; bi.setRGB(x, y, 0);
} } } }
ImageIO.write(bi, \
void show() { JFrame frame=new JFrame(\细化\ JPanel panel=new JPanel(); ImageIcon img1 = new ImageIcon(\ JLabel label1=new JLabel(\细化前\ panel.add(label1); ImageIcon img2 = new ImageIcon( \ JLabel label2=new JLabel(\细化后\ panel.add(label2); frame.setSize(700, 700); frame.add(panel); frame.setVisible(true); } /**
* @param args
* @throws IOException */
public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Image temp= new Image(); temp.ZhangThinning(); temp.show(); }