Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. public static ArrayList<MatOfPoint> makeComplete(Mat mat) {
  2. System.out.println("makeComplete: START");
  3. Mat dst = new Mat();
  4. Core.copyMakeBorder(mat, dst, 10, 10, 10, 10, Core.BORDER_CONSTANT);
  5.  
  6. ArrayList<MatOfPoint> cnts = Tools.getContours(dst);
  7. ArrayList<MatOfPoint2f> opened = new ArrayList<>();
  8.  
  9. //convert to MatOfPoint2f to use approxPolyDP
  10. for (MatOfPoint m : cnts) {
  11. MatOfPoint2f temp = new MatOfPoint2f(m.toArray());
  12. opened.add(temp);
  13. System.out.println("First loop runs");
  14. }
  15.  
  16. ArrayList<MatOfPoint> closed = new ArrayList<>();
  17. for (MatOfPoint2f conts : opened) {
  18. MatOfPoint2f temp = new MatOfPoint2f();
  19. Imgproc.approxPolyDP(conts, temp, 3, true);
  20. MatOfPoint closedTemp = new MatOfPoint(temp.toArray());
  21. closed.add(closedTemp);
  22. System.out.println("Second loop runs");
  23. }
  24.  
  25. System.out.println("makeComplete: END");
  26. return closed;
  27. }
  28.  
  29. public static ArrayList<MatOfPoint> getConvexHull(Mat mat) {
  30. Mat dst = new Mat();
  31. Core.copyMakeBorder(mat, dst, 10, 10, 10, 10, Core.BORDER_CONSTANT);
  32.  
  33. ArrayList<MatOfPoint> cnts = Tools.getContours(dst);
  34. ArrayList<MatOfPoint> out = new ArrayList<MatOfPoint>();
  35. MatOfPoint mopIn = cnts.get(0);
  36. MatOfInt hull = new MatOfInt();
  37. Imgproc.convexHull(mopIn, hull, false);
  38.  
  39. MatOfPoint mopOut = new MatOfPoint();
  40. mopOut.create((int) hull.size().height, 1, CvType.CV_32SC2);
  41.  
  42. for (int i = 0; i < hull.size().height; i++) {
  43. int index = (int) hull.get(i, 0)[0];
  44. double[] point = new double[]{
  45. mopIn.get(index, 0)[0], mopIn.get(index, 0)[1]
  46. };
  47. mopOut.put(i, 0, point);
  48. }
  49.  
  50. out.add(mopOut);
  51. return out;
  52. }
  53.  
  54. // EMPTY - value of backgroung e.g. white for the sample image
  55. PixelType curPixel = EMPTY;
  56. int y = height - 1; // last row, the one we added
  57. for (int x = 0; x < width; ++x)
  58. {
  59. // img(y,x) - current pixel, is "empty"
  60. // img (y-1, x) - pixel above the current
  61. if (img(y-1, x) != img(y, x))
  62. {
  63. // pixel above isn't empty, so we make current pixel non-empty
  64. img(y, x) = img(y-1, x);
  65. // if we were drawing, then stop, otherwise - start
  66. if (curPixel != EMPTY)
  67. curPixel = img(y-1, x);
  68. else
  69. curPixel = EMPTY;
  70. }
  71. else
  72. {
  73. img(y, x) = curPixel;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement