@Override
    public void run() {
      // Initialisation
      cptRect = 0;
      initialiseRectangle();

      if (video.isOpened()) {
        while (begin == true) {
          // On récupère l'image de la CaptureVideo
          video.retrieve(frameaux);
          // On modifie les dimensions de la frame
          Imgproc.resize(frameaux, frame, frame.size());
          // On copie
          frame.copyTo(currentFrame);

          if (jCheckBoxMotionDetection.isSelected()) {
            if (firstFrame) {
              frame.copyTo(lastFrame);
              firstFrame = false;
              continue;
            }

            // Soustraction de currentFrame par rapport à la dernière
            Core.subtract(currentFrame, lastFrame, processedFrame);

            // Filtre en niveau de gris
            Imgproc.cvtColor(processedFrame, processedFrame, Imgproc.COLOR_RGB2GRAY);

            // Filtre threshold + récupération du Jslider
            int threshold = jSliderThreshold.getValue();
            Imgproc.threshold(
                processedFrame, processedFrame, threshold, 255, Imgproc.THRESH_BINARY);

            // Detecte les contours et check dans les
            detection_contours(currentFrame, processedFrame);
          }
          // Dessine les rectangles d'authentifications
          drawRectangle();

          currentFrame.copyTo(processedFrame);

          // Encodage de la frame en MatOfByte
          Highgui.imencode(".jpg", processedFrame, matOfByte);
          byte[] byteArray = matOfByte.toArray();

          // Affichage de l'image
          try {
            in = new ByteArrayInputStream(byteArray);
            bufImage = ImageIO.read(in);
            image.updateImage(bufImage);
          } catch (Exception ex) {
            ex.printStackTrace();
          }

          try {
            Thread.sleep(50);
          } catch (Exception ex) {
            ex.printStackTrace();
          }
        }
      }
    }
Ejemplo n.º 2
0
  // Bhattacharyya coefficient for two distributions
  // a, b are original data samples
  // better if a, b are dimension-reduced Principal Components
  public static double Bhattacharyya(Mat a, Mat b) {

    // if not dimension-reduced
    //		int k = a.cols()/2;
    //		Mat temp1 = new Mat();
    //		Mat temp2 = new Mat();
    //		Mat pa = new Mat();
    //		Mat pb = new Mat();
    //		Core.PCACompute(a, temp1, temp2, k);
    //		Core.PCAProject(a, temp1, temp2, pa);
    //		Core.PCACompute(b, temp1, temp2, k);
    //		Core.PCAProject(b, temp1, temp2, pb);
    //

    Mat cova = new Mat();
    Mat covb = new Mat();
    Mat meana = new Mat();
    Mat meanb = new Mat();
    Core.calcCovarMatrix(a, cova, meana, Core.COVAR_NORMAL | Core.COVAR_ROWS);
    Core.calcCovarMatrix(b, covb, meanb, Core.COVAR_NORMAL | Core.COVAR_ROWS);

    // if not dimension-reduced
    //		Core.calcCovarMatrix(pa, cova, meana, Core.COVAR_NORMAL|Core.COVAR_ROWS);
    //		Core.calcCovarMatrix(pb, covb, meanb, Core.COVAR_NORMAL|Core.COVAR_ROWS);

    //		System.out.println("cov");
    //		System.out.println(cova.dump());
    //		System.out.println(covb.dump());

    double detcova = Core.determinant(cova);
    double detcovb = Core.determinant(covb);
    Mat cov = new Mat();
    Core.add(cova, covb, cov);
    //		cov = cov.mul(cov, 0.5);

    //		Core.scaleAdd(cov, 0.5, new Mat(), cov);
    for (int i = 0; i < cov.rows(); i++)
      for (int j = 0; j < cov.cols(); j++) {
        cov.put(i, j, 0.5 * cov.get(i, j)[0]);
      }

    double detcov = Core.determinant(cov);

    Mat inv = new Mat(); // inverse of cov
    Core.invert(cov, inv);

    Mat mabsub = new Mat();
    Core.subtract(meana, meanb, mabsub);

    Mat mabsub_tp = new Mat();
    Core.transpose(mabsub, mabsub_tp);

    Mat temp = new Mat();
    Core.gemm(mabsub, inv, 1, new Mat(), 0, temp);
    //		System.out.println(temp.dump());
    //		System.out.println(mabsub_tp.dump());

    Mat res = new Mat();
    Core.gemm(temp, mabsub_tp, 1, new Mat(), 0, res);
    //		System.out.println(res.dump());

    //		Mat temp = mabsub_tp.mul(inv);
    //		System.out.println(temp.dump());
    //
    //		Mat res = temp.mul(mabsub);
    //		System.out.println(res.dump());

    double s2 = Math.sqrt(detcov / (Math.sqrt(detcova * detcovb)));
    // s1 = 0.125 * (mabsub_tp*inv*mabsub)
    double s1 = 0.125 * res.get(0, 0)[0];

    double bdis = 1 / (Math.exp(s1) * s2);
    return bdis;
  }