识别处理图像中人脸,人体是图像识别的一个重要分支,在很多场合都需要对人进行查找和处理,在拍照,自动驾驶,机器人,医学,安防等上都有广泛 的用途。opencv 有众多的级联分类器,可以进行简单的人脸,眼,鼻子,嘴,上体,全身,腿的分类。这些分类器还可以通过训练或者组合进一步强化识别能力,从而把几个弱分类器变成一个强分类器使用。
分类器都是一个概率问题,精确度高了,会有遗露,精确度低了,会有错选,通过更多的训练可以使用识别库日渐完善。使用OPENCV自带的训练有很多不尽如人意,不过在特定情况下或者某些要求不高的场合,使用一些手段还是可以使用的的。在查找人眼的过程中,会找到很多非人眼的信息,和人脸结合,人眼查找就精确了很多。
import org.opencv.core.*;
import org.opencv.imgcodecs.*;
import org.opencv.objdetect.*;
import org.opencv.imgproc.*;
public class DetectBody {
public static void main(String[] args) {
try {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat src = Imgcodecs.imread("E:/work/qqq/b5.jpg");
Imgcodecs.imwrite("E:/work/qqq/hh81.jpg", getUpperBody(src));
Imgcodecs.imwrite("E:/work/qqq/hh82.jpg", getLefteye(src));
Imgcodecs.imwrite("E:/work/qqq/hh83.jpg", getRighteye(src));
// Imgcodecs.imwrite("E:/work/qqq/hh8.jpg", getLeftear(src));
// Imgcodecs.imwrite("E:/work/qqq/hh8.jpg", getRightear(src));
Imgcodecs.imwrite("E:/work/qqq/hh84.jpg", getMouth(src));
Imgcodecs.imwrite("E:/work/qqq/hh85.jpg", getNose(src));
// Imgcodecs.imwrite("E:/work/qqq/hh8.jpg", getSmile(src));
// Imgcodecs.imwrite("E:/work/qqq/hh8.jpg", getLowerBody(src));
// Imgcodecs.imwrite("E:/work/qqq/hh8.jpg", getFullBody(src));
Imgcodecs.imwrite("E:/work/qqq/hh86.jpg", getFace(src));
Imgcodecs.imwrite("E:/work/qqq/hh87.jpg", getProfileFace(getFace(src)));
CascadeClassifier faceDetector = new CascadeClassifier("./resource/haarcascade_frontalface_alt2.xml");
MatOfRect objDetections2 = new MatOfRect();
faceDetector.detectMultiScale(src, objDetections2);
for (Rect rect : objDetections2.toArray()) {
Imgproc.rectangle(src, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 0, 255), 2);
Mat s = src.submat(rect);
getLefteye(s).copyTo(s);
}
Imgcodecs.imwrite("E:/work/qqq/hh88.jpg", src);
} catch (Exception e) {
System.out.println("例外:" + e);
}
}
public static Mat getUpperBody(Mat src) {
Mat result = src.clone();
if (src.cols() > 1000 || src.rows() > 1000) {
Imgproc.resize(src, result, new Size(src.cols() / 3, src.rows() / 3));
}
CascadeClassifier faceDetector = new CascadeClassifier("./resource/haarcascade_mcs_upperbody.xml");
MatOfRect objDetections = new MatOfRect();
faceDetector.detectMultiScale(result, objDetections);
for (Rect rect : objDetections.toArray()) {
Imgproc.rectangle(result, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 0, 255), 2);
}
return result;
}
public static Mat getLefteye(Mat src) {
Mat result = src.clone();
if (src.cols() > 1000 || src.rows() > 1000) {
Imgproc.resize(src, result, new Size(src.cols() / 3, src.rows() / 3));
}
CascadeClassifier faceDetector = new CascadeClassifier("./resource/haarcascade_mcs_lefteye.xml");
MatOfRect objDetections = new MatOfRect();
faceDetector.detectMultiScale(result, objDetections);
for (Rect rect : objDetections.toArray()) {
Imgproc.rectangle(result, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 0, 255), 2);
}
return result;
}
public static Mat getRighteye(Mat src) {
Mat result = src.clone();
if (src.cols() > 1000 || src.rows() > 1000) {
Imgproc.resize(src, result, new Size(src.cols() / 3, src.rows() / 3));
}
CascadeClassifier faceDetector = new CascadeClassifier("./resource/haarcascade_mcs_righteye.xml");
MatOfRect objDetections = new MatOfRect();
faceDetector.detectMultiScale(result, objDetections);
for (Rect rect : objDetections.toArray()) {
Imgproc.rectangle(result, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 0, 255), 2);
}
return result;
}
public static Mat getLeftear(Mat src) {
Mat result = src.clone();
if (src.cols() > 1000 || src.rows() > 1000) {
Imgproc.resize(src, result, new Size(src.cols() / 3, src.rows() / 3));
}
CascadeClassifier faceDetector = new CascadeClassifier("./resource/haarcascade_mcs_leftear.xml");
MatOfRect objDetections = new MatOfRect();
faceDetector.detectMultiScale(result, objDetections);
for (Rect rect : objDetections.toArray()) {
Imgproc.rectangle(result, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 0, 255), 2);
}
return result;
}
public static Mat getRightear(Mat src) {
Mat result = src.clone();
if (src.cols() > 1000 || src.rows() > 1000) {
Imgproc.resize(src, result, new Size(src.cols() / 3, src.rows() / 3));
}
CascadeClassifier faceDetector = new CascadeClassifier("./resource/haarcascade_mcs_rightear.xml");
MatOfRect objDetections = new MatOfRect();
faceDetector.detectMultiScale(result, objDetections);
for (Rect rect : objDetections.toArray()) {
Imgproc.rectangle(result, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 0, 255), 2);
}
return result;
}
public static Mat getMouth(Mat src) {
Mat result = src.clone();
if (src.cols() > 1000 || src.rows() > 1000) {
Imgproc.resize(src, result, new Size(src.cols() / 3, src.rows() / 3));
}
CascadeClassifier faceDetector = new CascadeClassifier("./resource/haarcascade_mcs_mouth.xml");
MatOfRect objDetections = new MatOfRect();
faceDetector.detectMultiScale(result, objDetections);
for (Rect rect : objDetections.toArray()) {
Imgproc.rectangle(result, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 0, 255), 2);
}
return result;
}
public static Mat getNose(Mat src) {
Mat result = src.clone();
if (src.cols() > 1000 || src.rows() > 1000) {
Imgproc.resize(src, result, new Size(src.cols() / 3, src.rows() / 3));
}
CascadeClassifier faceDetector = new CascadeClassifier("./resource/haarcascade_mcs_nose.xml");
MatOfRect objDetections = new MatOfRect();
faceDetector.detectMultiScale(result, objDetections);
for (Rect rect : objDetections.toArray()) {
Imgproc.rectangle(result, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 0, 255), 2);
}
return result;
}
public static Mat getSmile(Mat src) {
Mat result = src.clone();
if (src.cols() > 1000 || src.rows() > 1000) {
Imgproc.resize(src, result, new Size(src.cols() / 3, src.rows() / 3));
}
CascadeClassifier faceDetector = new CascadeClassifier("./resource/haarcascade_smile.xml");
MatOfRect objDetections = new MatOfRect();
faceDetector.detectMultiScale(result, objDetections);
for (Rect rect : objDetections.toArray()) {
Imgproc.rectangle(result, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 0, 255), 2);
}
return result;
}
public static Mat getLowerBody(Mat src) {
Mat result = src.clone();
if (src.cols() > 1000 || src.rows() > 1000) {
Imgproc.resize(src, result, new Size(src.cols() / 3, src.rows() / 3));
}
CascadeClassifier faceDetector = new CascadeClassifier("./resource/haarcascade_lowerbody.xml");
MatOfRect objDetections = new MatOfRect();
faceDetector.detectMultiScale(result, objDetections);
for (Rect rect : objDetections.toArray()) {
Imgproc.rectangle(result, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 0, 255), 2);
}
return result;
}
public static Mat getFullBody(Mat src) {
Mat result = src.clone();
if (src.cols() > 1000 || src.rows() > 1000) {
Imgproc.resize(src, result, new Size(src.cols() / 3, src.rows() / 3));
}
CascadeClassifier faceDetector = new CascadeClassifier("./resource/haarcascade_fullbody.xml");
MatOfRect objDetections = new MatOfRect();
faceDetector.detectMultiScale(result, objDetections);
for (Rect rect : objDetections.toArray()) {
Imgproc.rectangle(result, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 0, 255), 2);
}
return result;
}
public static Mat getFace(Mat src) {
Mat result = src.clone();
if (src.cols() > 1000 || src.rows() > 1000) {
Imgproc.resize(src, result, new Size(src.cols() / 3, src.rows() / 3));
}
CascadeClassifier faceDetector = new CascadeClassifier("./resource/haarcascade_frontalface_alt2.xml");
MatOfRect objDetections = new MatOfRect();
faceDetector.detectMultiScale(result, objDetections);
for (Rect rect : objDetections.toArray()) {
Imgproc.rectangle(result, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 0, 255), 2);
}
return result;
}
public static Mat getProfileFace(Mat src) {
Mat result = src.clone();
if (src.cols() > 1000 || src.rows() > 1000) {
Imgproc.resize(src, result, new Size(src.cols() / 3, src.rows() / 3));
}
CascadeClassifier faceDetector = new CascadeClassifier("./resource/haarcascade_profileface.xml");
MatOfRect objDetections = new MatOfRect();
faceDetector.detectMultiScale(result, objDetections);
for (Rect rect : objDetections.toArray()) {
Imgproc.rectangle(result, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 0, 255), 2);
}
return result;
}
}
上半身的查找,感觉还是比较准确的,特别是在自拍相机中。由于距离人数固定,可以通过大小和人脸结合,很容易过滤掉不准确的分类
对人眼的查找,单纯查找人眼,这是左眼,有很多误选,最后结合人脸,就准确了。
右眼
嘴的选择,也需要结合人脸查找
鼻子
人脸
结合人脸的眼睛选择就变得准确了