摘要
图像识别是目前很热门的研究领域,涉及的知识很广,包括信息论、模式识别、模糊数学、图像编码、内容分类等等。本文仅对使用Java实现了一个简单的图像文本二值处理,关于识别并未实现。步骤
-
建立文本字符模板二值矩阵
-
对测试字符进行二值矩阵化处理
代码
/*
*@(#)StdModelRepository.java
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
packagecn.edu.ynu.sei.recognition.util;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.logging.Level;
importjava.util.logging.Logger;
importjavax.imageio.ImageIO;
/**
*HoldcharactercharImgsasstandardmodelrepository.
*@author88250
*@version1.0.0.0,Mar20,2008
*/
publicclassStdModelRepository{
/**
*holdcharacterimages
*/
List<BufferedImage>charImgs=newArrayList<BufferedImage>();
/**
*defaultwidthofacharacter
*/
staticintwidth=16;
/**
*defaultheightofacharacter
*/
staticintheight=28;
/**
*standardcharactermodelmatrix
*/
publicint[][][]stdCharMatrix=newint[27][width][height];
/**
*Defaultconstructor.
*/
publicStdModelRepository(){
BufferedImagelowercase=null;
try{
lowercase=ImageIO.read(newFile("lowercase.png"));
}catch(IOExceptionex){
Logger.getLogger(StdModelRepository.class.getName()).
log(Level.SEVERE,null,ex);
}
for(inti=0;i<26;i++){
charImgs.add(lowercase.getSubimage(i*width,
0,
width,
height));
}
for(inti=0;i<charImgs.size();i++){
Imageimage=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]);
}
}
}
*@(#)StdModelRepository.java
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
packagecn.edu.ynu.sei.recognition.util;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.logging.Level;
importjava.util.logging.Logger;
importjavax.imageio.ImageIO;
/**
*HoldcharactercharImgsasstandardmodelrepository.
*@author88250
*@version1.0.0.0,Mar20,2008
*/
publicclassStdModelRepository{
/**
*holdcharacterimages
*/
List<BufferedImage>charImgs=newArrayList<BufferedImage>();
/**
*defaultwidthofacharacter
*/
staticintwidth=16;
/**
*defaultheightofacharacter
*/
staticintheight=28;
/**
*standardcharactermodelmatrix
*/
publicint[][][]stdCharMatrix=newint[27][width][height];
/**
*Defaultconstructor.
*/
publicStdModelRepository(){
BufferedImagelowercase=null;
try{
lowercase=ImageIO.read(newFile("lowercase.png"));
}catch(IOExceptionex){
Logger.getLogger(StdModelRepository.class.getName()).
log(Level.SEVERE,null,ex);
}
for(inti=0;i<26;i++){
charImgs.add(lowercase.getSubimage(i*width,
0,
width,
height));
}
for(inti=0;i<charImgs.size();i++){
Imageimage=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
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
packagecn.edu.ynu.sei.recognition.util;
importjava.awt.Image;
importjava.awt.image.PixelGrabber;
importjava.util.logging.Level;
importjava.util.logging.Logger;
/**
*Mainipulationofimagedata.
*@author88250
*@version1.0.0.3,Mar20,2008
*/
publicclassImageUtils{
/**
*Returnallofthepixelvaluesofsepecified<code>image</code>.
*@paramimagethesepecifiedimage
*@paramwidthwidthofthe<code>image</code>
*@paramheightheightofthe<code>image</code>
*@return
*/
publicstaticint[]getPixels(Imageimage,intwidth,intheight){
int[]pixels=newint[width*height];
try{
newPixelGrabber(image,0,0,width,height,pixels,0,width).grabPixels();
}catch(InterruptedExceptionex){
Logger.getLogger(ImageUtils.class.getName()).
log(Level.SEVERE,null,ex);
}
returnpixels;
}
/**
*Getamatrixthatdescribedthe<code>pixels</code>.
*<p>
*Forexample,theresultofthematrixlikethis:
*<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>
*itdescribesthealphbet'a'.
*</p>
*@parampixelsthepixelarray
*@paramsparseFactorsparsefactor
*@returnamatrixthatdescribesaalphbet
*/
publicstaticint[][]getSymbolMatrix(int[]pixels,intsparseFactor){
finalintwidth=StdModelRepository.width;
finalintheight=StdModelRepository.height;
int[][]ret=newint[width][height];
for(inti=0;i<height;i++){
for(intj=0;j<width;j++){
if(pixels[i*width+j]==-1){
ret[j][i]=0;
}else{
ret[j][i]=1;
}
}
}
returnret;
}
/**
*Printthe<code>matrix</code>underconsole.
*@parammatrixthesepecifiedmatrixdata
*/
publicstaticvoiddisplayMatrix(int[][]matrix){
System.out.println(" ");
for(inti=0;i<matrix[0].length;i++){
for(intj=0;j<matrix.length;j++){
if(matrix[j][i]!=0){
System.out.print("*");
}else{
System.out.print("");
}
}
System.out.println();
}
}
}
*@(#)ImageUtils.java
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
packagecn.edu.ynu.sei.recognition.util;
importjava.awt.Image;
importjava.awt.image.PixelGrabber;
importjava.util.logging.Level;
importjava.util.logging.Logger;
/**
*Mainipulationofimagedata.
*@author88250
*@version1.0.0.3,Mar20,2008
*/
publicclassImageUtils{
/**
*Returnallofthepixelvaluesofsepecified<code>image</code>.
*@paramimagethesepecifiedimage
*@paramwidthwidthofthe<code>image</code>
*@paramheightheightofthe<code>image</code>
*@return
*/
publicstaticint[]getPixels(Imageimage,intwidth,intheight){
int[]pixels=newint[width*height];
try{
newPixelGrabber(image,0,0,width,height,pixels,0,width).grabPixels();
}catch(InterruptedExceptionex){
Logger.getLogger(ImageUtils.class.getName()).
log(Level.SEVERE,null,ex);
}
returnpixels;
}
/**
*Getamatrixthatdescribedthe<code>pixels</code>.
*<p>
*Forexample,theresultofthematrixlikethis:
*<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>
*itdescribesthealphbet'a'.
*</p>
*@parampixelsthepixelarray
*@paramsparseFactorsparsefactor
*@returnamatrixthatdescribesaalphbet
*/
publicstaticint[][]getSymbolMatrix(int[]pixels,intsparseFactor){
finalintwidth=StdModelRepository.width;
finalintheight=StdModelRepository.height;
int[][]ret=newint[width][height];
for(inti=0;i<height;i++){
for(intj=0;j<width;j++){
if(pixels[i*width+j]==-1){
ret[j][i]=0;
}else{
ret[j][i]=1;
}
}
}
returnret;
}
/**
*Printthe<code>matrix</code>underconsole.
*@parammatrixthesepecifiedmatrixdata
*/
publicstaticvoiddisplayMatrix(int[][]matrix){
System.out.println(" ");
for(inti=0;i<matrix[0].length;i++){
for(intj=0;j<matrix.length;j++){
if(matrix[j][i]!=0){
System.out.print("*");
}else{
System.out.print("");
}
}
System.out.println();
}
}
}
/*
*@(#)ImageTextRecognitor.java
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
packagecn.edu.ynu.sei.recognition;
importcn.edu.ynu.sei.recognition.util.StdModelRepository;
importcn.edu.ynu.sei.recognition.util.ImageUtils;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.util.logging.Level;
importjava.util.logging.Logger;
importjavax.imageio.ImageIO;
/**
*Demonstraterecognitioncharactersfromaimage.
*@author88250
*@version1.0.0.2,Mar20,2008
*/
publicclassImageTextRecognitor{
privatefinalstaticlongserialVersionUID=1L;
privateStdModelRepositorysmr=newStdModelRepository();
privateBufferedImagebufferedImage;
/**
*Defaultconstructor.
*/
publicImageTextRecognitor(){
try{
bufferedImage=ImageIO.read(newFile("a_yahei12.png"));
}catch(IOExceptionex){
Logger.getLogger(StdModelRepository.class.getName()).
log(Level.SEVERE,null,ex);
}
}
/**
*Returnthematchedcharacterwithsepecifiedimage.
*@paramsymbolMatrixthematrixofsepecifiedimage
*@returnthecharacter
*/
//TODO:thecorealgorithm
publicchargetMatchResult(int[][]symbolMatrix){
charresult=0;
inttmpEvaluation=100;
intevaluation=0;
returnresult;
}
/**
*Mainprogramentry.
*@paramargsshouldbe<code>null</code>
*/
publicstaticvoidmain(String[]args){
ImageTextRecognitorirt=newImageTextRecognitor();
BufferedImagebi=irt.bufferedImage;
Imageimg=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()));
}
}
*@(#)ImageTextRecognitor.java
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
packagecn.edu.ynu.sei.recognition;
importcn.edu.ynu.sei.recognition.util.StdModelRepository;
importcn.edu.ynu.sei.recognition.util.ImageUtils;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.util.logging.Level;
importjava.util.logging.Logger;
importjavax.imageio.ImageIO;
/**
*Demonstraterecognitioncharactersfromaimage.
*@author88250
*@version1.0.0.2,Mar20,2008
*/
publicclassImageTextRecognitor{
privatefinalstaticlongserialVersionUID=1L;
privateStdModelRepositorysmr=newStdModelRepository();
privateBufferedImagebufferedImage;
/**
*Defaultconstructor.
*/
publicImageTextRecognitor(){
try{
bufferedImage=ImageIO.read(newFile("a_yahei12.png"));
}catch(IOExceptionex){
Logger.getLogger(StdModelRepository.class.getName()).
log(Level.SEVERE,null,ex);
}
}
/**
*Returnthematchedcharacterwithsepecifiedimage.
*@paramsymbolMatrixthematrixofsepecifiedimage
*@returnthecharacter
*/
//TODO:thecorealgorithm
publicchargetMatchResult(int[][]symbolMatrix){
charresult=0;
inttmpEvaluation=100;
intevaluation=0;
returnresult;
}
/**
*Mainprogramentry.
*@paramargsshouldbe<code>null</code>
*/
publicstaticvoidmain(String[]args){
ImageTextRecognitorirt=newImageTextRecognitor();
BufferedImagebi=irt.bufferedImage;
Imageimg=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)