学习需要,对手里人脸图片进行了剪切,将人脸图像铺满整张图片
FaceCrop.java
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50import 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.highgui.Highgui; import org.opencv.objdetect.CascadeClassifier; // //检测输入图像中的人脸,将最大的脸保存到指定的输出文件中 // public class FaceCrop { public static double calcArea(Rect rect) { return rect.width*rect.height; } public static String xmlfilePath="lbpcascade_frontalface.xml"; public static void faceCrop(String inputImageFilename,String outputImageFilename) { CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath); Mat image = Highgui.imread(inputImageFilename); // 检测人脸. // MatOfRect 是矩形容器. MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(image, faceDetections); //System.out.println(String.format("检测到%s张脸", faceDetections.toArray().length)); // 找出最大的1张脸 Rect maxRect=new Rect(0,0,0,0); for (Rect rect : faceDetections.toArray()) { if(calcArea(maxRect)<calcArea(rect)) { maxRect=rect; } //给脸上面画矩形 //Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0)); } if(calcArea(maxRect)>0){ //创建人脸拷贝区域 Mat roi_img = new Mat(image,maxRect); //创建临时的人脸拷贝图形 Mat tmp_img = new Mat(); //人脸拷贝 roi_img.copyTo(tmp_img); // 保存最大的1张脸 Highgui.imwrite(outputImageFilename, tmp_img); } } }
FileSelect.java
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35import java.io.File; /* * 用于检查裁剪后的人脸个数是否符合要求 * 1.少于指定书目则进行删除 * 2.删除内容包含该文件夹 */ public class FileSelect { public static int getFileCount(String path){ System.out.print(path); File file = new File(path); File files[] = file.listFiles(); System.out.println(" 1 "); System.out.println(":"+files.length); return files.length; } public static void DeleteDir(String path){ File file = new File(path); File files[] = file.listFiles(); for(File f : files){ f.delete(); } file.delete(); } //J:STUTest1131002 public static void getAllFileName(String path){ File file = new File(path); File files[] = file.listFiles(); for(File f : files){ System.out.println(f.getPath()); } } }
TestMain.java
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73import java.io.File; import org.opencv.core.Core; public class TestMain { static int i=0; public static void main(String[] args) { //faceCrop(); testDeleteFile(); } public static void testDeleteFile() { String path = "J:\STU\STU_CROP"; //要操作的文件夹 File files[] = new File(path).listFiles(); //获取文件目录下的所有子文件目录 /* * 可以使用getPath()获取文件的绝对路径 */ for(File f : files){ if(FileSelect.getFileCount(f.getPath())<10){ FileSelect.DeleteDir(f.getPath()); } System.out.println(f.getPath()+":delete success"); } /* for(long no=1131001;no<1131004;no++){ String abPath = path+"\0"+no; if(FileSelect.getFileCount(abPath)<11){ FileSelect.DeleteDir(abPath); } System.out.println(no+" delete success"); } */ } public static void faceCrop() { // 鍔犺浇OpenCV鍔ㄦ�閾炬帴搴� System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //FaceCrop.faceCrop("123.jpg","D:\faceDetection.jpg"); showDir(new File("J:\STU\STU_PIC")); System.out.println("Over"); } //循环遍历文件夹 public static void showDir(File dir) { File[] files = dir.listFiles(); for(File file : files) { if(file.isDirectory()) { System.out.println("=================i="+i); i++; try { showDir(file); } catch(Exception e) { //System.out.println(file+":Access Deny!!"); System.err.println(file+":Access Deny!!"); } } else{ String s1[] = file.toString().split("\\"); String filename = s1[s1.length-1]; String fileNo = s1[s1.length-2]; File file_new = new File("J:\STU\STU_CROP\"+fileNo); if(!file_new.exists()){ //File file_new = new File("D:\STU_CROP\"+i); file_new.mkdirs(); } FaceCrop.faceCrop(file.toString(),"J:\STU\STU_CROP\"+fileNo+"\"+filename); } } } }
最后
以上就是仁爱月饼最近收集整理的关于openCv+Java实现人脸剪切的全部内容,更多相关openCv+Java实现人脸剪切内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复