code: public static void fitBinaryImage(GrayF32 input) {
  
  		GrayU8 binary = new GrayU8(input.width,input.height);
  		BufferedImage polygon = new BufferedImage(input.width,input.height,BufferedImage.TYPE_INT_RGB);
  
  		// the mean pixel value is often a reasonable threshold when creating a binary image
  		double mean = ImageStatistics.mean(input);
  
  		// create a binary image by thresholding
  		ThresholdImageOps.threshold(input, binary, (float) mean, true);
  
  		// reduce noise with some filtering
  		GrayU8 filtered = BinaryImageOps.erode8(binary, 1, null);
  		filtered = BinaryImageOps.dilate8(filtered, 1, null);
  
  		// Find internal and external contour around each shape
  		List<Contour> contours = BinaryImageOps.contour(filtered, ConnectRule.EIGHT,null);
  
  		// Fit a polygon to each shape and draw the results
  		Graphics2D g2 = polygon.createGraphics();
  		g2.setStroke(new BasicStroke(2));
  
  		for( Contour c : contours ) {
  			// Fit the polygon to the found external contour.  Note loop = true
  			List<PointIndex_I32> vertexes = ShapeFittingOps.fitPolygon(c.external,true, minSide,cornerPenalty);
  
  			g2.setColor(Color.RED);
  			VisualizeShapes.drawPolygon(vertexes,true,g2);
  
  			// handle internal contours now
  			g2.setColor(Color.BLUE);
  			for( List<Point2D_I32> internal : c.internal ) {
  				vertexes = ShapeFittingOps.fitPolygon(internal,true, minSide,cornerPenalty);
  				VisualizeShapes.drawPolygon(vertexes,true,g2);
  			}
  		}
  
  		gui.addImage(polygon, "Binary Blob Contours");
  	}
docs: Fits polygons to found contours around binary blobs.