新聞中心
java 實(shí)現(xiàn)圖片的文字識(shí)別
摘要圖像識(shí)別是目前很熱門的研究領(lǐng)域,涉及的知識(shí)很廣,包括信息論、模式識(shí)別、模糊數(shù)學(xué)、圖像編碼、內(nèi)容分類等等。本文僅對(duì)使用Java實(shí)現(xiàn)了一個(gè)簡單的圖像文本二值處理,關(guān)于識(shí)別并未實(shí)現(xiàn)。
創(chuàng)新互聯(lián)公司主要從事網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)天水,十余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
步驟
建立文本字符模板二值矩陣
對(duì)測試字符進(jìn)行二值矩陣化處理
代碼
/*
* @(#)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 charImgs = new ArrayList();
/** * 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 codeimage .* @param image the sepecified image
* @param width width of the image
* @param height height of the image
* @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;
}
資源來自:
如何使用Java實(shí)現(xiàn)屏幕找圖功能
測試代碼
[java] view plain copy
public static void main(String[] args) throws Exception {
findImage4FullScreen(ImageCognition.SIM_ACCURATE_VERY);
}
public static void findImage4FullScreen(int sim) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int w = (int) screenSize.getWidth();
int h = 200;
Robot robot = new Robot();
BufferedImage screenImg = robot.createScreenCapture(new Rectangle(0, 0,
w, h));//對(duì)屏幕指定范圍進(jìn)行截圖,保存到BufferedImage中
OutputStream out = new FileOutputStream("data/images/screen.png");
ImageIO.write(screenImg, "png", out);//將截到的BufferedImage寫到本地
InputStream in = new FileInputStream("data/images/search.png");
BufferedImage searchImg = ImageIO.read(in);//將要查找的本地圖讀到BufferedImage
//圖片識(shí)別工具類
ImageCognition ic = new ImageCognition();
ListCoordBean list = ic.imageSearch(screenImg, searchImg, sim);
for (CoordBean coordBean : list) {
System.out.println("找到圖片,坐標(biāo)是" + coordBean.getX() + ","
+ coordBean.getY());
//標(biāo)注找到的圖的位置
Graphics g = screenImg.getGraphics();
g.setColor(Color.BLACK);
g.drawRect(coordBean.getX(), coordBean.getY(),
searchImg.getWidth(), searchImg.getHeight());
g.setFont(new Font(null, Font.BOLD, 20));
g.drawString("←找到的圖片在這里",
coordBean.getX() + searchImg.getWidth() + 5,
coordBean.getY() + 10 + searchImg.getHeight() / 2);
out = new FileOutputStream("data/images/result.png");
ImageIO.write(screenImg, "png", out);
}
}
額外的類
CoordBean
package cn.xt.imgCongnition;
public class CoordBean {
private int x;
private int y;
/**
* 獲取x坐標(biāo)
*
* @return x坐標(biāo)
*/
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
/**
* 獲取y坐標(biāo)
*
* @return
*/
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
RgbImageComparerBean
package cn.xt.imgCongnition;
/**
* RGB 相關(guān),圖片相似度計(jì)算時(shí)使用
*
*/
public class RgbImageComparerBean {
/****** 顏色值數(shù)組,第一緯度為x坐標(biāo),第二緯度為y坐標(biāo) ******/
private int colorArray[][];
/*** 是否忽略此點(diǎn),若為true,則不納入比較的像素行列。 ***/
private boolean ignorePx[][];
/**** 圖片的寬高 ****/
private int imgWidth;
private int imgHeight;
// 圖片的像素總數(shù)
private int pxCount;
/**
* 獲取圖像的二維數(shù)組組成
*
* @return 圖像的二維數(shù)組
*/
public int[][] getColorArray() {
return colorArray;
}
/**
* 要對(duì)比的像素總數(shù),會(huì)自動(dòng)篩選掉不對(duì)比的顏色
*
* @param pxCount
*/
public void setPxCount(int pxCount) {
this.pxCount = pxCount;
}
/**
* 設(shè)置顏色二維數(shù)組
*
* @param colorArray
* 顏色二維數(shù)組,一維為x軸,二維為y軸
*/
public void setColorArray(int[][] colorArray) {
this.colorArray = colorArray;
this.imgWidth = this.colorArray.length;
this.imgHeight = this.colorArray[0].length;
this.pxCount = this.imgWidth * this.imgHeight;
}
/**
* 是否忽略此點(diǎn),若為true,則不納入像素比較行列。
*
* @return 具體x,y坐標(biāo)的那個(gè)像素點(diǎn)
*/
public boolean[][] getIgnorePx() {
return ignorePx;
}
/**
* 是否忽略此點(diǎn),若為true,則不納入像素比較行列。
*
* @param ignorePx
* 具體x,y坐標(biāo)的那個(gè)像素點(diǎn)
*/
public void setIgnorePx(boolean[][] ignorePx) {
this.ignorePx = ignorePx;
}
/**
* 獲取圖像的寬度
*
* @return 圖像寬度
*/
public int getImgWidth() {
return imgWidth;
}
/**
* 獲取圖像的高度
*
* @return 圖像高度
*/
public int getImgHeight() {
return imgHeight;
}
/**
* 獲取圖像里像素的總數(shù)
*
* @return
*/
public int getPxCount() {
return pxCount;
}
}
Java 可不可以做圖像識(shí)別的系統(tǒng)
當(dāng)然可以。
一、純JAVA開發(fā)的技術(shù)可行性,即JAVA是否能夠?qū)崿F(xiàn)圖像識(shí)別的各種算法。
二、如果第一點(diǎn)沒有問題,純JAVA與C++相比,開發(fā)效率上的差異。效率要低很多,和具體問題有關(guān)。
三、如果第一點(diǎn)沒有問題且第二點(diǎn)差異不太大時(shí),純JAVA與C++相比,相同算法的情況下,軟件運(yùn)行效率的差異。運(yùn)行效率的差異也很大,也是和具體的算法有關(guān)。
網(wǎng)站題目:圖像識(shí)別java代碼 java圖像識(shí)別技術(shù)
分享網(wǎng)址:http://fisionsoft.com.cn/article/dopcgsh.html