我如何从视频流中裁剪检测到的脸,并使用OpenCV Java将其存储在文件夹中



我使用的是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;
    }
}

在您的代码中,您正在检测Face矩形为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现在打破了视频和ImgCodecs。同时,核心。矩形函数已经移动到ImgProc.

相关内容

  • 没有找到相关文章

最新更新