title: Opencv car counting
body: <p>hi guys today i got a little problem about cars counting in opencv using java. i perfectly detect cars on video but my car counter didn't count perfectly for example if we have 10 cars it counts only 8 sometimes 9. this is my final year project please help me. here is my code about detection of cars</p>
  
  <pre><code>    while (running) {
          Mat previusFrame = new Mat();
          Mat currentFrame = new Mat();
          Mat grayImage1 = new Mat();
          Mat grayImage2 = new Mat();
          Mat differenceImage = new Mat();
          Mat thresholdImage = new Mat();
  
          camera.read(previusFrame);
          Imgproc.resize(previusFrame, previusFrame, new Size(WIDTH, HEIGHT));
          Imgproc.cvtColor(previusFrame, grayImage1, Imgproc.COLOR_BGR2GRAY);
          while (running) {
              boolean frameSucces = camera.read(currentFrame);
              if (frameSucces) {
                  Imgproc.resize(currentFrame, currentFrame, new Size(WIDTH, HEIGHT));
                  Imgproc.cvtColor(currentFrame, grayImage2, Imgproc.COLOR_BGR2GRAY);
  
              } else {
                  break;
              }
  
              //abbs differnce
              Core.absdiff(grayImage1, grayImage2, differenceImage);
              Imgproc.threshold(differenceImage, thresholdImage, SENSITIVE, 255, Imgproc.THRESH_BINARY);
              Imgproc.blur(thresholdImage, thresholdImage, new Size(BLUR, BLUR));
              Imgproc.threshold(thresholdImage, thresholdImage, SENSITIVE, 255, Imgproc.THRESH_BINARY);
  
              //finding Movement
              Mat heirarchy = new Mat();
              Rect boundingRect = new Rect(0, 0, 0, 0);
              List&lt;MatOfPoint&gt; contour = new ArrayList&lt;MatOfPoint&gt;();
              Imgproc.findContours(thresholdImage, contour, heirarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
              for (int i = 0; i &lt; contour.size(); i++) {
                  boundingRect = Imgproc.boundingRect(contour.get(i));
  
                  if (boundingRect.area() &gt; 500) {
                      Imgproc.rectangle(currentFrame, boundingRect.tl(), boundingRect.br(), new Scalar(0, 0, 255), 2);
  
                  }
              }
              Imgproc.line(currentFrame, new Point(10, 200), new Point(currentFrame.width() - 10, 200), new Scalar(0, 255, 0), 3);
  
              Moments moment = Imgproc.moments(thresholdImage);
              double m10 = moment.get_m10();
              double m01 = moment.get_m01();
              double m_area = moment.get_m00();
              int posx = (int) (m10 / m_area);
              int posy = (int) (m01 / m_area);
              if (posy &gt; 202 &amp;&amp; posy &lt;= 245) {
                  i++;
              }
              label.setText("cars Number" + i);
              MatOfByte matof = new MatOfByte();
              Imgcodecs.imencode(".bmp", currentFrame, matof);
              byte[] data = matof.toArray();
              try {
                  image = ImageIO.read(new ByteArrayInputStream(data));
              } catch (IOException ex) {
                  Logger.getLogger(MotionDetecter.class.getName()).log(Level.SEVERE, null, ex);
              }
              Graphics g = getGraphics();
              g.drawImage(image, 0, 20, null);
  
              grayImage2.copyTo(grayImage1);
  
          }
  
          camera.release();
  
          try {
              Thread.sleep(100);
          } catch (InterruptedException ex) {
              Logger.getLogger(MotionDetecter.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  }
  </code></pre>