您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 资本运营 > ZHANG快速并行细化算法JAVA实现
importjava.awt.image.BufferedImage;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importjavax.imageio.ImageIO;importjavax.swing.ImageIcon;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.SwingConstants;publicclassImage{intpixel[][];//像素Filefin=newFile(E:/img/before.png);BufferedImagebi;Filefout=newFile(E:/img/after.png);intwidth;intheight;intminx;intminy;voidinit()throwsIOException{bi=ImageIO.read(fin);width=bi.getWidth();height=bi.getHeight();minx=bi.getMinX();miny=bi.getMinY();System.out.println(width=+width+,height=+height+.);System.out.println(minx=+minx+,miniy=+miny+.);pixel=newint[width][height];for(inti=minx;iwidth;i++){for(intj=miny;jheight;j++){pixel[i][j]=(bi.getRGB(i,j)==-1)?1:0;//-1为白色}}}voidZhangThinning()throwsIOException{init();intneighbor[]=newint[8];//8领域intmarkNum=0;intmark[][]=newint[width][height];booleanloop=true;while(loop){loop=false;markNum=0;for(inty=miny+1;y+1height;y++){for(intx=minx+1;x+1width;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)=6intnp=neighbor[0]+neighbor[1]+neighbor[2]+neighbor[3]+neighbor[4]+neighbor[5]+neighbor[6]+neighbor[7];if(np2||np6)continue;//条件3:S(p)=1intsp=0;for(inti=1;i8;i++){if(neighbor[i]-neighbor[i-1]==1)sp++;}if(neighbor[0]-neighbor[7]==1)sp++;if(sp!=1)continue;//条件4:p2*p0*p6=0if(neighbor[2]*neighbor[0]*neighbor[6]!=0)continue;//条件5:p0*p6*p4=0if(neighbor[0]*neighbor[6]*neighbor[4]!=0)continue;//标记删除mark[x][y]=1;markNum++;loop=true;}}if(markNum0){for(inty=miny;y+1height;y++){for(intx=minx;x+1width;x++){//删除标记点if(mark[x][y]==1){pixel[x][y]=0;mark[x][y]=0;bi.setRGB(x,y,0);}}}}markNum=0;for(inty=miny+1;y+1height;y++){for(intx=minx+1;x+1width;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)=6intnp=neighbor[0]+neighbor[1]+neighbor[2]+neighbor[3]+neighbor[4]+neighbor[5]+neighbor[6]+neighbor[7];if(np2||np6)continue;//条件3:S(p)=1intsp=0;for(inti=1;i8;i++){if(neighbor[i]-neighbor[i-1]==1)sp++;}if(neighbor[0]-neighbor[7]==1)sp++;if(sp!=1)continue;//条件4:p2*p0*p4==0if(neighbor[2]*neighbor[0]*neighbor[4]!=0)continue;//条件5:p2*p6*p4==0if(neighbor[2]*neighbor[6]*neighbor[4]!=0)continue;//标记删除mark[x][y]=1;markNum++;loop=true;}}if(markNum0){for(inty=miny;yheight;y++){for(intx=minx;xwidth;x++){//删除标记点if(mark[x][y]==1){pixel[x][y]=0;bi.setRGB(x,y,0);}}}}}ImageIO.write(bi,png,fout);}voidshow(){JFrameframe=newJFrame(细化);JPanelpanel=newJPanel();ImageIconimg1=newImageIcon(E:/img/before.png);JLabellabel1=newJLabel(细化前,img1,SwingConstants.LEFT);panel.add(label1);ImageIconimg2=newImageIcon(E:/img/after.png);JLabellabel2=newJLabel(细化后,img2,SwingConstants.LEFT);panel.add(label2);frame.setSize(700,700);frame.add(panel);frame.setVisible(true);}/***@paramargs*@throwsIOException*/publicstaticvoidmain(String[]args)throwsIOException{//TODOAuto-generatedmethodstubImagetemp=newImage();temp.ZhangThinning();temp.show();}}
本文标题:ZHANG快速并行细化算法JAVA实现
链接地址:https://www.777doc.com/doc-6422952 .html