boofcv.alg.filter.binary.GThresholdImageOps Java Examples
The following examples show how to use
boofcv.alg.filter.binary.GThresholdImageOps.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContourHelper.java From cineast with MIT License | 6 votes |
/** * Applies a contour-detection algorithm on the provided image and returns a list of detected contours. First, the image * is converted to a BinaryImage using a threshold algorithm (Otsu). Afterwards, blobs in the image are detected using * an 8-connect rule. * * This method provides the best results if the image is a black & white, i.e. factually binary, image! * See {@link ContourHelper#segmentImageByColour(BufferedImage,float[])} to convert a coloured image to a binary image. * * @param image BufferedImage in which contours should be detected. * @return List of contours. */ public static List<Contour> getContours(BufferedImage image) { /* Draw a black frame around to image so as to make sure that all detected contours are internal contours. */ BufferedImage resized = new BufferedImage(image.getWidth() + 4, image.getHeight() + 4, image.getType()); Graphics g = resized.getGraphics(); g.setColor(Color.BLACK); g.fillRect(0,0,resized.getWidth(),resized.getHeight()); g.drawImage(image, 2,2, image.getWidth(), image.getHeight(), null); /* Convert to BufferedImage to Gray-scale image and prepare Binary image. */ GrayF32 input = ConvertBufferedImage.convertFromSingle(resized, null, GrayF32.class); GrayU8 binary = new GrayU8(input.width,input.height); GrayS32 label = new GrayS32(input.width,input.height); /* Select a global threshold using Otsu's method and apply that threshold. */ double threshold = GThresholdImageOps.computeOtsu(input, 0, 255); ThresholdImageOps.threshold(input, binary,(float)threshold,true); /* Remove small blobs through erosion and dilation; The null in the input indicates that it should internally * declare the work image it needs this is less efficient, but easier to code. */ GrayU8 filtered = BinaryImageOps.erode8(binary, 1, null); filtered = BinaryImageOps.dilate8(filtered, 1, null); /* Detect blobs inside the image using an 8-connect rule. */ return BinaryImageOps.contour(filtered, ConnectRule.EIGHT, label); }