opencv3_java 图像检测人脸图像CascadeClassifier DetectObjects lbpcascade_frontalface

图像检测人脸图像CascadeClassifier DetectObjects lbpcascade_frontalface


package opencv_java_demo;

import org.opencv.core.*;
import org.opencv.imgcodecs.*;
import org.opencv.objdetect.*;
import org.opencv.imgproc.*;

public class DetectObjects {
	private static Mat dobj(Mat src){
		Mat dst=src.clone();
		
		CascadeClassifier objDetector=new CascadeClassifier("./data/lbpcascades/lbpcascade_frontalface.xml");
		
		MatOfRect objDetections=new MatOfRect();
		
		objDetector.detectMultiScale(dst, objDetections);
		
		if(objDetections.toArray().length<=0){
			return src;
		}
		for(Rect rect:objDetections.toArray()){
			Imgproc.rectangle(dst, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.width), new Scalar(0,0,255),2);
		}
		return dst;
	}

	public static void main(String[] args) {
		try{
			System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
			
			Mat src=Imgcodecs.imread("./images/lenna.jpg");
			if(src.empty()){
				throw new Exception("no file");
			}
			Mat dst=dobj(src);
			Imgcodecs.imwrite("./images/dobj.jpg", dst);
		}catch(Exception e){
			System.out.println("例外:"+e);
		}
	}
}




你可能感兴趣的:(java,opencv3)