title: Java - Motion / Object detection (Human detection) using OpenCV
body: <p>I got the video stream from cctv camera using opencv. Now I want to detect a simple motion / object from this video stream. For example, if any person come in any selected zone then rectangular border should be generated around him.  I search some opencv tutorial, but I didn't got any proper solution or idea.I used the following code to display video stream. </p>
  
  <pre><code>    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&amp;u=admin&amp;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() &gt; 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);
      } 
  }
  </code></pre>
  
  <p>Can any one knows how could we do that.</p>