1.什么是OpenCV(来自Baidu百科)
OpenCV是Intel资助的开源计算机视觉库。它由一系列 C 函数和少量 C++ 类构成,实现了图像处理和计算机视觉方面的很多通用算法。
OpenCV 拥有包括 300 多个C函数的跨平台的中、高层 API。它不依赖于其它的外部库——尽管也可以使用某些外部库。OpenCV 对非商业应用和商业应用都是免费(FREE)的。OpenCV 为Intel® Integrated Performance Primitives (IPP) 提供了透明接口。 这意味着如果有为特定处理器优化的的 IPP 库, OpenCV 将在运行时自动加载这些库。
2. JAVA中如何调用(参考JNI2OpenCV)
JNI2OpenCV将OpenCV的C/C++程序代码编译成动态链接库文件,Java便可以利用本地方法调用OpenCV的计算机视觉库中的一系列的函数和算法。
JNI2OpenCV中提供了FaceDetection.java的测试程序,程序代码如下:
//--以下是程序代码--
class JNIOpenCV
{
static
{
System.loadLibrary("JNI2OpenCV");
}
//加载动态链接库
public native int[] detectFace(int minFaceWidth, int minFaceHeight, String cascade, String filename);
//声明本地方法
}
public class FaceDetection
{
private JNIOpenCV myJNIOpenCV;
private FaceDetection myFaceDetection;
public FaceDetection()
{
myJNIOpenCV = new JNIOpenCV();
String filename = "lena.jpg";//OpenCV的官方测试图片(人脸识别)
String cascade = "haarcascade_frontalface_alt.xml";
int[] detectedFaces = myJNIOpenCV.detectFace(40, 40, cascade, filename);
int numFaces = detectedFaces.length / 4;
System.out.println("numFaces = " + numFaces);
for (int i = 0; i < numFaces; i++)
{
System.out.println("Face " + i + ": " + detectedFaces[4 * i + 0] + " " + detectedFaces[4 * i + 1] + " " + detectedFaces[4 * i + 2] + " " + detectedFaces[4 * i + 3]);
}
}
public static void main(String args[])
{
FaceDetection myFaceDetection = new FaceDetection();
}
}
程序运行之后,会在命令行中打印出含有多少个Faces,以及各个Faces的位置等信息。
3.将人脸位置标志出来
为了更直观的呈现人脸识别的结果,笔者在图片上直接用矩形框标志出具体的位置。程序分成两部分,人脸识别部分(FaceDetection)和图片绘图(DrawInImg)部分。具体程序如下:
FaceDetection.java
//-------------------------------------
class JNIOpenCV {
static {
System.loadLibrary("JNI2OpenCV");
}
public native int[] detectFace(int minFaceWidth, int minFaceHeight, String cascade, String filename);
}
public class FaceDetection {
private JNIOpenCV myJNIOpenCV;
private FaceDetection myFaceDetection;
public FaceDetection() {
myJNIOpenCV = new JNIOpenCV();
String inputFilename = "4.jpg";
String outputFilename = "out.jpg";
String cascade = "haarcascade_frontalface_alt.xml";
int[] detectedFaces = myJNIOpenCV.detectFace(10 , 10, cascade, inputFilename);
int numFaces = detectedFaces.length / 4;
System.out.println("numFaces = " + numFaces);
for (int i = 0; i < numFaces; i++) {
System.out.println("Face " + i + ": " + detectedFaces[4 * i + 0] + " " + detectedFaces[4 * i + 1] + " " + detectedFaces[4 * i + 2] + " " + detectedFaces[4 * i + 3]);
}
int[][] RectInt=new int[numFaces][4];
for (int i = 0; i < numFaces; i++)
{
RectInt[i][0]=detectedFaces[4 * i + 0];
RectInt[i][1]=detectedFaces[4 * i + 1];
RectInt[i][2]=detectedFaces[4 * i + 2];
RectInt[i][3]=detectedFaces[4 * i + 3];
}
if(new DrawInImg(inputFilename,outputFilename,RectInt).DrawRect())
System.out.println("File create success ! ");
else
System.out.println("File create error ! ");
}
public static void main(String args[]) {
FaceDetection myFaceDetection = new FaceDetection();
}
}
DrawInImg.java
//----------------------------
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
public class DrawInImg
{
private boolean isDone=false;
private String inputFileName="";
private String outputFileName="";
private int[][] RectInt;
public DrawInImg(String inputFileName,String outputFileName,int[][] RectInt)
{
this.inputFileName=inputFileName;
this.outputFileName=outputFileName;
this.RectInt=RectInt;
}
public boolean DrawRect()
{
try
{
FileInputStream fis = new FileInputStream(inputFileName);
FileOutputStream fos = new FileOutputStream(outputFileName);
BufferedImage img = ImageIO.read(fis);
Graphics g = img.getGraphics();
g.setColor(Color.GREEN);
for(int i=0;i<RectInt.length;i++)
{
for(int j=0;j<RectInt[i].length;j++)
{
g.drawRect(RectInt[i][0],RectInt[i][1],RectInt[i][2],RectInt[i][3]);
// System.out.print(RectInt[i][j]+" ");
}
System.out.print("\r\n");
}
img.flush();
g.dispose();
ImageIO.write(img, "JPG", fos);
while(true)
{
if(new File(outputFileName).exists())
{
this.isDone=true;
break;
}
}
}
catch (IOException ioe)
{
ioe=null;
}
return this.isDone;
}
}
运行的效果如下: (注意看里面的白框)