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

本文介绍了一个用Java实现的简单图像文本二值处理方法。通过构建字符模板矩阵,并将待识别字符转换为二值矩阵,实现了初步的文字匹配。文章提供了源代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

摘要

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

步骤

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

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

代码

/*
*@(#)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();
}
}
}

/*
*@(#)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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值