图像识别与处理代码.docx
- 文档编号:2121186
- 上传时间:2023-05-02
- 格式:DOCX
- 页数:101
- 大小:37.76KB
图像识别与处理代码.docx
《图像识别与处理代码.docx》由会员分享,可在线阅读,更多相关《图像识别与处理代码.docx(101页珍藏版)》请在冰点文库上搜索。
图像识别与处理代码
1.FileChooserTest.java类:
packageOperationOfPictures;
importjava.awt.Image;
importjava.awt.Toolkit;
importjavax.swing.JFrame;
publicclassFileChooserTest{
publicstaticvoidmain(String[]args){
ImageViewerFrameframe=newImageViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Toolkitkit=Toolkit.getDefaultToolkit();
Imageimage=kit.getImage("Mark.jpg");
frame.setIconImage(image);
}
}
2.Canvas.java类:
packageOperationOfPictures;
importjava.awt.geom.AffineTransform;
importjava.awt.image.BufferedImage;
importjava.awt.*;
importjavax.swing.JPanel;
publicclassCanvasextendsJPanel{
privateBufferedImagebufferedImage;//Buffered
privateAffineTransformtrans=newAffineTransform();
publicvoidsetImage(BufferedImagebufferedImage){
if(bufferedImage!
=null){
this.bufferedImage=bufferedImage;
}
if(isVisible()){
paintImmediately(0,0,getWidth(),getHeight());
}
}
publicBufferedImagegetImage(){
returnbufferedImage;
}
publicvoidsetRota(doublerota){
trans.setToRotation(rota,(bufferedImage.getWidth())>>1,(bufferedImage.getHeight())>>1);
}
protectedvoidpaintComponent(Graphicsg){
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
if(bufferedImage==null){
return;
}
Graphics2Dgg=(Graphics2D)g.create();
intiw=bufferedImage.getWidth(),ih=bufferedImage.getHeight();
intanchorX=(getWidth()-iw)>>1,anchorY=(getHeight()-ih)>>1;
AffineTransformaf=gg.getTransform();
af.translate(anchorX,anchorY);
af.concatenate(trans);
gg.drawImage(bufferedImage,af,this);
gg.dispose();
}
}
3.Cellapplet.java类:
packageOperationOfPictures;
importjava.applet.Applet;
importjava.awt.*;
importjavax.swing.*;
publicclassCellappletextendsApplet{
privateintdefault_r=1;
privatelongdefault_nr=18;
privateintr;
privatelongnr;
privatebooleanlayout=false;
privateThreaddrawThread=null;
privateCellcanvascanvas;
privateChoiceradiusChoice;
privateJTextFieldruleField;
privateJLabelruleSequence;
privateButtonstopButton,restartButton;
publicCellapplet(){
}
publicvoidinit(){
if(drawThread!
=null){
drawThread=null;
}
r=1;
nr=18;
if(r<1){
r=default_r;
}
if(nr<0){
nr=default_nr;}
if(layout==false){
GridBagLayoutgridLayout=newGridBagLayout();//布局管理器
GridBagConstraintsconstr;//使用GridBagLayout布局管理器的约束
setLayout(gridLayout);//设定添加布局管理器
PanelcellPanel=newPanel();
cellPanel.setLayout(newGridLayout(1,0));//设定添加布局管理器
canvas=newCellcanvas(r,nr);
cellPanel.add("Center",canvas);
constr=newGridBagConstraints();
constr.fill=GridBagConstraints.BOTH;
constr.insets=newInsets(0,0,0,0);
constr.weightx=1.0;
constr.weighty=1.0;
constr.gridwidth=GridBagConstraints.REMAINDER;
gridLayout.setConstraints(cellPanel,constr);
add(cellPanel);
JLabelradius_label=newJLabel("半径:
");
constr=newGridBagConstraints();
constr.fill=GridBagConstraints.BOTH;
constr.insets=newInsets(2,4,2,4);
constr.weightx=0.1;
constr.weighty=0.0;
constr.gridx=3;
constr.gridy=2;
gridLayout.setConstraints(radius_label,constr);
add(radius_label);
radiusChoice=newChoice();
radiusChoice.addItem("1");
radiusChoice.addItem("2");
radiusChoice.addItem("3");
radiusChoice.select(r-1);
radiusChoice.setEnabled(false);
constr=newGridBagConstraints();
constr.fill=GridBagConstraints.BOTH;
constr.insets=newInsets(2,4,2,4);
constr.weightx=0.3;
constr.weighty=0.0;
constr.gridx=4;
constr.gridy=2;
gridLayout.setConstraints(radiusChoice,constr);
add(radiusChoice);
JLabelrule_label=newJLabel("编码:
");
constr=newGridBagConstraints();
constr.fill=GridBagConstraints.BOTH;
constr.insets=newInsets(2,4,2,4);
constr.weightx=0.1;
constr.weighty=0.0;
constr.gridx=5;
constr.gridy=2;
gridLayout.setConstraints(rule_label,constr);
add(rule_label);
ruleField=newJTextField(""+nr,6);
ruleField.setEnabled(false);
constr=newGridBagConstraints();
constr.fill=GridBagConstraints.BOTH;
constr.insets=newInsets(2,4,2,4);
constr.weightx=0.3;
constr.weighty=0.0;
constr.gridx=6;
constr.gridy=2;
constr.gridwidth=GridBagConstraints.REMAINDER;
gridLayout.setConstraints(ruleField,constr);
add(ruleField);
stopButton=newButton("Stop");
constr=newGridBagConstraints();
constr.fill=GridBagConstraints.BOTH;
constr.insets=newInsets(2,4,2,4);
constr.weightx=0.3;
constr.weighty=0.0;
constr.gridx=3;
constr.gridy=3;
constr.gridwidth=2;
gridLayout.setConstraints(stopButton,constr);
add(stopButton);
restartButton=newButton("Restart");
restartButton.setEnabled(false);
constr=newGridBagConstraints();
constr.fill=GridBagConstraints.BOTH;
constr.insets=newInsets(2,4,2,4);
constr.weightx=0.3;
constr.weighty=0.0;
constr.gridx=5;
constr.gridy=3;
constr.gridwidth=GridBagConstraints.REMAINDER;
gridLayout.setConstraints(restartButton,constr);
add(restartButton);
ruleSequence=newJLabel("");
ruleSequence.setText("基元序列:
"+canvas.getRule());
constr=newGridBagConstraints();
constr.fill=GridBagConstraints.BOTH;
constr.insets=newInsets(2,4,2,4);
constr.weightx=0.3;
constr.weighty=0.0;
constr.gridx=1;
constr.gridy=4;
constr.gridwidth=GridBagConstraints.REMAINDER;
gridLayout.setConstraints(ruleSequence,constr);
add(ruleSequence);
ruleSequence.setVisible(true);
validate();
layout=true;
}
}
publicvoidstart(){
drawThread=newThread(canvas);
drawThread.setPriority(Thread.MIN_PRIORITY);
drawThread.start();
}
publicbooleanaction(Eventevt,Objectarg){
if(evt.targetinstanceofButton){
if(arg.equals("Restart")){
intnew_r;
new_r=Integer.parseInt(radiusChoice.getSelectedItem());
longnew_nr;
new_nr=Long.parseLong(ruleField.getText());
if(drawThread!
=null)
drawThread=null;
r=new_r;
nr=new_nr;
canvas.reinit(r,nr);
drawThread=newThread(canvas);
drawThread.setPriority(Thread.MIN_PRIORITY);
drawThread.start();
ruleSequence.setText("基元序列:
"+canvas.getRule());
restartButton.setEnabled(false);
radiusChoice.setEnabled(false);
ruleField.setEnabled(false);
stopButton.setEnabled(true);
}elseif(arg.equals("Stop")){
if(drawThread!
=null){
drawThread.interrupt();
drawThread=null;
restartButton.setEnabled(true);
radiusChoice.setEnabled(true);
ruleField.setEnabled(true);
stopButton.setEnabled(false);
}}}
returntrue;
}}
4.Cellauto.java类:
packageOperationOfPictures;
importjava.util.Random;
publicclassCellauto{//细胞序列类
privatefinalintcellState=2;//状态数
privateintradius=1;//半径
privatelongruleMaster=18;//编码
privateint[]ruleSequence=makeRule();//制定细胞状态演化规则
publicCellauto(intr,longMaster){
if(radius!
=r||ruleMaster!
=Master){
if(r>=0){
radius=r;//细胞的邻域半径
}
if(Master>=0){
ruleMaster=Master;//细胞规则的控制者
}
ruleSequence=makeRule();//重新制定规则
}
}
privateint[]makeRule(){
intn=(int)(Math.pow(cellState,2*radius+1));//确定规则序列的长度
int[]Sequence=newint[n];
for(intk=1;k<=n;k++){
Sequence[k-1]=computeValue(k,ruleMaster);//确定规则序列每个元素的值
}
returnSequence;
}
privateintcomputeValue(intcount,longn){
if(count==1){
return(int)(n%cellState);
}else{
returncomputeValue(count-1,n/cellState);//递归求值
}
}
publicint[]initSequence(intn){//随机生成细胞初始状态序列
Randomrandom=newRandom();
int[]Sequence=newint[n];
for(inti=0;i Sequence[i]=(int)(cellState*random.nextDouble()); if(Sequence[i]>cellState-1){ Sequence[i]=cellState-1; }} returnSequence; } publicint[]nextSequence(int[]Sequence){//求当前细胞序列的下一个状态序列 intlen=Sequence.length; int[]newSequence=newint[len]; for(inti=0;i newSequence[i]=0; for(intj=-radius;j<=radius;j++) newSequence[i]=newSequence[i]*cellState+Sequence[(i+j+len)%len]; newSequence[i]=ruleSequence[newSequence[i]]; } returnnewSequence; } publicint[]getRuleSequence(){ returnruleSequence; } } 5.Cellcanvas.java类: packageOperationOfPictures; importjava.awt.*; publicclassCellcanvasextendsCanvasimplementsRunnable{//细胞状态板 privateintwidth=-1;//宽度 privateintheight=-1;//高度 privateintgridwidth=16;//细胞的大小 privateintnumber1,number2;//分别控制细胞的横向变化和纵向变化 privateCellautocell=null;//细胞序列类型 privateint[]startSequence=null;//细胞序列的开始状态 privateColor[]colors=newColor[2];//细胞的状态数组 privateImagecellPicture=null;//细胞图像 privateint[]ruleSequence=null; publicCellcanvas(intr,longMaster){ cell=newCellauto(r,Master);//细胞序列初始化实例 colors[0]=Color.white;//黑白色表示细胞的两种状态 colors[1]=Color.black; ruleSequence=cell.getRuleSequence(); } publicvoidreinit(intr,longMaster){//改变条件后的细胞序列实例 cell=newCellauto(r,Master); ruleSequence=cell.getRuleSequence(); } publicvoidrun(){ Graphics2Dg; Dimensiond=getSize(); int[]nextSequence; width=d.width; number1=(int)(width/gridwidth);//确定面板内一行容纳的细胞数,即细胞序列长度 height=d.height; number2=(int)(height/gridwidth); startSequence=cell.initSequence(number1); cellPicture=this.createImage(width,height); g=(Graphics2D)cellPicture.getGraphics(); //Drawthebackground g.setColor(Color.white);//设置背景色 g.fillRect(0,0,width,height);//填充背景色 //Drawtheinitialconfiguration for(inti=0;i g.setColor(colors[startSequence[i]]);//确定初始细胞序列各个细胞的状态颜色 g.fillRect(i*gridwidth,0,gridwidth,gridwidth);//画出初始细胞序列的状态 } show
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 图像 识别 处理 代码