我试图检测眼睛和嘴巴(图片中只有一个人),但它没有成功,我有很多圈子或指向左眼。 愿任何人帮助我吗?我尝试了不同的图像。
这是我的代码:
import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDouble;
import org.opencv.core.MatOfInt;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.ml.CvANN_MLP;
import org.opencv.objdetect.CascadeClassifier;
public class faceeyes extends Application{
private Triangle tr;
private File file;
Mat mat;
public Mat eyeDetector(Mat image , MatOfRect facedetections){
// XML Files needed for Detection
CascadeClassifier eye_cascade= new CascadeClassifier(
"C:\\Users\\...\\workspace_eclipse\\...\\src\\haarcascade_eye.xml");
CascadeClassifier mouth_cascade= new CascadeClassifier
("C:\\Users\\...\\workspace_eclipse\\...\\src\\haarcascade_mcs_mouth.xml");
MatOfRect eyedetections=new MatOfRect();
MatOfRect mouthDetections=new MatOfRect();
Rect[] facesArray = facedetections.toArray();
for (int i = 0; i < facesArray.length; i++){
Mat faceROI = image.submat(facesArray[i]);
eye_cascade.detectMultiScale(faceROI, eyedetections, 1.1, 1, 1, new Size(30,30), new Size());
Rect[] eyesArray = eyedetections.toArray();
System.out.println("Eyes Detected:" + eyesArray.length);
for (int j = 0; j < 3; j++){
Point center1 = new Point(facesArray[i].x + eyesArray[j].x + eyesArray[j].width * 0.5,
facesArray[i].y + eyesArray[j].y + eyesArray[j].height * 0.5);
int radius = (int) Math.round( 5 );
Core.circle(image, center1, radius, new Scalar(255, 0, 0), 4, 8, 0);
}
mouth_cascade.detectMultiScale(faceROI, mouthDetections, 1.1, 1, 1, new Size(30,30), new Size());
Rect[] mouthArray = mouthDetections.toArray();
for (int j = 0; j < 1; j++){
Point center1 = new Point(facesArray[i].x + mouthArray[j].x + mouthArray[j].width * 0.5,
facesArray[i].y + mouthArray[j].y + mouthArray[j].height * 0.5);
int radius = (int) Math.round( 3 );
Core.circle(image, center1, radius, new Scalar(255, 0, 0), 4, 8, 0);
}
}
return image;
}
public void start(Stage primaryStage){
String url="image/pic2.png";
File file = new File(url);
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
CascadeClassifier faceDetector
= new CascadeClassifier("C:\\Users\\...\\workspace_eclipse\\...\\src\\haarcascade_frontalface_alt.xml");
System.out.println(file.getPath());
Mat imageMat = Highgui
.imread(file.getPath());
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(imageMat, faceDetections, 1.1, 1, 1, new Size(30,30), new Size());
Rect[] rect2=faceDetections.toArray();
rect2.toString();
mat = eyeDetector(imageMat, faceDetections);
String eyes;
eyes = "image/Eye.png";
Highgui.imwrite(eyes, mat);
File filenew = new File(eyes);
BorderPane mainPane = new BorderPane();
mainPane.setTop(new ImageView(new Image("file:"+filenew.toString())));
Scene scene = new Scene(mainPane,6,50);
primaryStage.setScene(scene);
primaryStage.setMaximized(true);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}