Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
- // Load the image
- String path = "/home/bikz05/Desktop/geubs.png";
- Mat original = Highgui.imread(path);
- Mat image = new Mat();
- Imgproc.cvtColor(original, image, Imgproc.COLOR_BGR2GRAY);
- // Threshold the image
- Mat threshold = new Mat();
- Imgproc.threshold(image, threshold, 127, 255, 1);
- // Find the contours
- List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
- Imgproc.findContours(threshold, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
- // Get contour index with largest area
- double max_area = -1;
- int index = 0;
- for(int i=0; i< contours.size();i++) {
- if (Imgproc.contourArea(contours.get(i)) > max_area) {
- max_area = Imgproc.contourArea(contours.get(i));
- index = i;
- }
- }
- // Approximate the largest contour
- MatOfPoint2f approxCurve = new MatOfPoint2f();
- MatOfPoint2f oriCurve = new MatOfPoint2f( contours.get(index).toArray() );
- Imgproc.approxPolyDP(oriCurve, approxCurve, 6.0, true);
- // Draw contour points on the original image
- Point [] array = approxCurve.toArray();
- for(int i=0; i < array.length;i++) {
- Core.circle(original, array[i], 2, new Scalar(0, 0 ,255), 2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement