图像识别的java代码 图像识别开源代码

如何开发Java动态人脸识别

1.环境搭建

公司主营业务:成都做网站、成都网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出青羊免费做网站回馈大家。

整个项目的结构图

2.编写DetectFaceDemo.java,代码如下:

[java] view plaincopy

package com.njupt.zhb.test;

import org.opencv.core.Core;

import org.opencv.core.Mat;

import org.opencv.core.MatOfRect;

import org.opencv.core.Point;

import org.opencv.core.Rect;

import org.opencv.core.Scalar;

import org.opencv.highgui.Highgui;

import org.opencv.objdetect.CascadeClassifier;

//

// Detects faces in an image, draws boxes around them, and writes the results

// to "faceDetection.png".

//

public class DetectFaceDemo {

public void run() {

System.out.println("\nRunning DetectFaceDemo");

System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());

// Create a face detector from the cascade file in the resources

// directory.

//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());

//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());

//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误

/*

* Detected 0 faces Writing faceDetection.png libpng warning: Image

* width is zero in IHDR libpng warning: Image height is zero in IHDR

* libpng error: Invalid IHDR data

*/

//因此,我们将第一个字符去掉

String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);

CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);

Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));

// Detect faces in the image.

// MatOfRect is a special container class for Rect.

MatOfRect faceDetections = new MatOfRect();

faceDetector.detectMultiScale(image, faceDetections);

System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

// Draw a bounding box around each face.

for (Rect rect : faceDetections.toArray()) {

Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));

}

// Save the visualized detection.

String filename = "faceDetection.png";

System.out.println(String.format("Writing %s", filename));

Highgui.imwrite(filename, image);

}

}

3.编写测试类:

[java] view plaincopy

package com.njupt.zhb.test;

public class TestMain {

public static void main(String[] args) {

System.out.println("Hello, OpenCV");

// Load the native library.

System.loadLibrary("opencv_java246");

new DetectFaceDemo().run();

}

}

//运行结果:

//Hello, OpenCV

//

//Running DetectFaceDemo

///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml

//Detected 8 faces

//Writing faceDetection.png

java 实现图片的文字识别

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

步骤

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

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

代码

/*

* @(#)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实现屏幕找图功能

测试代码

[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));//对屏幕指定范围进行截图,保存到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

//图片识别工具类

ImageCognition ic = new ImageCognition();

ListCoordBean list = ic.imageSearch(screenImg, searchImg, sim);

for (CoordBean coordBean : list) {

System.out.println("找到图片,坐标是" + coordBean.getX() + ","

+ coordBean.getY());

//标注找到的图的位置

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坐标

*

* @return x坐标

*/

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

/**

* 获取y坐标

*

* @return

*/

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

}

RgbImageComparerBean

package cn.xt.imgCongnition;

/**

* RGB 相关,图片相似度计算时使用

*

*/

public class RgbImageComparerBean {

/****** 颜色值数组,第一纬度为x坐标,第二纬度为y坐标 ******/

private int colorArray[][];

/*** 是否忽略此点,若为true,则不纳入比较的像素行列。 ***/

private boolean ignorePx[][];

/**** 图片的宽高 ****/

private int imgWidth;

private int imgHeight;

// 图片的像素总数

private int pxCount;

/**

* 获取图像的二维数组组成

*

* @return 图像的二维数组

*/

public int[][] getColorArray() {

return colorArray;

}

/**

* 要对比的像素总数,会自动筛选掉不对比的颜色

*

* @param pxCount

*/

public void setPxCount(int pxCount) {

this.pxCount = pxCount;

}

/**

* 设置颜色二维数组

*

* @param colorArray

* 颜色二维数组,一维为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;

}

/**

* 是否忽略此点,若为true,则不纳入像素比较行列。

*

* @return 具体x,y坐标的那个像素点

*/

public boolean[][] getIgnorePx() {

return ignorePx;

}

/**

* 是否忽略此点,若为true,则不纳入像素比较行列。

*

* @param ignorePx

* 具体x,y坐标的那个像素点

*/

public void setIgnorePx(boolean[][] ignorePx) {

this.ignorePx = ignorePx;

}

/**

* 获取图像的宽度

*

* @return 图像宽度

*/

public int getImgWidth() {

return imgWidth;

}

/**

* 获取图像的高度

*

* @return 图像高度

*/

public int getImgHeight() {

return imgHeight;

}

/**

* 获取图像里像素的总数

*

* @return

*/

public int getPxCount() {

return pxCount;

}

}


网站名称:图像识别的java代码 图像识别开源代码
文章转载:http://hbruida.cn/article/hpcdgh.html