Tags

, , , , ,

grids

From [1] smoothing of binary image is described as following

“These masks are passed over the entire image to smooth it, and this process can
be repeated until there is no change in the image. These masks begin scanning in the lower right corner of the image and process each row moving upward row by row. The pixel in the center of the mask is the target. Pixels overlaid by any square marked “X” are ignored. If the pixels overlaid by the squares marked “=” all have the same value, that is, all zeros, or all ones, then the target pixel is forced to match them to have the same value, otherwise it is not changed.”

The Code for doing this :

package smoothingbinary;

/**
 *
 * @author nayef
 */
public class SmoothingBinaryImage {

    public int[][] smoothBinaryImage(int[][] binaryImage) {

        boolean hasChange;
        int filterVal = 0;
        do {
            hasChange = false;
            for (int y = binaryImage.length - 2; y > 0; y--) {
                for (int x = binaryImage[y].length - 2; x > 0; x--) {
                    hasChange = hasChange || smoothPoint(binaryImage, y, x, filterVal);
                }
            }

            filterVal++;
            filterVal = filterVal % 4;

        } while (hasChange);

        return binaryImage;
    }

    private boolean smoothPoint(int[][] binaryImage, int y, int x, int filterVal) {

        if (filterVal == 0
                && binaryImage[y][x - 1] == binaryImage[y - 1][x - 1]
                && binaryImage[y - 1][x - 1] == binaryImage[y - 1][x]
                && binaryImage[y - 1][x] == binaryImage[y - 1][x + 1]
                && binaryImage[y - 1][x + 1] == binaryImage[y][x + 1]
                && binaryImage[y][x] != binaryImage[y][x - 1]) {

            binaryImage[y][x] = binaryImage[y][x - 1];
            return true;
        }

        if (filterVal == 1
                && binaryImage[y - 1][x] == binaryImage[y - 1][x + 1]
                && binaryImage[y - 1][x + 1] == binaryImage[y][x + 1]
                && binaryImage[y][x + 1] == binaryImage[y + 1][x + 1]
                && binaryImage[y + 1][x + 1] == binaryImage[y + 1][x]
                && binaryImage[y][x] != binaryImage[y - 1][x]) {

            binaryImage[y][x] = binaryImage[y - 1][x];
            return true;
        }

        if (filterVal == 2
                && binaryImage[y][x + 1] == binaryImage[y + 1][x + 1]
                && binaryImage[y + 1][x + 1] == binaryImage[y + 1][x]
                && binaryImage[y + 1][x] == binaryImage[y + 1][x - 1]
                && binaryImage[y + 1][x - 1] == binaryImage[y][x - 1]
                && binaryImage[y][x] != binaryImage[y][x + 1]) {

            binaryImage[y][x] = binaryImage[y][x + 1];
            return true;
        }

        if (filterVal == 3
                && binaryImage[y + 1][x] == binaryImage[y + 1][x - 1]
                && binaryImage[y + 1][x - 1] == binaryImage[y][x - 1]
                && binaryImage[y][x - 1] == binaryImage[y - 1][x - 1]
                && binaryImage[y - 1][x - 1] == binaryImage[y - 1][x]
                && binaryImage[y][x] != binaryImage[y + 1][x]) {

            binaryImage[y][x] = binaryImage[y + 1][x];
            return true;
        }

        return false;

    }
}

Results:

smoothBinary

Refernces: [1] Character Recognition Systems: A Guide for Students and Practitioners by by Mohamed Cheriet, Nawwaf Kharma, Cheng-Lin Liu and Ching Suen .

Advertisements