Java文字图像识别(1)[88250原创]

 

摘要

图像识别是目前很热门的研究领域,涉及的知识很广,包括信息论、模式识别、模糊数学、图像编码、内容分类等等。本文仅对使用Java实现了一个简单的图像文本二值处理,关于识别并未实现。

步骤

  1. 建立文本字符模板二值矩阵

  2. 对测试字符进行二值矩阵化处理

代码

/*  
 * @(#)StdModelRepository.java
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
*/
package  cn.edu.ynu.sei.recognition.util;

import  java.awt.Image;
import  java.awt.image.BufferedImage;
import  java.io.File;
import  java.io.IOException;
import  java.util.ArrayList;
import  java.util.List;
import  java.util.logging.Level;
import  java.util.logging.Logger;
import  javax.imageio.ImageIO;

/**
 * Hold character charImgs as standard model repository.
 * 
@author  88250
 * 
@version  1.0.0.0, Mar 20, 2008
 
*/
public   class  StdModelRepository {

    
/**
     * hold character images
     
*/
    List
< BufferedImage >  charImgs  =   new  ArrayList < BufferedImage > ();

    
/**
     * default width of a character
     
*/
    
static   int  width  =   16 ;

    
/**
     * default height of a character
     
*/
    
static   int  height  =   28 ;

    
/**
     * standard character model matrix
     
*/
    
public   int [][][] stdCharMatrix  =   new   int [ 27 ][width][height];

    
/**
     * Default constructor.
     
*/
    
public  StdModelRepository() {
        BufferedImage lowercase 
=   null ;

        
try  {
            lowercase 
=  ImageIO.read( new  File( " lowercase.png " ));
        } 
catch  (IOException ex) {
            Logger.getLogger(StdModelRepository.
class .getName()).
                    log(Level.SEVERE, 
null , ex);
        }

        
for  ( int  i  =   0 ; i  <   26 ; i ++ ) {
            charImgs.add(lowercase.getSubimage(i 
*  width,
                                               
0 ,
                                               width,
                                               height));
        }

        
for  ( int  i  =   0 ; i  <  charImgs.size(); i ++ ) {
            Image image 
=  charImgs.get(i);
            
int [] pixels  =  ImageUtils.getPixels(image,
                                                image.getWidth(
null ),
                                                image.getHeight(
null ));

            stdCharMatrix[i] 
=  ImageUtils.getSymbolMatrix(pixels,  0 ).clone();

            ImageUtils.displayMatrix(stdCharMatrix[i]);

        }

    }
}

/*  
 * @(#)ImageUtils.java
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
*/
package  cn.edu.ynu.sei.recognition.util;

import  java.awt.Image;
import  java.awt.image.PixelGrabber;
import  java.util.logging.Level;
import  java.util.logging.Logger;

/**
 * Mainipulation of image data.
 * 
@author  88250
 * 
@version  1.0.0.3, Mar 20, 2008
 
*/
public   class  ImageUtils {

    
/**
     * Return all of the pixel values of sepecified <code>image</code>.
     * 
@param  image the sepecified image
     * 
@param  width width of the <code>image</code>
     * 
@param  height height of the <code>image</code>
     * 
@return
     
*/
    
public   static   int [] getPixels(Image image,  int  width,  int  height) {
        
int [] pixels  =   new   int [width  *  height];
        
try  {
            
new  PixelGrabber(image,  0 0 , width, height, pixels,  0 , width).grabPixels();
        } 
catch  (InterruptedException ex) {
            Logger.getLogger(ImageUtils.
class .getName()).
                    log(Level.SEVERE, 
null , ex);
        }
        
return  pixels;
    }

    
/**
     * Get a matrix that described the <code>pixels</code>.
     * <p>
     * For example, the result of the matrix like this:
     * <center>
     * 0000000000000000<br>
     * 0000000000000000<br>
     * 0001111111110000<br>
     * 0011111111111000<br>
     * 0011111111111100<br>
     * 0011000000011100<br>
     * 0000000000011100<br>
     * 0000111111111100<br>
     * 0011111111111100<br>
     * 0011111110011100<br>
     * 0111100000011100<br>
     * 0111100000011100<br>
     * 0111100000111100<br>
     * 0111100001111110<br>
     * 0011111111111100<br>
     * 0011111111111100<br>
     * 0001111111111100<br>
     * 0000000000000000<br>
     * 0000000000000000<br>
     * </center>
     * it describes the alphbet 'a'.
     * </p>
     * 
@param  pixels the pixel array
     * 
@param  sparseFactor sparse factor
     * 
@return  a matrix that describes a alphbet
     
*/
    
public   static   int [][] getSymbolMatrix( int [] pixels,  int  sparseFactor) {
        
final   int  width  =  StdModelRepository.width;
        
final   int  height  =  StdModelRepository.height;
        
int [][] ret  =   new   int [width][height];

        
for  ( int  i  =   0 ; i  <  height; i ++ ) {
            
for  ( int  j  =   0 ; j  <  width; j ++ ) {
                
if  (pixels[i  *  width  +  j]  ==   - 1 ) {
                    ret[j][i] 
=   0 ;
                } 
else  {
                    ret[j][i] 
=   1 ;
                }
            }
        }

        
return  ret;
    }

    
/**
     * Print the <code>matrix</code> under console.
     * 
@param  matrix the sepecified matrix data
     
*/
    
public   static   void  displayMatrix( int [][] matrix) {
        System.out.println(
" " );

        
for  ( int  i  =   0 ; i  <  matrix[ 0 ].length; i ++ ) {
            
for  ( int  j  =   0 ; j  <  matrix.length; j ++ ) {
                
if  (matrix[j][i]  !=   0 ) {
                    System.out.print(
" * " );
                } 
else  {
                    System.out.print(
"   " );
                }
            }
            System.out.println();
        }
    }
}

/*  
 * @(#)ImageTextRecognitor.java
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
*/
package  cn.edu.ynu.sei.recognition;

import  cn.edu.ynu.sei.recognition.util.StdModelRepository;
import  cn.edu.ynu.sei.recognition.util.ImageUtils;
import  java.awt.Image;
import  java.awt.image.BufferedImage;
import  java.io.File;
import  java.io.IOException;
import  java.util.logging.Level;
import  java.util.logging.Logger;
import  javax.imageio.ImageIO;

/**
 * Demonstrate recognition characters from a image.
 * 
@author  88250
 * 
@version  1.0.0.2, Mar 20, 2008
 
*/
public   class  ImageTextRecognitor {

    
private   final   static   long  serialVersionUID  =   1L ;

    
private  StdModelRepository smr  =   new  StdModelRepository();

    
private  BufferedImage bufferedImage;

    
/**
     * Default constructor.
     
*/
    
public  ImageTextRecognitor() {
        
try  {
            bufferedImage 
=  ImageIO.read( new  File( " a_yahei12.png " ));
        } 
catch  (IOException ex) {
            Logger.getLogger(StdModelRepository.
class .getName()).
                    log(Level.SEVERE, 
null , ex);
        }
    }

    
/**
     * Return the matched character with sepecified image.
     * 
@param  symbolMatrix the matrix of sepecified image
     * 
@return  the character 
     
*/
    
//  TODO: the core algorithm
     public   char  getMatchResult( int [][] symbolMatrix) {
        
char  result  =   0 ;
        
int  tmpEvaluation  =   100 ;
        
int  evaluation  =   0 ;


        
return  result;
    }

    
/**
     * Main program entry.
     * 
@param  args should be <code>null</code>
     
*/
    
public   static   void  main(String[] args) {
        ImageTextRecognitor irt 
=   new  ImageTextRecognitor();
        BufferedImage bi 
=  irt.bufferedImage;

        Image img 
=  bi.getScaledInstance( 16 28 , BufferedImage.SCALE_FAST);
        
int [] testImgPixels  =  ImageUtils.getPixels(img, img.getWidth( null ),
                                                   img.getHeight(
null ));
        ImageUtils.displayMatrix(ImageUtils.getSymbolMatrix(testImgPixels, 
2 ));
    
//  irt.getMatchResult(ImageUtils.getPixels(bi,
    
//                                        bi.getWidth(),
    
//                                      bi.getHeight()));

    }
}

测试

标准字符图像


测试字符图像


测试输出

init:
deps-jar:
Compiling 2 source files to /home/daniel/Work/Sources/Java/ImageTextRecognition/build/classes
compile:
run:




               
               
               
               
               
               
               
   *********   
  ***********  
  ************ 
  **       *** 
           *** 
    ********** 
  ************ 
  *******  *** 
 ****      *** 
 ****      *** 
 ****     **** 
 ****    ***** 
  ************ 
  ************ 
   *********** 
               
               
               
               
               
               




               
               
  ***          
  ***          
  ***          
  ***          
  ***          
  *** ******   
  ************ 
  ************ 
  *****   *****
  ****     ****
  ****     ****
  ****      ***
  ***       ***
  ****      ***
  ****     ****
  ****     ****
  *****   *****
  ************ 
  ************ 
  *** *******  
               
               
               
               
               
               




               
               
               
               
               
               
               
     ********* 
    ********** 
   *********** 
   ****      * 
  ****         
  ****         
  ****         
  ****         
  ****         
  ****         
  ****         
   ****      * 
   *********** 
    ********** 
     ********* 
               
               
               
               
               
               




               
               
           *** 
           *** 
           *** 
           *** 
           *** 
    ********** 
   *********** 
  ************ 
  ****    **** 
 ****     **** 
 ****      *** 
 ****      *** 
 ****      *** 
 ****      *** 
 ****      *** 
 ****     **** 
  ****    **** 
  ************ 
   *********** 
    ********** 
               
               
               
               
               
               




               
               
               
               
               
               
               
    ********   
   *********** 
  ************ 
  ****     ****
  ***      ****
 ****       ***
 **************
 **************
 ****          
 ****          
 ****          
  ****       **
  *************
   ************
    ********** 
               
               
               
               
               
               




               
               
       ********
      *********
      *****    
      ****     
      ***      
  *************
  *************
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
               
               
               
               
               
               




               
               
               
               
               
               
               
    ********** 
   *********** 
  ************ 
  ****    **** 
 ****     **** 
 ****      *** 
 ****      *** 
 ****      *** 
 ****      *** 
 ****      *** 
 ****     **** 
  ****    **** 
  ************ 
   *********** 
    ********** 
           *** 
          **** 
   *      **** 
   **** *****  
   **********  
   *********   




               
               
  ***          
  ***          
  ***          
  ***          
  ***          
  *** *******  
  ***********  
  ************ 
  *****   **** 
  ****     *** 
  ****     *** 
  ****     *** 
  ***      *** 
  ***      *** 
  ***      *** 
  ***      *** 
  ***      *** 
  ***      *** 
  ***      *** 
  ***      *** 
               
               
               
               
               
               




               
               
       ***     
       ***     
       ***     
       ***     
               
   *******     
   *******     
       ***     
       ***     
       ***     
       ***     
       ***     
       ***     
       ***     
       ***     
       ***     
       ***     
       ***     
  *************
  *************
               
               
               
               
               
               




               
               
       ****    
       ****    
       ****    
       ****    
               
   ********    
   ********    
       ****    
       ****    
       ****    
       ****    
       ****    
       ****    
       ****    
       ****    
       ****    
       ****    
       ****    
       ****    
       ****    
       ****    
       ****    
       ****    
  ********     
  ********     
  *******      




               
               
   ***         
   ***         
   ***         
   ***         
   ***         
   ***     ****
   ***    **** 
   ***   ****  
   *** *****   
   ********    
   *******     
   ********    
   ********    
   **** ****   
   ***  *****  
   ***   ****  
   ***    **** 
   ***    *****
   ***     *****
   ***      ****
               
               
               
               
               
               




               
               
  *******      
  *******      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ***      
      ******** 
      ******** 
       ******* 
               
               
               
               
               
               




               
               
               
               
               
               
               
 ************* 
 **************
 **************
 ***  ****  ***
 ***   ***  ***
 ***   ***  ***
 ***   ***  ***
 ***   ***  ***
 ***   ***  ***
 ***   ***  ***
 ***   ***  ***
 ***   ***  ***
 ***   ***  ***
 ***   ***  ***
 ***   ***  ***
               
               
               
               
               
               




               
               
               
               
               
               
               
  *** *******  
  ***********  
  ************ 
  *****   **** 
  ****     *** 
  ****     *** 
  ****     *** 
  ***      *** 
  ***      *** 
  ***      *** 
  ***      *** 
  ***      *** 
  ***      *** 
  ***      *** 
  ***      *** 
               
               
               
               
               
               




               
               
               
               
               
               
               
    ********   
   **********  
  ************ 
  ****    **** 
  ***      ****
 ****      ****
 ****      ****
 ****      ****
 ****      ****
 ****      ****
  ***      ****
  ****    **** 
  ************ 
   **********  
    ********   
               
               
               
               
               
               




               
               
               
               
               
               
               
  *** ******   
  ***********  
  ************ 
  *****   *****
  ****     ****
  ****     ****
  ***       ***
  ***       ***
  ***       ***
  ****     ****
  ****     ****
  *****   *****
  ************ 
  ************ 
  *** ******   
  ***          
  ***          
  ***          
  ***          
  ***          
  ***          




               
               
               
               
               
               
               
    ********** 
   *********** 
  ************ 
  ****    **** 
  ***     **** 
 ****      *** 
 ****      *** 
 ****      *** 
 ****      *** 
 ****      *** 
  ***     **** 
  ****    **** 
  ************ 
   *********** 
    ********** 
           *** 
           *** 
           *** 
           *** 
           *** 
           *** 




               
               
               
               
               
               
               
     ***********
     ***********
     ***********
     ******    *
     *****     
     ****      
     ****      
     ****      
     ****      
     ****      
     ****      
     ****      
     ****      
     ****      
     ****      
               
               
               
               
               
               




               
               
               
               
               
               
               
*    ********* 
*   ********** 
*   ********** 
*  ****      * 
   ****        
   *****       
    *********  
     ********* 
       ********
           ****
            ***
   **      ****
   ************
   *********** 
   **********  
               
               
               
               
               
               




               
               
               
      ****     
      ****     
      ****     
      ****     
  *************
  *************
      ****     
      ****     
      ****     
      ****     
      ****     
      ****     
      ****     
      ****     
      ****     
      ****     
      *****    
       ********
        *******
               
               
               
               
               
               




               
               
               
               
               
               
               
   ***      ***
   ***      ***
   ***      ***
   ***      ***
   ***      ***
   ***      ***
   ***      ***
   ***      ***
   ***      ***
   ****     ***
   ****    ****
   ****    ****
    ***********
    ***********
     **********
               
               
               
               
               
               




               
               
               
               
               
               
               
  ****       ***
  ****      ****
   ***      ****
   ****     ***
   ****    ****
    ***    ****
    ****   *** 
    ****  **** 
     ***  **** 
     **** ***  
     ********  
      *******  
      ******   
      ******   
       ****    
               
               
               
               
               
               




               
               
               
               
               
               
               
 ***          **
 ***          **
 ***          **
 ****         **
 ****  ****  ***
  ***  ****  ***
  ***  ****  ***
  **** ***** ***
  **************
   *************
   ****** *****
   *****  *****
   *****  *****
    ****  *****
    ****   *** 
               
               
               
               
               
               




               
               
               
               
               
               
               
******      ****
*  ****    ****
*   ****  *****
*   ***** **** 
*    ********  
*     ******   
       *****   
       ****    
      ******   
     ********  
     ********* 
    ****  **** 
   ****    ****
  *****     ****
  ****      ****
               
               
               
               
               
               




               
               
               
               
               
               
               
  ****       ***
  ****      ****
   ***      ****
   ****     ****
   ****    ****
    ****   ****
    ****   *** 
     ***  **** 
     **** **** 
     **** ***  
      *******  
      *******  
      ******   
       *****   
*      ****    
       ****    
       ****    
      ****     
     *****     
   ******      
   ******      




               
               
               
               
               
               
               
*   ***********
    ***********
           ****
          *****
         ***** 
         ****  
        ****   
       ****    
      *****    
     *****     
     ****      
    ****       
   ****        
   ************
   ************
               
               
               
               
               
               




               
               
               
               
               
               
               
     *******   
     *******   
     ********  
     *    ***  
     *    ***  
      *******  
     ********  
     ********  
    *****  **  
    ***   ***  
    ***   ***  
    *********  
     ********  
     ********  
               
               
               
               
               
               
               
BUILD SUCCESSFUL (total time: 1 second)

转载于:https://www.cnblogs.com/lanzhi/archive/2008/03/21/6470441.html

ImageComparerUI——基于Java语言实现的相似图像识别,基于直方图比较算法。 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.MediaTracker; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JPanel; public class ImageComparerUI extends JComponent implements ActionListener { /** * */ private static final long serialVersionUID = 1L; private JButton browseBtn; private JButton histogramBtn; private JButton compareBtn; private Dimension mySize; // image operator private MediaTracker tracker; private BufferedImage sourceImage; private BufferedImage candidateImage; private double simility; // command constants public final static String BROWSE_CMD = "Browse..."; public final static String HISTOGRAM_CMD = "Histogram Bins"; public final static String COMPARE_CMD = "Compare Result"; public ImageComparerUI() { JPanel btnPanel = new JPanel(); btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); browseBtn = new JButton("Browse..."); histogramBtn = new JButton("Histogram Bins"); compareBtn = new JButton("Compare Result"); // buttons btnPanel.add(browseBtn); btnPanel.add(histogramBtn); btnPanel.add(compareBtn); // setup listener... browseBtn.addActionListener(this); histogramBtn.addActionListener(this); compareBtn.addActionListener(this); mySize = new Dimension(620, 500); JFrame demoUI = new JFrame("Similiar Image Finder"); demoUI.getContentPane().setLayout(new BorderLayout()); demoUI.getContentPane().add(this, BorderLayout.CENTER); demoUI.getContentPane().add(btnPanel, BorderLayout.SOUTH); demoUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); demoUI.pack(); demoUI.setVisible(true); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; if(sourceImage != null) { Image scaledImage = sourceImage.getScaledInstance(300, 300, Image.SCALE_FAST); g2.drawImage(scaledImage, 0, 0, 300, 300, null); } if(candidateImage != null) { Image scaledImage = candidateImage.getScaledInstance(300, 330, Image.SCALE_FAST); g2.drawImage(scaledImage, 310, 0, 300, 300, null); } // display compare result info here Font myFont = new Font("Serif", Font.BOLD, 16); g2.setFont(myFont); g2.setPaint(Color.RED); g2.drawString("The degree of similarity : " + simility, 50, 350); } public void actionPerformed(ActionEvent e) { if(BROWSE_CMD.equals(e.getActionCommand())) { JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(null); File f = chooser.getSelectedFile(); BufferedImage bImage = null; if(f == null) return; try { bImage = ImageIO.read(f); } catch (IOException e1) { e1.printStackTrace(); } tracker = new MediaTracker(this); tracker.addImage(bImage, 1); // blocked 10 seconds to load the image data try { if (!tracker.waitForID(1, 10000)) { System.out.println("Load error."); System.exit(1); }// end if } catch (InterruptedException ine) { ine.printStackTrace(); System.exit(1); } // end catch if(sourceImage == null) { sourceImage = bImage; }else if(candidateImage == null) { candidateImage = bImage; } else { sourceImage = null; candidateImage = null; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值