Opencv java中的CascadeClassifier在网络摄像头中找不到人脸 我试图把C++从Java翻译成java。在C++中工作良好。这也很好用
但是翻译根本就没有检测到人脸。我没有明显的错误。我可以看到来自网络摄像头的视频输入处理(灰色/直方图…)和视频显示。级联加载不会给出错误。但是这个调用不会返回任何面孔。。。因此,您可能可以跳过所有代码,直接转到我的CascadeClassifier调用,转到publicmatdetect(matinputframe)。由于我不熟悉Java和OpenCV,所以我粘贴了其余部分(我删除了我认为可能不重要的内容),以防万一,但这并不意味着你要调试它 我也尝试过这个电话(和其他部分)在许多不同的方式和没有。。。没有想法了 谢谢Opencv java中的CascadeClassifier在网络摄像头中找不到人脸 我试图把C++从Java翻译成java。在C++中工作良好。这也很好用,opencv,Opencv,但是翻译根本就没有检测到人脸。我没有明显的错误。我可以看到来自网络摄像头的视频输入处理(灰色/直方图…)和视频显示。级联加载不会给出错误。但是这个调用不会返回任何面孔。。。因此,您可能可以跳过所有代码,直接转到我的CascadeClassifier调用,转到publicmatdetect(matinputframe)。由于我不熟悉Java和OpenCV,所以我粘贴了其余部分(我删除了我认为可能不重要的内容),以防万一,但这并不意味着你要调试它 我也尝试过这个电话(和其他部分)在许多不同的方式和没
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
class My_Panel extends JPanel{
private static final long serialVersionUID = 1L;
private BufferedImage image;
private CascadeClassifier face_cascade;
// Create a constructor method
public My_Panel(){
super();
String face_cascade_name = "/haarcascade_frontalface_alt.xml";
//String face_cascade_name = "/lbpcascade_frontalface.xml";
//-- 1. Load the cascades
String str;
str = getClass().getResource(face_cascade_name).getPath();
str = str.replace("/C:","C:");
face_cascade_name=str;
face_cascade=new CascadeClassifier(face_cascade_name);
if( !face_cascade.empty())
{
System.out.println("--(!)Error loading A\n");
return;
}
else
{
System.out.println("Face classifier loooaaaaaded up");
}
}
private BufferedImage getimage(){
return image;
}
public void setimage(BufferedImage newimage){
image=newimage;
return;
}
/**
* 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 BufferedImage matToBufferedImage(Mat matrix) {
int cols = matrix.cols();
int rows = matrix.rows();
int elemSize = (int)matrix.elemSize();
byte[] data = new byte[cols * rows * elemSize];
int type;
matrix.get(0, 0, data);
switch (matrix.channels()) {
case 1:
type = BufferedImage.TYPE_BYTE_GRAY;
break;
case 3:
type = BufferedImage.TYPE_3BYTE_BGR;
// bgr to rgb
byte b;
for(int i=0; i<data.length; i=i+3) {
b = data[i];
data[i] = data[i+2];
data[i+2] = b;
}
break;
default:
return null;
}
BufferedImage image2 = new BufferedImage(cols, rows, type);
image2.getRaster().setDataElements(0, 0, cols, rows, data);
return image2;
}
public void paintComponent(Graphics g){
BufferedImage temp=getimage();
g.drawImage(temp,10,10,temp.getWidth(),temp.getHeight(), this);
}
public Mat detect(Mat inputframe){
Mat mRgba=new Mat();
Mat mGrey=new Mat();
MatOfRect faces = new MatOfRect();
//MatOfRect eyes = new MatOfRect();
inputframe.copyTo(mRgba);
inputframe.copyTo(mGrey);
Imgproc.cvtColor( mRgba, mGrey, Imgproc.COLOR_BGR2GRAY);
Imgproc.equalizeHist( mGrey, mGrey );
face_cascade.detectMultiScale(mGrey, faces);
//face_cascade.detectMultiScale(mGrey, faces, 1.1, 2, 0|Objdetect.CASCADE_SCALE_IMAGE, new Size(30, 30), new Size(200,200) );
//face_cascade.detectMultiScale(mGrey, faces, 1.1, 2, 2//CV_HAAR_SCALE_IMAGE,
// ,new Size(30, 30), new Size(200,200) );
System.out.println(String.format("Detected %s faces", faces.toArray().length));
return mGrey;
}
}
public class window {
public static void main(String arg[]){
// Load the native library.
System.loadLibrary("opencv_java245");
String window_name = "Capture - Face detection";
JFrame frame = new JFrame(window_name);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
My_Panel my_panel = new My_Panel();
frame.setContentPane(my_panel);
frame.setVisible(true);
//-- 2. Read the video stream
BufferedImage temp;
Mat webcam_image=new Mat();
VideoCapture capture =new VideoCapture(0);
if( capture.isOpened())
{
while( true )
{
capture.read(webcam_image);
if( !webcam_image.empty() )
{
frame.setSize(webcam_image.width()+40,webcam_image.height()+60);
//-- 3. Apply the classifier to the captured image
// At this point I was wondering where this should be done.
// I put it within the panel class, but maybe one could actually
// create a processor object...
webcam_image=my_panel.detect(webcam_image);
//-- 4. Display the image
temp=my_panel.matToBufferedImage(webcam_image);
my_panel.setimage(temp);
my_panel.repaint();
}
else
{
System.out.println(" --(!) No captured frame -- Break!");
break;
}
}
}
return;
}
}
import java.awt.*;
导入java.awt.image.buffereImage;
导入javax.swing.*;
导入org.opencv.core.Mat;
导入org.opencv.core.MatOfRect;
导入org.opencv.highgui.VideoCapture;
导入org.opencv.imgproc.imgproc;
导入org.opencv.objdetect.CascadeClassifier;
类My_面板扩展了JPanel{
私有静态最终长serialVersionUID=1L;
私有缓冲图像;
私有级联;
//创建一个构造函数方法
公共My_面板(){
超级();
字符串face_cascade_name=“/haarcascade_frontalface_alt.xml”;
//字符串face_cascade_name=“/lbpcascade_frontalface.xml”;
//--1.加载叶栅
字符串str;
str=getClass().getResource(face_cascade_name).getPath();
str=str.replace(“/C:”,“C:”);
面_级联_name=str;
face_cascade=新的级联分类器(face_cascade_名称);
如果(!face_cascade.empty())
{
System.out.println(“-(!)加载错误\n”);
返回;
}
其他的
{
System.out.println(“面分类器LoooaaaadUp”);
}
}
私有缓冲区映像getimage(){
返回图像;
}
public void setimage(BufferedImage newimage){
图像=新图像;
返回;
}
/**
*将Mat转换/写入BuffereImage。
*
*@CV_8UC3或CV_8UC1型参数矩阵垫
*@return buffereImage类型为\u 3BYTE\u BGR或\u BYTE\u GRAY
*/
公共缓冲区映像MATTOBUFFERIDIMAGE(Mat矩阵){
int cols=matrix.cols();
int rows=matrix.rows();
int elemSize=(int)matrix.elemSize();
字节[]数据=新字节[cols*行*elemSize];
int型;
得到(0,0,数据);
开关(matrix.channels()){
案例1:
type=buffereImage.type\u BYTE\u GRAY;
打破
案例3:
type=buffereImage.type\u 3BYTE\u BGR;
//bgr到rgb
字节b;
对于(int i=0;i我已经尝试了您的代码,效果很好!您对haarcascade_frontalface_alt.xml文件位置只有一个问题。请尝试使用文件的完整路径:
face\u cascade=新的级联分类器(“D:/HelloCV/src/haarcascade\u frontalface\u alt.xml”)非常感谢您对它进行测试!!您完全正确。我有两个错误:1/加载CascadeClassifier时没有出现错误,因为真假情况是颠倒的,duh!2/CascadeClassifier没有加载,因为使用的方法返回了路径“C:/USERDATA/MEDICAL/CODE/java%20webcam%20facedetect/bin/haarcascade_frontalface_alt.xml”。html scape代码打破了一切。简单的空格就可以做到这一点:“C:/USERDATA/MEDICAL/CODE/java webcam facedetect/bin/haarcascade_frontalface_alt.xml”。错误已经解决了!谢谢!