opencv3_java 检测图像人脸眼睛CascadeClassifier DetectEye

检测图像人脸眼睛CascadeClassifier DetectEye


package opencv_java_demo;

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

public class DetectEye {
	private static Mat dobj(Mat src){
		Mat dst=src.clone();
		
		CascadeClassifier objDetector=new CascadeClassifier("./data/haarcascades/haarcascade_eye.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.height), 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/eye.jpg", dst);
		}catch(Exception e){
			System.out.println("例外:"+e);
		}

	}

}




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