找传奇、传世资源到传世资源站!

java车牌图像处理 输入一个不是整车的车牌照片,识别出车牌上的字符

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

车牌识别率 需要进一步提升
from clipboard
from clipboardfrom clipboard
from clipboard
from clipboard
from clipboard
from clipboardpackage lqk;import java.awt.*;import java.awt.event.*;import java.awt.image.ColorModel;import java.awt.image.ImageProducer;import java.awt.image.MemoryImageSource;import java.awt.image.PixelGrabber;import java.io.File;import javax.swing.JOptionPane;class MainFrame extends Frame implements ActionListener{ /** * */private static final long serialVersionUID = 1L;//什么是全局变量,那就是全局都要引用的,其它的都是局部变量//窗口对象private FileDialog filedialog_load;//声明1个打开文件对话筐private MyDialog options;private MenuItem itemOpen=null,itemOption=null;private MyPanel center=null;private Button next=null;//图像数据private Image image;//通过image对象,将图像画出来private int[][] gray;private int[] pixels,hy,vx;private int alpha=255,width,height,length;//透明度255是完全不透明private ColorModel cm=ColorModel.getRGBdefault();MainFrame(){//初始化窗体super("车牌图像识别");setSize(500,500); setVisible(true); //Image image=Toolkit.getDefaultToolkit().createImage(file.getAbsolutePath()); //this.setIconImage(image); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0);} }); //定义菜单栏 MenuBar menubar=new MenuBar(); Menu file=new Menu("文件"); itemOpen=new MenuItem("打开文件"); itemOpen.addActionListener(this); file.add(itemOpen); Menu option=new Menu("选项"); itemOption=new MenuItem("参数设置"); itemOption.addActionListener(this); option.add(itemOption); menubar.add(file); menubar.add(option); setMenuBar(menubar); //定义组件和布局,Frame默认是BorderLayout布局 center=new MyPanel();//根据命令显示原始和经过处理的图片 add(center,BorderLayout.CENTER); Panel south=new Panel(); next=new Button("二值化"); next.setEnabled(false); next.addActionListener(this); south.setLayout(new FlowLayout(FlowLayout.RIGHT)); south.add(next); add(south,BorderLayout.SOUTH); options=new MyDialog(this,"参数设置",true); //定义对话框 filedialog_load=new FileDialog(this,"打开文件话框",FileDialog.LOAD); filedialog_load.addWindowListener(new WindowAdapter()//对话框增加适配器 {public void windowClosing(WindowEvent e) { filedialog_load.setVisible(false);}}); validate();} /*基本的流程就是这样,经过每一步的处理,Image,pixels,width和height就会发生变化。 * * */public void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource()==itemOpen){filedialog_load.setVisible(true); if(filedialog_load.getFile()!=null){ //获取选中的图像路径 File file= new File(filedialog_load.getDirectory(),filedialog_load.getFile()); //根据路径得到原始图像 image=Toolkit.getDefaultToolkit().createImage(file.getAbsolutePath()); imageToPixels(image); this.setSize(width 30, height 100); next.setLabel("二值化"); next.setEnabled(true); } }if(e.getSource()==itemOption){options.setVisible(true);}if(e.getSource()==next){ //得到经过二值化处理的图像String label=next.getLabel();if(label.equals("二值化")){gray=pixelsToGrayArray();Binary k=new Binary(gray,width,height);gray=k.binary();next.setLabel("去除边框"); image=grayArrayToImage(gray,width,height);}else if(label.equals("去除边框")){RemoveFrame rf=new RemoveFrame(gray,width,height);rf.removeFrame();gray=rf.getGrayArray();height=rf.getHeight();width=rf.getWidth();image=grayArrayToImage(gray,width,height);next.setLabel("字符分割");}else if(label.equals("字符分割")){CharacterDivide cd=new CharacterDivide(gray,width,height);cd.divide();//字符分割hy=cd.getHy();vx=cd.getVx();length=cd.getLength();if(length>14){JOptionPane.showMessageDialog(this,"识别的字符数目超过7个,请更改参数设置!","警告对话框",JOptionPane.WARNING_MESSAGE);}if(Options.MODELVALUE==0){//快速识别模式,在字符分割以后,对字母和数字进行内分割InnerDivide id=new InnerDivide(gray,hy,vx,width,height);image=grayArrayToImage(id.paintF(),width,height);}else{//详细识别模式image=grayArrayToImage(cd.paintHVF(),width,height);}next.setLabel("字符缩放");}else if(label.equals("字符缩放")){CharacterZoom zoom=new CharacterZoom(gray,hy,vx);if(Options.MODELVALUE==0){//快速识别模式//Zoom zoom=new Zoom(gray);image=grayArrayToImage(zoom.zoom(vx[0],hy[0],vx[1]-vx[0],hy[1]-hy[0]),20,40);}else{image=grayArrayToImage(zoom.paintGray(),20*length,40);}next.setLabel("模板识别");}else if(label.equals("模板识别")){if(Options.MODELVALUE==0){QuickIdentify identify=new QuickIdentify(gray,hy,vx);JOptionPane.showMessageDialog(this,identify.judge(),"识别结果",JOptionPane.INFORMATION_MESSAGE);}else if(Options.MODELVALUE==1){DetailedIdentify identify=new DetailedIdentify(gray,hy,vx);JOptionPane.showMessageDialog(this,identify.judge(),"识别结果",JOptionPane.INFORMATION_MESSAGE);}else{MultipleIdentify identify=new MultipleIdentify(gray,hy,vx);JOptionPane.showMessageDialog(this,identify.judge(),"识别结果",JOptionPane.INFORMATION_MESSAGE);}next.setEnabled(false);}}center.setImage(image); center.repaint();}public void imageToPixels(Image image){//为全局变量pixels,iw,ih赋值MediaTracker tracker=new MediaTracker(this);//这里有一个this,导致该方法不能分离到别的类中//Image img=Toolkit.getDefaultToolkit().createImage(imagePath);tracker.addImage(image, 0);//等待图像完全加载try{tracker.waitForID(0);}catch(InterruptedException e){e.printStackTrace();}//获取图像的宽度和高度width=image.getWidth(this);height=image.getHeight(this);//提取图像的像素pixels=new int[width*height];try{PixelGrabber pg=new PixelGrabber(image,0,0,width,height,pixels,0,width);pg.grabPixels();}catch (InterruptedException e) {e.printStackTrace();}//对RGB值和Alpha值进行重新计算和赋值int gray;alpha=cm.getAlpha(pixels[0]);for(int i=0;i<width*height;i ){int red=cm.getRed(pixels[i]);int green=cm.getGreen(pixels[i]);int blue=cm.getBlue(pixels[i]);gray=(int)(red*0.3 green*0.59 blue*0.11);pixels[i]=alpha<<24|gray<<16|gray<<8|gray;/*是将pixels[i]赋值为如下: * pixels[i] 第3字节 第2字节 第1字节 第0字节alpha red green blue * */}}public Image grayArrayToImage(int[][]gray,int width,int height){Image image=null;int []pixels=new int[width*height]; int g; for(int i=0;i<height;i )for(int j=0;j<width;j ){g=gray[i][j]*255;//如果是1的话就是255,如果是0的话,还是0//g=gray[i][j];pixels[i*width j]=alpha<<24|g<<16|g<<8|g;} ImageProducer ip=new MemoryImageSource(width,height,pixels,0,width);image=createImage(ip);return image;}/*public Image pixelsToImage(int []pixels,int width,int height){//根据像素和宽高产生一幅图像Image image=null;ImageProducer ip=new MemoryImageSource(width,height,pixels,0,width);image=createImage(ip);return image;}*/public int[][] pixelsToGrayArray(){int [][]grayArray=new int[height][width];for(int i=0;i<width*height;i ){int red=cm.getRed(pixels[i]);int green=cm.getGreen(pixels[i]);int blue=cm.getBlue(pixels[i]);int gray=(int)(red*0.3 green*0.59 blue*0.11);//pixels[i]=alpha<<24|gray<<16|gray<<8|gray;grayArray[i/width][i%width]=gray;}return grayArray; }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复