摘要圖像識別是目前很熱門的研究領(lǐng)域,涉及的知識很廣,包括信息論、模式識別、模糊數(shù)學(xué)、圖像編碼、內(nèi)容分類等等。本文僅對使用Java實現(xiàn)了一個簡單的圖像文本二值處理,關(guān)于識別并未實現(xiàn)。
專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)龍亭免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了1000多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
步驟
建立文本字符模板二值矩陣
對測試字符進(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;
}
資源來自:
首先導(dǎo)入各種需要的包:
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.*;
讀取圖片的方法如下:
Image[] array = new Image[10];
Image image = ImageIO.read(new File("d:\\source.gif"));//根據(jù)你實際情況改文件路徑吧
array[0] = image;
圖片讀出來了。
如果你有一個Image對象,想把它寫入文件可以這樣做:
BufferedImage image = ImageIO.read(new File("d:\\source.gif"));
//要想保存這個對象的話你要把image聲明為BufferedImage 類型
ImageIO.write(image, "png", new File("f:\\test.png"));
package com.he;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class CodeFact
extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
//設(shè)置頁面不緩存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 在內(nèi)存中創(chuàng)建圖象
int width = 60, height = 20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 獲取圖形上下文
Graphics g = image.getGraphics();
//生成隨機(jī)類
Random random = new Random();
// 設(shè)定背景色
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
//設(shè)定字體
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
//畫邊框
g.setColor(new Color(33,66,99));
g.drawRect(0,0,width-1,height-1);
// 隨機(jī)產(chǎn)生155條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測到
g.setColor(getRandColor(160, 200));
for (int i = 0; i 155; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
// 取隨機(jī)產(chǎn)生的認(rèn)證碼(4位數(shù)字)
String sRand = "";
for (int i = 0; i 4; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
// 將認(rèn)證碼顯示到圖象中
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110),
20 + random.nextInt(110))); //調(diào)用函數(shù)出來的顏色相同,可能是因為種子太接近,所以只能直接生成
g.drawString(rand, 13 * i + 6, 16);
}
// 將認(rèn)證碼存入SESSION
HttpSession session = request.getSession();
session.setAttribute("rand", sRand);
// 圖象生效
g.dispose();
// 輸出圖象到頁面
ImageIO.write(image, "JPEG", response.getOutputStream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//給定范圍獲得隨機(jī)顏色
private Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc 255) {
fc = 255;
}
if (bc 255) {
bc = 255;
}
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
}
你試試!!
import ja.io.*;
//獲取文件夾內(nèi)容
public class getthing
{
public static void main(String[] args) throws Exception
{
System.out.println(welstr);
listFile(new File("e:\\aa")); //想要搜索的路徑
}
public static void listFile(File file) throws Exception
{
if(file.isFile())
{
//輸出的是完整的文件夾內(nèi)文件的路徑
System.out.println("File :"+file.getAbsolutePath());
//01.jpg就是你要找的圖片
if (file.getAbsolutePath().endsWith("01.jpg"))
System.out.println("有搜索的圖片");
}
else
{
System.out.println("Dir :"+file.getAbsolutePath());
File[] files =file.listFiles();
for(int i=0;ifiles.length;i++)
{
listFile(files[i]);
System.out.println("回車");
}
}
}
}
1 首先確定你要搜索的目錄
2 要搜索的關(guān)鍵字 如“花”
3 只搜索圖片類型 .jpg .gif .png .bmp之類的
4 在文本框里獲得搜索的關(guān)鍵字
5 取得要搜索目錄下的所有圖片類型的名字
6 用關(guān)鍵字和取得的文件名一一進(jìn)行對比
7 若有關(guān)鍵字 記錄該圖片的名字
8 若都沒關(guān)鍵字 表示無該名字的圖片
9 空白區(qū)域你可以用一個窗體來表示
10 把搜索到的圖片都顯示在這個窗體。
新聞名稱:java圖片識別代碼 java實現(xiàn)圖片識別技術(shù)
文章轉(zhuǎn)載:http://chinadenli.net/article12/dogdjdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、面包屑導(dǎo)航、品牌網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、品牌網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)