Exemple #1
0
  public static void bhadis() {

    Mat a =
        new Mat(6, 4, CvType.CV_32F) {
          {
            put(0, 0, 0, 1, 1, 5);
            put(1, 0, 1, 0, 2, 4);
            put(2, 0, 2, 2, 3, 6);
            put(3, 0, 0, 4, 6, 1);
            put(4, 0, 5, 4, 6, 1);
            put(5, 0, 4, 2, 8, 1);
          }
        };

    Mat b =
        new Mat(6, 4, CvType.CV_32F) {
          {
            put(0, 0, 4, 9, 2, 1);
            put(1, 0, 9, 6, 0, 3);
            put(2, 0, 2, 0, 8, 6);
            put(3, 0, 1, 3, 6, 9);
            put(4, 0, 5, 7, 6, 1);
            put(5, 0, 4, 2, 8, 1);
          }
        };

    System.out.println(a.dump());
    System.out.println(b.dump());

    double d = Distance.Bhattacharyya(b, b);

    System.out.println("Bhattacharyya distance = " + d);
  }
Exemple #2
0
 public static void normlise() {
   Mat data =
       new Mat(3, 4, CvType.CV_32F) {
         {
           put(0, 0, 1, 2, 2, 4);
           put(1, 0, 2, 4, 4, 8);
           put(2, 0, 3, 6, 6, 12);
         }
       };
   System.out.println(data.dump());
   Mat norm = WormGene.normaliseMeanVariance(data);
   System.out.println(norm.dump());
 }
  public static Mat getCCH(Mat image) {
    ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(
        image, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);

    Mat chainHistogram = Mat.zeros(1, 8, CvType.CV_32F);
    int n = 0;
    MatOfPoint2f approxCurve = new MatOfPoint2f();
    for (MatOfPoint contour : contours) {

      // get the freeman chain code from the contours
      int rows = contour.rows();
      // System.out.println("\nrows"+rows+"\n"+contour.dump());
      int direction = 7;
      Mat prevPoint = contours.get(0).row(0);
      n += rows - 1;
      for (int i = 1; i < rows; i++) {
        // get the current point
        double x1 = contour.get(i - 1, 0)[1];
        double y1 = contour.get(i - 1, 0)[0];

        // get the second point
        double x2 = contour.get(i, 0)[1];
        double y2 = contour.get(i, 0)[0];

        if (x2 == x1 && y2 == y1 + 1) direction = 0;
        else if (x2 == x1 - 1 && y2 == y1 + 1) direction = 1;
        else if (x2 == x1 - 1 && y2 == y1) direction = 2;
        else if (x2 == x1 - 1 && y2 == y1 - 1) direction = 3;
        else if (x2 == x1 && y2 == y1 - 1) direction = 4;
        else if (x2 == x1 + 1 && y2 == y1 - 1) direction = 5;
        else if (x2 == x1 + 1 && y2 == y1) direction = 6;
        else if (x2 == x1 + 1 && y2 == y1 + 1) direction = 7;
        else System.out.print("err");
        double counter = chainHistogram.get(0, direction)[0];
        chainHistogram.put(0, direction, ++counter);
        System.out.print(direction);
      }
    }
    System.out.println("\n" + chainHistogram.dump());
    Scalar alpha = new Scalar(n); // the factor
    Core.divide(chainHistogram, alpha, chainHistogram);
    System.out.println("\nrows=" + n + " " + chainHistogram.dump());
    return chainHistogram;
  }
Exemple #4
0
 public static void V2M() {
   Vector<double[]> v = new Vector<double[]>();
   double[] temp = {1.0, 2.0, 3.0, 4.0};
   v.add(temp);
   v.add(temp);
   v.add(temp);
   Mat m = DataConverter.jvector2Mat(v);
   System.out.println(m.dump());
 }
  public static void main(String[] args) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    //      Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );
    //      System.out.println( "mat = " + mat.dump() );

    Sample n = new Sample();
    //   n.templateMatching();

    // put text in image
    //      Mat data= Highgui.imread("images/erosion.jpg");

    //      Core.putText(data, "Sample", new Point(50,80), Core.FONT_HERSHEY_SIMPLEX, 1, new
    // Scalar(0,0,0),2);
    //
    //      Highgui.imwrite("images/erosion2.jpg", data);

    // getting dct of an image
    String path = "images/croppedfeature/go (20).jpg";
    path = "images/wordseg/img1.png";
    Mat image = Highgui.imread(path, Highgui.IMREAD_GRAYSCALE);
    ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    Imgproc.threshold(image, image, 0, 255, Imgproc.THRESH_OTSU);
    Imgproc.threshold(image, image, 220, 128, Imgproc.THRESH_BINARY_INV);
    Mat newImg = new Mat(45, 100, image.type());

    newImg.setTo(new Scalar(0));
    n.copyMat(image, newImg);

    int vgap = 25;
    int hgap = 45 / 3;

    Moments m = Imgproc.moments(image, false);
    Mat hu = new Mat();
    Imgproc.HuMoments(m, hu);
    System.out.println(hu.dump());

    //      //divide the mat into 12 parts then get the features of each part
    //      int count=1;
    //      for(int j=0; j<45; j+=hgap){
    //    	  for(int i=0;i<100;i+=vgap){
    //    		  Mat result = newImg.submat(j, j+hgap, i, i+vgap);
    //
    //
    //    		  Moments m= Imgproc.moments(result, false);
    //    		  double m01= m.get_m01();
    //    		  double m00= m.get_m00();
    //    		  double m10 = m.get_m10();
    //    		  int x= m00!=0? (int)(m10/m00):0;
    //    		  int y= m00!=0? (int)(m01/m00):0;
    //    		  Mat hu= new Mat();
    //    		  Imgproc.HuMoments(m, hu);
    //    		  System.out.println(hu.dump());
    //    		  System.out.println(count+" :"+x+" and "+y);
    //    		  Imgproc.threshold(result, result, 0,254, Imgproc.THRESH_BINARY_INV);
    //    		  Highgui.imwrite("images/submat/"+count+".jpg", result);
    //    		  count++;
    //
    //    	  }
    //      }
    //
    //    for(int i=vgap;i<100;i+=vgap){
    //	  Point pt1= new Point(i, 0);
    //      Point pt2= new Point(i, 99);
    //      Core.line(newImg, pt1, pt2, new Scalar(0,0,0));
    //  }
    //  for(int i=hgap;i<45;i+=hgap){
    //	  Point pt1= new Point(0, i);
    //      Point pt2= new Point(99, i);
    //      Core.line(newImg, pt1, pt2, new Scalar(0,0,0));
    //  }
    //      Highgui.imwrite("images/submat/copyto.jpg", newImg);
  }
  /**
   * Analyze video frames using computer vision approach and generate a ArrayList<AttitudeRec>
   *
   * @param recs output ArrayList of AttitudeRec
   * @return total number of frame of the video
   */
  private int analyzeVideo(ArrayList<AttitudeRec> recs) {
    VideoMetaInfo meta = new VideoMetaInfo(new File(mPath, "videometa.json"));

    int decimation = 1;

    if (meta.fps > DECIMATION_FPS_TARGET) {
      decimation = (int) (meta.fps / DECIMATION_FPS_TARGET);
      meta.fps /= decimation;
    }

    VideoDecoderForOpenCV videoDecoder =
        new VideoDecoderForOpenCV(
            new File(mPath, "video.mp4"), decimation); // every 3 frame process 1 frame

    Mat frame;
    Mat gray = new Mat();
    int i = -1;

    Size frameSize = videoDecoder.getSize();

    if (frameSize.width != meta.frameWidth || frameSize.height != meta.frameHeight) {
      // this is very unlikely
      return -1;
    }

    if (TRACE_VIDEO_ANALYSIS) {
      Debug.startMethodTracing("cvprocess");
    }

    Size patternSize = new Size(4, 11);

    float fc = (float) (meta.frameWidth / 2.0 / Math.tan(meta.fovWidth / 2.0));
    Mat camMat = cameraMatrix(fc, new Size(frameSize.width / 2, frameSize.height / 2));
    MatOfDouble coeff = new MatOfDouble(); // dummy

    MatOfPoint2f centers = new MatOfPoint2f();
    MatOfPoint3f grid = asymmetricalCircleGrid(patternSize);
    Mat rvec = new MatOfFloat();
    Mat tvec = new MatOfFloat();

    MatOfPoint2f reprojCenters = new MatOfPoint2f();

    if (LOCAL_LOGV) {
      Log.v(TAG, "Camera Mat = \n" + camMat.dump());
    }

    long startTime = System.nanoTime();

    while ((frame = videoDecoder.getFrame()) != null) {
      if (LOCAL_LOGV) {
        Log.v(TAG, "got a frame " + i);
      }

      // has to be in front, as there are cases where execution
      // will skip the later part of this while
      i++;

      // convert to gray manually as by default findCirclesGridDefault uses COLOR_BGR2GRAY
      Imgproc.cvtColor(frame, gray, Imgproc.COLOR_RGB2GRAY);

      boolean foundPattern =
          Calib3d.findCirclesGridDefault(
              gray, patternSize, centers, Calib3d.CALIB_CB_ASYMMETRIC_GRID);

      if (!foundPattern) {
        // skip to next frame
        continue;
      }

      if (OUTPUT_DEBUG_IMAGE) {
        Calib3d.drawChessboardCorners(frame, patternSize, centers, true);
      }

      // figure out the extrinsic parameters using real ground truth 3D points and the pixel
      // position of blobs found in findCircleGrid, an estimated camera matrix and
      // no-distortion are assumed.
      boolean foundSolution =
          Calib3d.solvePnP(grid, centers, camMat, coeff, rvec, tvec, false, Calib3d.CV_ITERATIVE);

      if (!foundSolution) {
        // skip to next frame
        if (LOCAL_LOGV) {
          Log.v(TAG, "cannot find pnp solution in frame " + i + ", skipped.");
        }
        continue;
      }

      // reproject points to for evaluation of result accuracy of solvePnP
      Calib3d.projectPoints(grid, rvec, tvec, camMat, coeff, reprojCenters);

      // error is evaluated in norm2, which is real error in pixel distance / sqrt(2)
      double error = Core.norm(centers, reprojCenters, Core.NORM_L2);

      if (LOCAL_LOGV) {
        Log.v(TAG, "Found attitude, re-projection error = " + error);
      }

      // if error is reasonable, add it into the results
      if (error < REPROJECTION_THREASHOLD) {
        double[] rv = new double[3];
        rvec.get(0, 0, rv);
        recs.add(new AttitudeRec((double) i / meta.fps, rodr2rpy(rv)));
      }

      if (OUTPUT_DEBUG_IMAGE) {
        Calib3d.drawChessboardCorners(frame, patternSize, reprojCenters, true);
        Highgui.imwrite(
            Environment.getExternalStorageDirectory().getPath()
                + "/RVCVRecData/DebugCV/img"
                + i
                + ".png",
            frame);
      }
    }

    if (LOCAL_LOGV) {
      Log.v(TAG, "Finished decoding");
    }

    if (TRACE_VIDEO_ANALYSIS) {
      Debug.stopMethodTracing();
    }

    if (LOCAL_LOGV) {
      // time analysis
      double totalTime = (System.nanoTime() - startTime) / 1e9;
      Log.i(TAG, "Total time: " + totalTime + "s, Per frame time: " + totalTime / i);
    }
    return i;
  }