王兵 2 years ago
parent 1968b3bb27
commit 63632a821d

Binary file not shown.

After

Width:  |  Height:  |  Size: 278 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 291 KiB

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

@ -2,7 +2,6 @@ package base;
import org.opencv.core.Mat; import org.opencv.core.Mat;
import org.opencv.core.Rect; import org.opencv.core.Rect;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgcodecs.Imgcodecs;
import java.io.File; import java.io.File;

@ -0,0 +1,82 @@
package face;
import base.DllLoad;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfInt;
import org.opencv.core.MatOfRect;
import org.opencv.core.Rect;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class FaceDetector extends DllLoad {
public static void main(String[] args) {
File file1 = new File("imgs/face/face1.png");
File file2 = new File("imgs/face/face2.png");
Mat mat1 = Imgcodecs.imread(file1.getAbsolutePath());
Mat mat2 = Imgcodecs.imread(file2.getAbsolutePath());
List<Mat> mats1 = detectFace(mat1);
List<Mat> mats2 = detectFace(mat2);
for (Mat mat : mats1) {
// show(mat);
}
for (Mat mat : mats2) {
// show(mat);
}
Mat hist_1 = new Mat();
Mat hist_2 = new Mat();
MatOfFloat ranges = new MatOfFloat(0f, 256f);
MatOfInt histSize = new MatOfInt(25);
Imgproc.calcHist(Arrays.asList(mats1.get(0)), new MatOfInt(0),
new Mat(), hist_1, histSize, ranges);
Imgproc.calcHist(Arrays.asList(mats2.get(0)), new MatOfInt(0),
new Mat(), hist_2, histSize, ranges);
// 直方图相似度
double v1 = Imgproc.compareHist(hist_1, hist_2, Imgproc.CV_COMP_CORREL) * 100;
double v2 = Imgproc.compareHist(hist_1, hist_2, Imgproc.CV_COMP_INTERSECT);
System.out.print("相似度:");
// System.out.println((v1 + v2) / 2);
System.out.println(v1);
System.exit(0);
}
/**
*
*/
public static List<Mat> detectFace(Mat src) {
List<Mat> result = new ArrayList<>();
File haarcascade = new File("imgs/face/haarcascades/haarcascade_frontalface_alt.xml");
// 实例化级联分类器
CascadeClassifier faceDetector = new CascadeClassifier(haarcascade.getAbsolutePath());
// 进行人脸和眼睛的检测
// Gray: 要进行检测的人脸图像
// scaleFactor: 前后两次扫描中,搜索窗口的比例系数
double scaleFactor = 1.2;
// minneighbors目标至少被检测到minNeighbors次才会被认为是目标
int minNeighbors = 3;
// minsize和maxsize: 目标的最小尺寸和最大尺寸
Size minSize = new Size(32, 32);
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(src, faceDetections, scaleFactor, minNeighbors, 1, minSize);
for (Rect rect : faceDetections.toArray()) {
Mat mat = new Mat(src, rect);
result.add(mat);
}
return result;
}
}
Loading…
Cancel
Save

Powered by TurnKey Linux.