Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  2.  
  3. // Load the image
  4. String path = "/home/bikz05/Desktop/geubs.png";
  5. Mat original = Highgui.imread(path);
  6. Mat image = new Mat();
  7. Imgproc.cvtColor(original, image, Imgproc.COLOR_BGR2GRAY);
  8.  
  9. // Threshold the image
  10. Mat threshold = new Mat();
  11. Imgproc.threshold(image, threshold, 127, 255, 1);
  12.  
  13. // Find the contours
  14. List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
  15. Imgproc.findContours(threshold, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
  16.  
  17. // Get contour index with largest area
  18. double max_area = -1;
  19. int index = 0;
  20. for(int i=0; i< contours.size();i++) {
  21. if (Imgproc.contourArea(contours.get(i)) > max_area) {
  22. max_area = Imgproc.contourArea(contours.get(i));
  23. index = i;
  24. }
  25. }
  26.  
  27. // Approximate the largest contour
  28. MatOfPoint2f approxCurve = new MatOfPoint2f();
  29. MatOfPoint2f oriCurve = new MatOfPoint2f( contours.get(index).toArray() );
  30. Imgproc.approxPolyDP(oriCurve, approxCurve, 6.0, true);
  31.  
  32. // Draw contour points on the original image
  33. Point [] array = approxCurve.toArray();
  34. for(int i=0; i < array.length;i++) {
  35. Core.circle(original, array[i], 2, new Scalar(0, 0 ,255), 2);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement