我正在使用OpenCV 2.4.9,Java语言和eclipse。我的人脸检测代码如下。问题是如何裁剪检测到的面部并将其存储在文件夹中?
我努力尝试但无法获得所需的输出。
package code03;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.VideoCapture;
import org.opencv.objdetect.CascadeClassifier;
public class VideoPanel2 extends JPanel implements ActionListener
{
private static final long serialVersionUID = 1L;
//***********************************************************************************************
private BufferedImage image;
int count = 1;
//***********************************************************************************************
public VideoPanel2()
{
super();
}
//***********************************************************************************************
public BufferedImage getimage()
{
return image;
}
//***********************************************************************************************
public void setimage(BufferedImage newimage)
{
image = newimage;
return;
}
//***********************************************************************************************
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (this.image==null) return;
g.drawImage(this.image,10,492,650,43,this.image.getWidth(),this.image.getHeight(), count, count, null);
}
//***********************************************************************************************
public void DatainIt() throws Exception{
JFrame frame = new JFrame("Face Detection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,800);
System.loadLibrary("opencv_java249");
CascadeClassifier faceDetector = new CascadeClassifier("./res/haarcascade_frontalface_alt.xml");
//CascadeClassifier faceDetector = new CascadeClassifier("./res/lbpcascade_frontalface.xml");
VideoPanel2 vidPanel = new VideoPanel2();
frame.setContentPane(vidPanel);
//BUTTON
JButton save = new JButton("Add a new Person");
//save.setBounds(2, 2, 30, 80);
JPanel pbutton = new JPanel();
pbutton.add(save);
//TextField
JTextField p_name = new JTextField(25);
frame.add(p_name);
frame.add(pbutton);
frame.setVisible(true);
save.addActionListener(this);
Mat webcam_image = new Mat();
MatToBufImg mat2Buf = new MatToBufImg();
VideoCapture capture = new VideoCapture(0);
if(capture.isOpened())
{
Thread .sleep(100); //Give time to webcam to initialize itself
while(true)
{
capture.read(webcam_image);
if(!webcam_image.empty())
{
frame.setSize(webcam_image.width()+40, webcam_image.height()+60);
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(webcam_image, faceDetections);
for(Rect rect : faceDetections.toArray())
{
Core.rectangle(webcam_image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255,0));
//Mat croppedImage = setimage(rect);
}
mat2Buf.setMatrix(webcam_image, ".jpg");
//Highgui.imwrite("webcam_image.jpg", faceDetections);
//File file = new File("Image" + "." + ".jpg");
//ImageIO.write((RenderedImage) webcam_image, ".jpg", file);
vidPanel.setimage(mat2Buf.getBufferedImage());
vidPanel.repaint();
// get the video stream
//BufferedImage bi = mat2Buf.getBufferedImage();//getimage();
//bi.getSubimage(arg0, arg1, arg2, arg3);
//File outputfile= new File("D:\\Java Project\\FaceRecognition\\src\\code03\\Face Database\\saved.jpg");
//ImageIO.write((RenderedImage) bi, "jpg", outputfile);
}
else
{
System.out.println("Problems with WebCam Capture");
break;
}
}
}//end if
capture.release();
}//end DatainIt()
//***********************************************************************************************
public static void main(String arg[]) throws Exception{
VideoPanel2 vid = new VideoPanel2();
vid.DatainIt();
}//end main
//***********************************************************************************************
public void actionPerformed(ActionEvent arg0) {
}
}//end of class
此课程中使用的其他文件的代码是:
package code03;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.imageio.ImageIO;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.highgui.Highgui;
public class MatToBufImg{
Mat matrix;
MatOfByte mob;
String fileExten;
public MatToBufImg(){
}
public MatToBufImg(Mat amatrix, String fileExt){
matrix = amatrix;
fileExten = fileExt;
}
public void setMatrix(Mat amatrix, String fileExt){
matrix = amatrix;
fileExten = fileExt;
mob = new MatOfByte();
}
public BufferedImage getBufferedImage(){
Highgui.imencode(fileExten, matrix, mob);
byte[] byteArray = mob.toArray();
BufferedImage bufImage = null;
try{
InputStream in = new ByteArrayInputStream(byteArray);
bufImage = ImageIO.read(in);
}catch(Exception e){
e.printStackTrace();
}
return bufImage;
}
}
答案 0 :(得分:3)
在您的代码中,您将Face Rectangles检测为MatOfRect并在视频上绘制一个矩形。
Core.rectangle(webcam_image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255,0));
在这里,您已经拥有了所需图像的roi。因此裁剪框架的roi部分为:
Mat faceROI = new Mat(webcam_image,rect);
Highgui.imwrite("Face_frameNumber_faceInImageNumber.jpeg", faceROI);
另外,请考虑转到Opencv 3.1 /最新版本。您将受益于新功能,例如优化和过去一年贡献的许多算法。
OPENCV 3以后:
Highgui现已破坏为VideoIO和ImgCodecs。此外,Core.rectangle之类的函数已移至ImgProc。