Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1.  
  2. import java.util.LinkedList;
  3. import java.util.Random;
  4. import Jama.Matrix;
  5.  
  6. public class Ransac
  7. {
  8. private LinkedList<Point[]> couples = new LinkedList<Point[]>();
  9.  
  10. public Ransac(LinkedList<Point[]> couples)
  11. {
  12. this.couples = couples;
  13. }
  14.  
  15. public LinkedList<Point[]> runRansac(int iter, double maxerror)
  16. {
  17. LinkedList<Point[]> newCouples = new LinkedList<Point[]>();
  18. Matrix model = algorithm(iter, maxerror);
  19. for(int i = 0; i < couples.size(); i++)
  20. {
  21. if(couples.get(i)[1].getDistance() < maxerror)
  22. {
  23. newCouples.add(couples.get(i));
  24. }
  25. }
  26. return newCouples;
  27. }
  28.  
  29. private Matrix algorithm(int iter, double maxerror)
  30. {
  31. Matrix bestmodel = null;
  32. double bestscore = 0;
  33. double error = 0;
  34.  
  35. for(int i = 0; i < iter; i++)
  36. {
  37. Matrix model = null;
  38. while(model == null)
  39. {
  40. Point[][] samples = heuristicChoice(8, 200);
  41. System.out.println("Punkt kontrolny " + i);
  42. model = calculateModel(samples);
  43. }
  44. System.out.println("Koniec pΔ™tli while");
  45. double score = 0;
  46. for(int j = 0; j < couples.size(); j++)
  47. {
  48. error = modelError(model, couples.get(j)[0], couples.get(j)[1]);
  49. couples.get(j)[1].setDistance(error);
  50. if(error < maxerror)
  51. score++;
  52. }
  53. if(score > bestscore)
  54. {
  55. bestscore = score;
  56. bestmodel = model;
  57. }
  58. System.out.println("Koniec iteracji " + i);
  59. }
  60. return bestmodel;
  61. }
  62.  
  63. private Point[][] randomChoice()
  64. {
  65. Point[][] chosen = new Point[3][2];
  66. Random rand = new Random();
  67. int position = 0;
  68. for(int i = 0; i < 3; i++)
  69. {
  70. position = rand.nextInt(couples.size());
  71. chosen[i][0] = couples.get(position)[0];
  72. chosen[i][1] = couples.get(position)[1];
  73. }
  74. return chosen;
  75. }
  76.  
  77. private Point[][] heuristicChoice(double r, double R)
  78. {
  79. System.out.println("Obliczanie heurystyki - start");
  80. Point[][] chosen = new Point[3][2];
  81. Random rand = new Random();
  82. int position = rand.nextInt(couples.size());
  83. System.out.println("Obliczanie heurystyki - 1");
  84. chosen[0][0] = couples.get(position)[0];
  85. chosen[0][1] = couples.get(position)[1];
  86. System.out.println("Obliczanie heurystyki - 2");
  87. position = rand.nextInt(couples.size());
  88. while(!checkPoints(chosen[0][0], chosen[0][1], couples.get(position)[0], couples.get(position)[1], r, R))
  89. {
  90. position = rand.nextInt(couples.size());
  91. }
  92. chosen[1][0] = couples.get(position)[0];
  93. chosen[1][1] = couples.get(position)[1];
  94. System.out.println("Obliczanie heurystyki - 3");
  95. position = rand.nextInt(couples.size());
  96. while(!checkPoints(chosen[0][0], chosen[0][1], couples.get(position)[0], couples.get(position)[1], r, R) && checkPoints(chosen[1][0], chosen[1][1], couples.get(position)[0], couples.get(position)[1], r, R))
  97. position = rand.nextInt(couples.size());
  98. chosen[2][0] = couples.get(position)[0];
  99. chosen[2][1] = couples.get(position)[1];
  100. System.out.println("Obliczanie heurystyki - koniec");
  101. return chosen;
  102. }
  103.  
  104. private boolean checkPoints(Point p1, Point p2, Point p3, Point p4, double r, double R)
  105. {
  106. double condition1 = Math.pow((p1.getX() - p3.getX()), 2) + Math.pow((p1.getY() - p3.getY()), 2);
  107. double condition2 = Math.pow((p2.getX() - p4.getX()), 2) + Math.pow((p2.getY() - p4.getY()), 2);
  108. System.out.println("Obliczanie heurystyki - 2a");
  109. return condition1 > r * r && condition1 < R * R && condition2 > r * r && condition2 < R * R;
  110. }
  111.  
  112. private Matrix calculateModel(Point[][] samples)
  113. {
  114. double[][] array = new double[][]
  115. {
  116. {samples[0][0].getX(),samples[0][0].getY(),1,0,0,0},
  117. {samples[1][0].getX(),samples[1][0].getY(),1,0,0,0},
  118. {samples[2][0].getX(),samples[2][0].getY(),1,0,0,0},
  119. {0,0,0,samples[0][0].getX(),samples[0][0].getY(),1},
  120. {0,0,0,samples[1][0].getX(),samples[1][0].getY(),1},
  121. {0,0,0,samples[2][0].getX(),samples[2][0].getY(),1},
  122. };
  123. Matrix matrix1 = new Matrix(array);
  124. try
  125. {
  126. matrix1 = matrix1.inverse();
  127. }
  128. catch(Exception e)
  129. {
  130. e.printStackTrace();
  131. return null;
  132. }
  133. array = new double[][]
  134. {
  135. {samples[0][1].getX()},
  136. {samples[1][1].getX()},
  137. {samples[2][1].getX()},
  138. {samples[0][1].getY()},
  139. {samples[1][1].getY()},
  140. {samples[2][1].getY()}
  141. };
  142. Matrix matrix2 = new Matrix(array);
  143. double[][] result = matrix1.times(matrix2).getArray();
  144.  
  145. array = new double[][]
  146. {
  147. {result[0][0], result[1][0], result[2][0]},
  148. {result[3][0], result[4][0], result[5][0]},
  149. {0, 0, 1}
  150. };
  151.  
  152. return new Matrix(array);
  153. }
  154.  
  155. private double modelError(Matrix model, Point p1, Point p2)
  156. {
  157. double[][] mod = model.getArray();
  158. double error = (mod[0][0] * p1.getX() + mod[0][1] * p1.getY() + mod[0][2]
  159. - p2.getX()) * (mod[0][0] * p1.getX() + mod[0][1] * p1.getY() + mod[0][2] - p2.getX())
  160. + (mod [1][0] * p1.getX() + mod[1][1] * p1.getY() + mod[1][2]
  161. - p2.getY()) * (mod[1][0] * p1.getX() + mod[1][1] * p1.getY() + mod[1][2] - p2.getY());
  162. return Math.sqrt(error);
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement