gpt4 book ai didi

java - 使用具有大小的 Java OpenCV 库时出错

转载 作者:行者123 更新时间:2023-12-01 11:40:41 24 4
gpt4 key购买 nike

我最近在 eclipse 上使用 java 中的 openCV 工作,并且正在开发一个眼动追踪软件,我正在使用其他人创建的基本代码并计划对其进行调整,但我遇到了几行代码的错误,并且不明白为什么。这是全类同学

package have;

import java.awt.*;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
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.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

class FacePanel extends JPanel{
private static final long serialVersionUID = 1L;
private BufferedImage image;
// Create a constructor method
public FacePanel(){
super();
}
/*
* Converts/writes a Mat into a BufferedImage.
*
* @param matrix Mat of type CV_8UC3 or CV_8UC1
* @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY
*/
public boolean matToBufferedImage(Mat matrix) {
MatOfByte mb=new MatOfByte();
Highgui.imencode(".jpg", matrix, mb);
try {
this.image = ImageIO.read(new ByteArrayInputStream(mb.toArray()));
} catch (IOException e) {
e.printStackTrace();
return false; // Error
}
return true; // Successful
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if (this.image==null) return;
g.drawImage(this.image,10,10,this.image.getWidth(),this.image.getHeight(), null);
}

}
class FaceDetector {
private CascadeClassifier face_cascade;
// Create a constructor method
public FaceDetector(){
// face_cascade=new CascadeClassifier("./cascades/lbpcascade_frontalface_alt.xml");
//..didn't have not much luck with the lbp

face_cascade=new CascadeClassifier("./cascades/haarcascade_frontalface_alt.xml");
if(face_cascade.empty())
{
System.out.println("--(!)Error loading A\n");
return;
}
else
{
System.out.println("Face classifier loooaaaaaded up");
}
}
public Mat detect(Mat inputframe){
Mat mRgba=new Mat();
Mat mGrey=new Mat();
MatOfRect faces = new MatOfRect();
inputframe.copyTo(mRgba);
inputframe.copyTo(mGrey);
Imgproc.cvtColor( mRgba, mGrey, Imgproc.COLOR_BGR2GRAY);
Imgproc.equalizeHist( mGrey, mGrey );
face_cascade.detectMultiScale(mGrey, faces);
System.out.println(String.format("Detected %s faces", faces.toArray().length));
for(Rect rect:faces.toArray())
{
Point center= new Point(rect.x + rect.width*0.5, rect.y + rect.height*0.5 );
//draw a blue eclipse around face

错误从这里开始,错误代码为:“构造函数 Size(double, double, int, int, int, Scalar) 未定义”

          Size s = new Size( rect.width*0.5, rect.height*0.5), 0, 0, 360, new Scalar( 0, 0, 255 )

然后在 ellipse 处出现错误:“Core 类型中的方法 ellipse(Mat, RotatedRect, Scalar, int, int) 不适用于参数 (Mat, Point, Size, int, int, int) )

           Core.ellipse( mRgba, center,s , 4, 8, 0 );  
}
return mRgba;
}

}
公开课眼睛{

public static void main(String arg[]) throws InterruptedException{  
// Load the native library.
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
//or ... System.loadLibrary("opencv_java244");

//make the JFrame
JFrame frame = new JFrame("WebCam Capture - Face detection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

FaceDetector faceDetector=new FaceDetector();
FacePanel facePanel = new FacePanel();
frame.setSize(400,400); //give the frame some arbitrary size
frame.setBackground(Color.BLUE);
frame.add(facePanel,BorderLayout.CENTER);
frame.setVisible(true);

//Open and Read from the video stream
Mat webcam_image=new Mat();
VideoCapture webCam =new VideoCapture(0);

if( webCam.isOpened())
{
Thread.sleep(500); /// This one-time delay allows the Webcam to initialize itself
while( true )
{
webCam.read(webcam_image);
if( !webcam_image.empty() )
{
Thread.sleep(200); /// This delay eases the computational load .. with little performance leakage
frame.setSize(webcam_image.width()+40,webcam_image.height()+60);
//Apply the classifier to the captured image
webcam_image=faceDetector.detect(webcam_image);
//Display the image
facePanel.matToBufferedImage(webcam_image);
facePanel.repaint();
}
else
{
System.out.println(" --(!) No captured frame from webcam !");
break;
}
}
}
webCam.release(); //release the webcam

} //end main

}

任何和所有的帮助将不胜感激

最佳答案

如果您查看 OpenCV Java API,您可以看到已定义的 Size 构造函数以及需要哪些参数:

http://docs.opencv.org/java/org/opencv/core/Size.html

一个尺寸包含两个值:高度和宽度。

所以你的代码:

 Size s = new Size( rect.width*0.5, rect.height*0.5), 0, 0, 360, new Scalar( 0, 0, 255 )

应该是:

Size s = new Size( rect.width*0.5, rect.height*0.5);

这将创建一个大小为矩形宽度一半和矩形高度一半的大小。

<小时/>

再次介绍 Ellipse 方法:

http://docs.opencv.org/java/org/opencv/core/Core.html

有很多变体,但看起来您希望使用以下内容:

public static void ellipse(Mat img,
Point center,
Size axes,
double angle,
double startAngle,
double endAngle,
Scalar color)

所以你的代码:

Core.ellipse( mRgba, center,s , 4, 8, 0 ); 

可能只是缺少颜色标量:

 Core.ellipse( mRgba, center,s , 4, 8, 0, new Scalar(B,G,R));

其中 B、G、R 是每个颜色 channel 的 double 值。

关于java - 使用具有大小的 Java OpenCV 库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29552579/

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