Here you can find the source of erodeImage(final BufferedImage img, int structElementWidth, int structElementHeight, final int rgbForegroundColor, final int rgbBackgroundColor)
Parameter | Description |
---|---|
img | the image to erode |
structElementWidth | Width of the structure element mask |
structElementHeight | Height of the structure element mask |
rgbForegroundColor | Foreground color |
rgbBackgroundColor | Background color |
protected static BufferedImage erodeImage(final BufferedImage img, int structElementWidth, int structElementHeight, final int rgbForegroundColor, final int rgbBackgroundColor)
//package com.java2s; //License from project: Open Source License import java.awt.image.BufferedImage; public class Main { /**//from w w w . j a v a 2 s .c o m * Creates and returns an erosion image, using the algorithm from morphological image processing. * <p> * Assumes the structuring element is filled with ones and thereby only needs it's width and height. The origin is * placed in the middle of the structuring element. If width and/ or height are even, they are incremented to make * sure there is a middle pixel. * * @param img * the image to erode * @param structElementWidth Width of the structure element mask * @param structElementHeight Height of the structure element mask * @param rgbForegroundColor Foreground color * @param rgbBackgroundColor Background color * @return The eroded image as BufferedImage */ protected static BufferedImage erodeImage(final BufferedImage img, int structElementWidth, int structElementHeight, final int rgbForegroundColor, final int rgbBackgroundColor) { final BufferedImage erosionedImage = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB); boolean fits; // The origin of the structuring element will be it's middle pixel // Therefore make sure there is a middle pixel, ie make width and height // uneven. if ((structElementWidth % 2) == 0) { structElementWidth++; } if ((structElementHeight % 2) == 0) { structElementHeight++; } // Metaphorically places the structure element // In every possible position for (int w = 0; w < img.getWidth(); w++) { for (int h = 0; h < img.getHeight(); h++) { fits = true; // The origin of the structuring element is it's middle pixel for (int x = w - (structElementWidth / 2); x <= w + (structElementWidth / 2); x++) { for (int y = h - (structElementHeight / 2); y <= h + (structElementHeight / 2); y++) { // As long as the pixels not over the border if (x >= 0 && x < img.getWidth() && y >= 0 && y < img.getHeight()) { // Assumes all the pixels in the structureImage are // 1. If the pixel does not have the right color // black, set fits false, set the pixel in the // erosionImage to the foreground color and break // the loop if (img.getRGB(x, y) != rgbForegroundColor) { fits = false; erosionedImage.setRGB(w, h, rgbBackgroundColor); break; } } } } // After every pixel was checked and if fits is true // Set the pixel in the erosionImage black // Some room for performance increase with a better break? if (fits) { erosionedImage.setRGB(w, h, rgbForegroundColor); } } } return erosionedImage; } }