gpt4 book ai didi

java - 使用 Java 和 OpenCV 进行眼睛检测时需要帮助解决异常处理

转载 作者:行者123 更新时间:2023-12-01 12:37:20 28 4
gpt4 key购买 nike

我正在尝试使用 Java 中的 OpenCV 来检测眼睛,它运行一段时间绝对正常,但之后出现“ArrayIndexOutOfBound”异常。我正在 JPanel 中运行该程序,连续检测面部和眼睛。眼睛和脸部检测正常。所以我猜代码是对的,只需要改变一些东西来解决异常。如何摆脱它?

代码如下所示:

 public Mat eyeDetector(Mat image , MatOfRect facedetections)
{
// XML Files needed for Detection

CascadeClassifier eye_cascade= new CascadeClassifier("other/haarcascade_eye_tree_eyeglasses.xml");

MatOfRect eyedetections=new MatOfRect();

Rect[] facesArray = facedetections.toArray();

for (int i = 0; i < facesArray.length; i++)
{
Mat faceROI = image.submat(facesArray[i]);

//-- In each face, detect eyes

eye_cascade.detectMultiScale(faceROI, eyedetections);

Rect[] eyesArray = eyedetections.toArray();

System.out.println("Eyes Detected:" + eyesArray.length);

for (int j = 0; j < eyesArray.length; j++)
{
//System.out.println("for loop");

Point center1 = new Point(facesArray[i].x + eyesArray[i].x + eyesArray[i].width * 0.5, facesArray[i].y + eyesArray[i].y + eyesArray[i].height * 0.5);
int radius = (int) Math.round((eyesArray[i].width + eyesArray[i].height) * 0.25);
Core.circle(image, center1, radius, new Scalar(255, 0, 0), 4, 8, 0);
}
}
return image;
}

public static void main(String[] args)
{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
new FaceDetection().faceDetector();
}

//Face Detection Method
void faceDetector()
{
int x=0,y=0;
//int frameno = 0;
FacePanel panel = new FacePanel();
panel.initialize();
VideoCapture camera = new VideoCapture(0);

//Opens the camera. 0 for in built camera
Mat image = new Mat(); //creates matrix for image
BufferedImage buffimage = null;

//Checks whether camera is opened or not
if(camera.isOpened())
{
while(true)
{
//Captures the image from camera
if(camera.read(image))
{
//frameno++;
//if((frameno % 5) == 0)
//{
//Class for detecting face. Provided by OpenCV
CascadeClassifier FaceDetection = new CascadeClassifier("other/lbpcascade_frontalface.xml");

MatOfRect faceDetections = new MatOfRect();

//This Method detects faces
FaceDetection.detectMultiScale(image, faceDetections);

System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

//Draw a green rectangle around face
for (Rect rect : faceDetections.toArray())
{
x=rect.x;
y=rect.y;
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 255, 0));
}

//System.out.println(x+y);
Mat eye_detect = new Eye_Detection().eyeDetector(image, faceDetections);
buffimage = new Convert().matToBuff(eye_detect);
panel.display(buffimage , x , y);
//}
}
}

最佳答案

问题是在语句中抛出异常

for (int i = 0; i < facesArray.length; i++)
{
...
for (int j = 0; j < eyesArray.length; j++)
{
...
Point center1 = new Point(
facesArray[i].x + eyesArray[i].x + eyesArray[i].width * 0.5,
facesArray[i].y + eyesArray[i].y + eyesArray[i].height * 0.5);
...

用于eyesArray的下标应该是j而不是i

关于java - 使用 Java 和 OpenCV 进行眼睛检测时需要帮助解决异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25469605/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com