Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.57 KB | None | 0 0
  1.     package com.ar.game;
  2.  
  3.     import com.badlogic.gdx.ApplicationAdapter;
  4.     import com.badlogic.gdx.Gdx;
  5.     import com.badlogic.gdx.graphics.Color;
  6.     import com.badlogic.gdx.graphics.GL20;
  7.     import com.badlogic.gdx.graphics.PerspectiveCamera;
  8.     import com.badlogic.gdx.graphics.VertexAttributes;
  9.     import com.badlogic.gdx.graphics.g3d.*;
  10.     import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
  11.     import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
  12.     import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
  13.     import dk.ar.opencv.UtilAR;
  14.     import org.opencv.calib3d.Calib3d;
  15.     import org.opencv.core.*;
  16.     import org.opencv.highgui.Highgui;
  17.     import org.opencv.highgui.VideoCapture;
  18.     import org.opencv.imgproc.Imgproc;
  19.  
  20.     import javax.rmi.CORBA.Util;
  21.     import java.io.File;
  22.     import java.lang.Math.*;
  23.  
  24.     import java.util.ArrayList;
  25.     import java.util.Arrays;
  26.     import java.util.List;
  27.     import java.util.Scanner;
  28.  
  29.  
  30.  
  31.     public class MyGdxGame extends ApplicationAdapter {
  32.  
  33.         VideoCapture webcam;
  34.         Mat frame = new Mat();
  35.         Mat frameDrawing = new Mat();
  36.         Mat frameBW = new Mat();
  37.         Mat frameMono = new Mat();
  38.         MatOfPoint2f corners2d = new MatOfPoint2f();
  39.         MatOfPoint3f corners3d = new MatOfPoint3f();
  40.  
  41.         PerspectiveCamera cam;
  42.         ArrayList<ModelInstance> arrows;
  43.         ModelBatch batch;
  44.         private Environment environment;
  45.  
  46.         boolean useWebcam = true;
  47.  
  48.         @Override
  49.         public void create () {
  50.             if (useWebcam) {
  51.                 webcam = new VideoCapture(0);
  52.  
  53.             } else {
  54.  
  55.  
  56.                 String path = getClass().getResource("/webcam.png").getPath();
  57.  
  58.                 System.out.println("Path " + path);
  59.                 if (path.startsWith("/")) {
  60.                     path = path.substring(1);
  61.                 }
  62.                 frame = Highgui.imread(path);
  63.  
  64.                 if (!frame.empty())
  65.                 {
  66.                     System.out.println("Height: " + frame.height() + " Width: " + frame.width());
  67.                 }
  68.                 else
  69.                 {
  70.                     System.out.println("Image is empty!" );
  71.                 }
  72.  
  73.             }
  74.  
  75.  
  76.             cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  77.             cam.position.set(3.6f, 2.4f, -15f);
  78.             cam.lookAt(3,5,0);
  79.             cam.near = 1f;
  80.             cam.far = 300f;
  81.             cam.update();
  82.  
  83.             environment = new Environment();
  84.             environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.2f, 0.8f, 0.4f, 1f));
  85.             environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, 0.8f, 0.2f));
  86.  
  87.             batch = new ModelBatch();
  88.  
  89.             ModelBuilder mb = new ModelBuilder();
  90.  
  91.             arrows = new ArrayList<ModelInstance>();
  92.  
  93.             Model arrow_x = mb.createArrow(0, 0, 0, 1, 0, 0, 0.2f, 0.5f, 20, GL20.GL_TRIANGLES, new Material(ColorAttribute.createDiffuse(Color.BLUE)),
  94.                     VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
  95.  
  96.             arrows.add(new ModelInstance(arrow_x));
  97.  
  98.             Model arrow_y = mb.createArrow(0, 0, 0, 0, 1, 0, 0.2f, 0.5f, 20, GL20.GL_TRIANGLES, new Material(ColorAttribute.createDiffuse(Color.RED)),
  99.                     VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
  100.  
  101.             arrows.add(new ModelInstance(arrow_y));
  102.  
  103.             Model arrow_z = mb.createArrow(0, 0, 0, 0, 0, 1, 0.2f, 0.5f, 20, GL20.GL_TRIANGLES, new Material(ColorAttribute.createDiffuse(Color.GREEN)),
  104.                     VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
  105.  
  106.             arrows.add(new ModelInstance(arrow_z));
  107.         }
  108.  
  109.         @Override
  110.         public void render () {
  111.             if(useWebcam) {
  112.  
  113.                 if (!webcam.isOpened() || !webcam.grab() || !webcam.retrieve(frame)) {
  114.                     System.out.println("Unable to get frame");
  115.                     return;
  116.                 }
  117.  
  118.  
  119.               Core.flip(frame, frame, 1);
  120.  
  121.             }
  122.  
  123.  
  124.  
  125.             Imgproc.cvtColor(frame, frameBW, Imgproc.COLOR_RGB2GRAY);
  126.  
  127.             double t = 0.5;
  128.             Imgproc.threshold(frameBW, frameMono, t * 255, 255, Imgproc.THRESH_BINARY);
  129.  
  130.             frameDrawing = frame.clone();
  131.  
  132.             List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
  133.             Mat hierarchy = new Mat();
  134.  
  135.  
  136.             Imgproc.findContours(frameMono, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
  137.  
  138.             Scalar green = new Scalar(0,255,0);
  139.  
  140.             MatOfPoint2f approx = new MatOfPoint2f();
  141.  
  142.             List<List<Point>> squares = new ArrayList<List<Point>>();
  143.  
  144.             for(int i = 0; i < contours.size(); i++) {
  145.                 MatOfPoint2f poly = new MatOfPoint2f( contours.get(i).toArray() );
  146.                 Imgproc.approxPolyDP( poly, approx, 3, true);
  147.  
  148.                 double area = Imgproc.contourArea(approx,true);
  149.                 boolean clockwise = area > 0;
  150.                 if(approx.size().area() == 4 && Math.abs(area) > 500 && clockwise) {
  151.                     Scalar color = new Scalar((clockwise ? 0 : 255), (clockwise ? 255 : 0 ), 0);
  152.  
  153.                     Point prev = null;
  154.                     Point current = null;
  155.                     System.out.println("approx length: " + approx.size());
  156.                     System.out.println("Area is " + area );
  157.  
  158.                     List<Point> square = new ArrayList<Point>();
  159.  
  160.                     for(int j  =0; j < approx.size().height  ; j++ ) {
  161.                         for (int k = 0; k < approx.size().width; k++) {
  162.                             System.out.println(j + ", " + k);
  163.  
  164.                             double[] p = approx.get(j, k);
  165.                             System.out.println(p[0] + ", " + p[1]);
  166.                             prev = current;
  167.                             current = new Point(p[0],p[1]);
  168.  
  169.                             square.add(current);
  170.  
  171.                             color = new Scalar(j * (255/3) , 128, 128);
  172.  
  173.                             Core.circle(frameDrawing, current, 10,color);
  174.  
  175.                             if(current != null && prev != null) {
  176.                                 System.out.println("Drawing line");
  177.                                 Core.line(frameDrawing, prev, current, color, 5);
  178.  
  179.                             }
  180.  
  181.  
  182.                         }
  183.                     }
  184.                     // Add square to array
  185.                     squares.add(square);
  186.  
  187.                     //Render line from last to first on image
  188.                     double[] p = approx.get(0, 0);
  189.                     System.out.println("First: " + p[0] + ", " + p[1]);
  190.                     Point first = new Point(p[0], p[1]);
  191.                     Core.line(frameDrawing, first, current, color, 5);
  192.                     MatOfPoint points = new MatOfPoint(approx.toArray());
  193.  
  194.  
  195.                   //  Core.rectangle(
  196.                   //          frameMono,
  197.                   //          new Point(rect.x+rect.width,rect.y+rect.height),
  198.                   //          new Point(rect.x+rect.width,rect.y+rect.height) , new Scalar(255, 0, 0, 255), 3); // FIXME Coordinates
  199.                     //Point(rect.x+rect.width,rect.y+rect.height) , new Scalar(255, 0, 0, 255), 3);
  200.  
  201.                 }
  202.  
  203.             }
  204.  
  205.             System.out.println("Total of " + squares.size() + " detected");
  206.  
  207.             UtilAR.imDrawBackground(frameDrawing);
  208.  
  209.             if(squares.size() == 1) {
  210.  
  211.                 List<Point> corners = new ArrayList<Point>();
  212.                 for (int i = 0; i < squares.size(); i++) {
  213.                     List<Point> square = squares.get(i);
  214.                     corners2d.fromList(square);
  215.                 }
  216.  
  217.                 MatOfDouble distortionCoefficients = UtilAR.getDefaultDistortionCoefficients();
  218.                 Mat cameraMatrix = UtilAR.getDefaultIntrinsicMatrixMads(frame.width(), frame.height());
  219.                 corners3d = getCorner3f();
  220.  
  221.                 Mat rotationVector = new Mat();
  222.                 Mat transformationVector =  new Mat();
  223.  
  224.                 System.out.println(corners2d.toList().toString());
  225.                 System.out.println(corners3d.toList().toString());
  226.  
  227.                 Calib3d.solvePnP(corners3d,
  228.                         corners2d,
  229.                         cameraMatrix,
  230.                         distortionCoefficients,
  231.                         rotationVector,
  232.                         transformationVector);
  233.  
  234.                 System.out.println("Rotation: " + rotationVector.dump());
  235.                 System.out.println("Transform: " + transformationVector.dump());
  236.  
  237.                 UtilAR.setCameraByRT(rotationVector, transformationVector, cam);
  238.  
  239.                 System.out.println("Camera position: " + cam.position.toString());
  240.  
  241.                 int cols = frame.cols()/5;
  242.                 int rows = frame.rows()/5;
  243.                 List<Point> cornerList = new ArrayList<Point>();
  244.                 cornerList.add(new Point(0,0));
  245.                 cornerList.add(new Point(cols,0));
  246.                 cornerList.add(new Point(cols,rows));
  247.                 cornerList.add(new Point(0,rows));
  248.  
  249.                 MatOfPoint2f obj_corners = new MatOfPoint2f();
  250.                 obj_corners.fromList(cornerList);
  251.  
  252.                 Mat trans = Imgproc.getPerspectiveTransform(corners2d, obj_corners);
  253.                 Mat transformedMat = new Mat();
  254.  
  255.                 Imgproc.warpPerspective(
  256.                         frame,
  257.                         transformedMat,
  258.                         trans,
  259.                         new Size(200,200),
  260.                         Imgproc.INTER_LINEAR,
  261.                         Imgproc.BORDER_CONSTANT,
  262.                         green);
  263.  
  264.                 Core.flip(transformedMat,transformedMat, 1);
  265.  
  266.                 Mat preview = frame.colRange(0,200).rowRange(0,200);
  267.                 transformedMat.copyTo(preview);
  268.  
  269.                 UtilAR.imDrawBackground(frame);
  270.                 batch.begin(cam);
  271.                 for(ModelInstance i : arrows) {
  272.                     batch.render(i, environment);
  273.                 }
  274.                 batch.end();
  275.             }
  276.  
  277.         }
  278.  
  279.         MatOfPoint3f getCorner3f() {
  280.             MatOfPoint3f corners3f = new MatOfPoint3f();
  281.             Point3[] vp = new Point3[4];
  282.             vp[0] = new Point3(0,0,0);
  283.             vp[1] = new Point3(0,1,0);
  284.             vp[2] = new Point3(1,1,0);
  285.             vp[3] = new Point3(1,0,0);
  286.  
  287.             corners3f.fromArray(vp);
  288.             return corners3f;
  289.         }
  290.  
  291.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement