天天看点

图像处理------ 二值腐蚀

概述:

腐蚀是图像形态学的两个基本操作之一,另外一个是膨胀(dilate)。二值图像上的腐蚀是腐蚀最典

型的运用,但是腐蚀操作同样可以运用于灰度图像。二值图像腐蚀操作最基本的效果是腐蚀图像

中前景色区域的边缘。使得前景图像区域变小,前景图像内部的背景区域被放大。

基本原理:

腐蚀操作要求有待处理的2d图像f(x,y)以及操作数矩阵(类似卷积操作中的kernel矩阵),常见的

为3x3的操作数矩阵。二值图像腐蚀操作的数学定义如下:

1.      假设x是二值图像中所有像素欧几里德坐标的集合,k为3x3的操作数矩阵

2.       kx表示操作数处理x的结果,x表示起始像素点

3.      腐蚀操作k对x的所有像素点操作,kx是x所有像素点的子集。

一个二值图像腐蚀的例子如下,操作数矩阵为3x3,起始点为中心像素点,前景像素表示为1,背

景像素表示为0.图示如下:

图像处理------ 二值腐蚀

当操作数在像素矩阵上移动时,任何一个在操作数之下的输入像素为背景像素时,则设置中心像素

为背景像素0,否则中心像素[0,0]下的输入像素值不变。

三:程序效果

图像处理------ 二值腐蚀

package com.gloomyfish.morphology;  

import java.awt.color;  

import java.awt.image.bufferedimage;  

public class erosionfilter extends binaryfilter {  

    private color backgroundcolor;  

    public erosionfilter() {  

        backgroundcolor = color.white;  

    }  

    public color getbackcolor() {  

        return backgroundcolor;  

    public void setbackcolor(color forgecolor) {  

        this.backgroundcolor = forgecolor;  

    @override  

    public bufferedimage filter(bufferedimage src, bufferedimage dest) {  

        int width = src.getwidth();  

        int height = src.getheight();  

        if ( dest == null )  

            dest = createcompatibledestimage( src, null );  

        int[] inpixels = new int[width*height];  

        int[] outpixels = new int[width*height];  

        src = super.filter(src, null); // we need to create new one  

        getrgb( src, 0, 0, width, height, inpixels );  

        int index = 0, index1 = 0, newrow = 0, newcol = 0;  

        int ta1 = 0, tr1 = 0, tg1 = 0, tb1 = 0;  

        for(int row=0; row<height; row++) {  

            int ta = 0, tr = 0, tg = 0, tb = 0;  

            for(int col=0; col<width; col++) {  

                index = row * width + col;  

                ta = (inpixels[index] >> 24) & 0xff;  

                tr = (inpixels[index] >> 16) & 0xff;  

                tg = (inpixels[index] >> 8) & 0xff;  

                tb = inpixels[index] & 0xff;  

                boolean erosion = false;  

                for(int offsety=-1; offsety<=1; offsety++) {  

                    for(int offsetx=-1; offsetx<=1; offsetx++) {  

                        if(offsety==0 && offsetx==0) {  

                            continue;  

                        }  

                        newrow = row + offsety;  

                        newcol = col + offsetx;  

                        if(newrow <0 || newrow >=height) {  

                            newrow = 0;  

                        if(newcol < 0 || newcol >=width) {  

                            newcol = 0;  

                        index1 = newrow * width + newcol;  

                        ta1 = (inpixels[index1] >> 24) & 0xff;  

                        tr1 = (inpixels[index1] >> 16) & 0xff;  

                        tg1= (inpixels[index1] >> 8) & 0xff;  

                        tb1 = inpixels[index1] & 0xff;  

                        if(tr1 == backgroundcolor.getred() && tg1 == tb1) {  

                            erosion = true;  

                            break;  

                    }  

                    if(erosion){  

                        break;  

                }  

                if(erosion) {  

                    tr = tg = tb = backgroundcolor.getred();  

                } else {  

                    tr = tg = tb = 255 - backgroundcolor.getred();  

                outpixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;  

            }  

        }  

        setrgb( dest, 0, 0, width, height, outpixels );  

        return dest;  

}  

继续阅读