Java - 使用OpenCV的运动/对象检测(人体检测)

时间:2015-05-23 10:05:41

标签: java opencv object detection motion

我使用opencv从cctv相机获得了视频流。现在我想从这个视频流中检测一个简单的运动/对象。例如,如果任何人进入任何选定区域,则应在他周围生成矩形边框。我搜索了一些opencv教程,但我没有得到任何正确的解决方案或想法。我使用以下代码来显示视频流。

    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferByte;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;

    import org.opencv.core.Core;
    import org.opencv.core.Mat;
    import org.opencv.highgui.Highgui;
    import org.opencv.highgui.VideoCapture;
    import org.opencv.imgproc.Imgproc;
    import org.opencv.objdetect.CascadeClassifier;

    public class VideoStream
    {
        static BufferedImage tmpImg=null;
        public static void main(String args[]) throws InterruptedException
        {
            System.out.println("opencv start..");

            // Load native library
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

            VideoCapture camView=new VideoCapture();
            camView.open("http://192.168.1.7:80/cgi-bin/view.cgi?chn=6&u=admin&p=");

            if(!camView.isOpened())
            {
                System.out.println("Camera Error..");
            }
            else
            {
                System.out.println("Camera successfully opened");
            }

            videoCamera cam=new videoCamera(camView);

            //Initialize swing components
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            frame.add(cam);
            frame.setSize(1024,768);  
            frame.setVisible(true);

            while(camView.isOpened())
            {
                cam.repaint();
            }
        }
    }

@SuppressWarnings("serial")
        class videoCamera extends JPanel
{
    VideoCapture camera;
    Mat mat=new Mat();


    public videoCamera(VideoCapture cam) 
    {
        camera  = cam;
    }

    public BufferedImage Mat2BufferedImage(Mat m)
    {
        int type = BufferedImage.TYPE_BYTE_GRAY;
        if (m.channels() > 1)
        {
            type = BufferedImage.TYPE_3BYTE_BGR;
        }
        int bufferSize = m.channels() * m.cols() * m.rows();
        byte[] b = new byte[bufferSize];
        m.get(0, 0, b); // get all the pixels
        BufferedImage img = new BufferedImage(m.cols(), m.rows(), type);
        final byte[] targetPixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
        System.arraycopy(b, 0, targetPixels, 0, b.length);
        return img;
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Mat mat = new Mat();

        camera.read(mat);

        BufferedImage image = Mat2BufferedImage(mat);

        g.drawImage(image,0,0,image.getWidth(),image.getHeight(), null);
    } 
}

任何人都知道我们怎么能这样做。

0 个答案:

没有答案