摘要
图像识别是目前很热门的研究领域,涉及的知识很广,包括信息论、模式识别、模糊数学、图像编码、内容分类等等。本文仅对使用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.
*/
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;
/**
*HoldcharactercharImgsasstandardmodelrepository.
*
@author
88250
*
@version
1.0.0.0,Mar20,2008
*/
public
class
StdModelRepository{
/**
*holdcharacterimages
*/
List
<
BufferedImage
>
charImgs
=
new
ArrayList
<
BufferedImage
>
();
/**
*defaultwidthofacharacter
*/
static
int
width
=
16
;
/**
*defaultheightofacharacter
*/
static
int
height
=
28
;
/**
*standardcharactermodelmatrix
*/
public
int
[][][]stdCharMatrix
=
new
int
[
27
][width][height];
/**
*Defaultconstructor.
*/
public
StdModelRepository(){
BufferedImagelowercase
=
null
;
try
{
lowercase
=
ImageIO.read(
new
File(
"
lowercase.png
"
));
}
catch
(IOExceptionex){
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
++
){
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.
*/
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;
/**
*Mainipulationofimagedata.
*
@author
88250
*
@version
1.0.0.3,Mar20,2008
*/
public
class
ImageUtils{
/**
*Returnallofthepixelvaluesofsepecified<code>image</code>.
*
@param
imagethesepecifiedimage
*
@param
widthwidthofthe<code>image</code>
*
@param
heightheightofthe<code>image</code>
*
@return
*/
public
static
int
[]getPixels(Imageimage,
int
width,
int
height){
int
[]pixels
=
new
int
[width
*
height];
try
{
new
PixelGrabber(image,
0
,
0
,width,height,pixels,
0
,width).grabPixels();
}
catch
(InterruptedExceptionex){
Logger.getLogger(ImageUtils.
class
.getName()).
log(Level.SEVERE,
null
,ex);
}
return
pixels;
}
/**
*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>
*
@param
pixelsthepixelarray
*
@param
sparseFactorsparsefactor
*
@return
amatrixthatdescribesaalphbet
*/
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;
}
/**
*Printthe<code>matrix</code>underconsole.
*
@param
matrixthesepecifiedmatrixdata
*/
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
*
*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.
*/
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;
/**
*Demonstraterecognitioncharactersfromaimage.
*
@author
88250
*
@version
1.0.0.2,Mar20,2008
*/
public
class
ImageTextRecognitor{
private
final
static
long
serialVersionUID
=
1L
;
private
StdModelRepositorysmr
=
new
StdModelRepository();
private
BufferedImagebufferedImage;
/**
*Defaultconstructor.
*/
public
ImageTextRecognitor(){
try
{
bufferedImage
=
ImageIO.read(
new
File(
"
a_yahei12.png
"
));
}
catch
(IOExceptionex){
Logger.getLogger(StdModelRepository.
class
.getName()).
log(Level.SEVERE,
null
,ex);
}
}
/**
*Returnthematchedcharacterwithsepecifiedimage.
*
@param
symbolMatrixthematrixofsepecifiedimage
*
@return
thecharacter
*/
//
TODO:thecorealgorithm
public
char
getMatchResult(
int
[][]symbolMatrix){
char
result
=
0
;
int
tmpEvaluation
=
100
;
int
evaluation
=
0
;
return
result;
}
/**
*Mainprogramentry.
*
@param
argsshouldbe<code>null</code>
*/
public
static
void
main(String[]args){
ImageTextRecognitorirt
=
new
ImageTextRecognitor();
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)