Java文字Image Recognition(1)[88250原創]__Java

來源:互聯網
上載者:User
  摘要 Image Recognition是目前很熱門的研究領域,涉及的知識很廣,包括資訊理論、模式識別、模糊數學、映像編碼、內容分類等等。本文僅對使用Java實現了一個簡單的映像文本二值處理,關於識別並未實現。

步驟 建立文本字元模板二值矩陣 對測試字元進行二值矩陣化處理 代碼 /*  
 * @(#)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  =  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.