在之前博客中我们介绍了opencv4.4的安装: https://blog.csdn.net/datouniao1/article/details/108072623
我们利用opencv4.4中提供的算法实现一个人脸识别
先不说那么多直接上代码:
package org.opencv.test;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import org.junit.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.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
public class FaceDemo extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
protected Mat dst;
protected Mat truth;
protected Scalar colorBlack;
protected Scalar colorWhite;
protected static final int matSize = 10;
protected static final double EPS = 0.001;
protected static final double weakEPS = 0.5;
private BufferedImage mImg;
@Override
public void paint(Graphics g){
if(mImg!=null){
g.drawImage(mImg, 0, 0, mImg.getWidth(),mImg.getHeight(),this);
}
}
@Test
public void mainTest() {
try {
String opencvDllName = "D:\\opencv\\opencv-4.4.0\\build\\install\\java\\opencv_java440.dll";
System.load(opencvDllName);
// System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
} catch (SecurityException e) {
System.out.println(e.toString());
System.exit(-1);
} catch (UnsatisfiedLinkError e) {
System.out.println(e.toString());
System.exit(-1);
}
Core.setErrorVerbosity(false);
String pwd;
try {
pwd = new File(".").getCanonicalPath() + File.separator;
} catch (IOException e) {
System.out.println(e);
return;
}
OpenCVTestRunner.LENA_PATH = pwd + "res/drawable/OIP.jpg";
OpenCVTestRunner.CHESS_PATH = pwd + "res/drawable/chessboard.jpg";
OpenCVTestRunner.LBPCASCADE_FRONTALFACE_PATH = pwd + "res/raw/lbpcascade_frontalface.xml";
JFrame frame = new JFrame("camera");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
FaceDemo panel = new FaceDemo();
frame.setContentPane(panel);
frame.setVisible(true);
frame.setSize(500+frame.getInsets().left+frame.getInsets().right,500+frame.getInsets().top+frame.getInsets().bottom);
CascadeClassifier faceDetector = new CascadeClassifier("D:\\opencv\\opencv-4.4.0\\data\\haarcascades\\haarcascade_frontalface_alt2.xml");
//CascadeClassifier eyeDetector = new CascadeClassifier("D:\\opencv\\opencv-4.4.0\\data\\haarcascades\\lbpcascade_frontalface.xml");
MatOfRect faceDetections = new MatOfRect();
Mat lena = Imgcodecs.imread(OpenCVTestRunner.LENA_PATH);
faceDetector.detectMultiScale(lena, faceDetections);
// 转为图像显示
Rect[] rects = faceDetections.toArray();
if(rects != null && rects.length >= 1){
for (Rect rect : rects) {
//画矩形
Imgproc.rectangle(lena, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255), 2);
}
}
panel.mImg = panel.mat2BI(lena);
panel.repaint();
int i=1000;
while(i>100) {
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
i--;
}
}
private BufferedImage mat2BI(Mat mat) {
int dataSize = mat.cols() * mat.rows() * (int) mat.elemSize();
byte[] data = new byte[dataSize];
mat.get(0, 0, data);
int type = mat.channels() == 1 ? BufferedImage.TYPE_BYTE_GRAY : BufferedImage.TYPE_3BYTE_BGR;
if (type == BufferedImage.TYPE_3BYTE_BGR) {
for (int i = 0; i < dataSize; i += 3) {
byte blue = data[i + 0];
data[i + 0] = data[i + 2];
data[i + 2] = blue;
}
}
BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
image.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), data);
return image;
}
}
程序运行效果:
希望对你有所帮助