opencv人臉識別
1.官網下載opencv:https://opencv.org/releases/

2.雙擊打開生成檔案夾opencv,

3.創建專案
4.將opencv-454.jar和opencv_java454.dll復制到專案中



5.專案中引入jar

6.將人臉識別的類別庫映入專案


7.測驗代碼
package liu.com.dome;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import java.util.Arrays;
public class FaceAi {
// 初始化人臉探測器
static CascadeClassifier faceDetector;
static int i = 0;
static {
String path=System.getProperty("user.dir")+"/opencv/opencv_java454.dll";
System.load(path);
faceDetector = new CascadeClassifier(System.getProperty("user.dir")+"/opencv/haarcascade_frontalface_alt.xml");
}
public static void main(String[] args) {
//人臉識別后摳圖
// getFace("F:/123.png","123_1.jpeg");
//人臉識別后摳圖
// getFace("F:/1234.jpg","1234_1.jpeg");
//相似度對比
double v = compare_image("F:/0123_1.jpeg", "F:/01234_1.jpeg");
}
/**
* 人臉識別
* @param imagePath 圖片路徑
* @param outFile 處理后的圖片路徑
*/
public static void getFace(String imagePath,String outFile) {
// 原始影像
Mat image = Imgcodecs.imread(imagePath);
System.out.println("圖片長-寬:"+image.rows()+"-"+image.cols());
//創建一個矩形框住人臉
MatOfRect matOfRect = new MatOfRect();
//執行人臉檢測
faceDetector.detectMultiScale(image,matOfRect);
//識別出人臉
Rect[] rects = matOfRect.toArray();
System.out.println("識別出人臉的數量"+rects.length);
for (int i=0;i<rects.length;i++){
Imgproc.rectangle(image,
new Point(rects[i].x,rects[i].y),//框的起點
new Point(rects[i].x+rects[i].width,rects[i].y+rects[i].height),//框的寬,框的高
new Scalar(255,255,255)//框的顏色
);
//保存識別后的圖片
Imgcodecs.imwrite("F:/"+outFile, image);
//把檢測到的人臉摳出保存
imageCut(imagePath,"F:/"+i+outFile,rects[i].x,rects[i].y,rects[i].width,rects[i].height);
}
System.out.println("成功檢測到人臉在"+"F:/"+outFile+"路徑上");
}
/**
* 裁剪人臉
*
* @param imagePath 圖片路徑
* @param outFile 摳圖后的圖片路徑
* @param posX 摳圖坐標
* @param posY
* @param width 摳圖的寬
* @param height 摳圖的高
*/
public static void imageCut(String imagePath, String outFile, int posX, int posY, int width, int height) {
// 原始影像
Mat image = Imgcodecs.imread(imagePath);
// 截取的區域:引數,坐標X,坐標Y,截圖寬度,截圖長度
Rect rect = new Rect(posX, posY, width, height);
Mat sub = image.submat(rect);
Mat mat = new Mat();
Size size = new Size(300, 300); //指定摳出來的圖示尺寸
Imgproc.resize(sub, mat, size); // 將人臉進行截圖并保存
Imgcodecs.imwrite(outFile, mat);
System.out.println(String.format("圖片裁切成功,裁切后圖片檔案為", outFile));
}
/**
* 人臉比對
*
* @param img_1
* @param img_2
* @return
*/
public static double compare_image(String img_1, String img_2) {
Mat mat_1 = conv_Mat(img_1);
Mat mat_2 = conv_Mat(img_2);
Mat hist_1 = new Mat();
Mat hist_2 = new Mat();
//顏色范圍
MatOfFloat ranges = new MatOfFloat(0f, 256f);
//直方圖大小, 越大匹配越精確 (越慢)
MatOfInt histSize = new MatOfInt(1000);
Imgproc.calcHist(Arrays.asList(mat_1), new MatOfInt(0), new Mat(), hist_1, histSize, ranges);
Imgproc.calcHist(Arrays.asList(mat_2), new MatOfInt(0), new Mat(), hist_2, histSize, ranges);
// CORREL 相關系數
double res = Imgproc.compareHist(hist_1, hist_2, Imgproc.CV_COMP_CORREL);
return res;
}
/**
* 灰度化人臉
*
* @param img
* @return
*/
public static Mat conv_Mat(String img) {
Mat image0 = Imgcodecs.imread(img);
Mat image1 = new Mat();
// 灰度化
Imgproc.cvtColor(image0, image1, Imgproc.COLOR_BGR2GRAY);
// 探測人臉
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image1, faceDetections);
// rect中人臉圖片的范圍
for (Rect rect : faceDetections.toArray()) {
Mat face = new Mat(image1, rect);
return face;
}
return null;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/333520.html
標籤:其他
上一篇:漢諾塔 - Java
