天天看点

图像处理------ 二值膨胀及应用

基本原理:

膨胀是图像形态学的两个基本操作之一,另外一个是腐蚀操作。最典型的应用是在二值图像

中使用这两个基本操作,是很多识别技术中重要的中间处理步骤。在灰度图像中根据阈值同

样可以完成膨胀与腐蚀操作。对一幅二值图像f(x,y)完成膨胀操作,与对图像的卷积操作类

似,要有个操作数矩阵,最常见的为3x3的矩阵,与卷积操作不同的,是如果矩阵中的像素

点有任意一个点的值是前景色,则设置中心像素点为前景色,否则不变。

程序效果:(上为源图,下为膨胀以后效果)

图像处理------ 二值膨胀及应用

程序原理:

首先把一幅彩色图像转换为灰度图像,转换方法参见这里

<a target="_blank" href="http://blog.csdn.net/jia20003/article/details/7392325">http://blog.csdn.net/jia20003/article/details/7392325</a>

然根据像素平均值作为阈值,转换为二值图像,转换方法参见这里

最后在二值图像上使用膨胀操作,输出处理以后图像

源代码:

package com.gloomyfish.morphology;  

import java.awt.color;  

import java.awt.image.bufferedimage;  

public class dilatefilter extends binaryfilter {  

    public dilatefilter() {  

        forgecolor = color.white;  

    }  

    private color forgecolor;  

    public color getforgecolor() {  

        return forgecolor;  

    public void setforgecolor(color forgecolor) {  

        this.forgecolor = 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&lt;height; row++) {  

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

            for(int col=0; col&lt;width; col++) {  

                index = row * width + col;  

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

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

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

                tb = inpixels[index] &amp; 0xff;  

                boolean dilation = false;  

                for(int offsety=-1; offsety&lt;=1; offsety++) {  

                    for(int offsetx=-1; offsetx&lt;=1; offsetx++) {  

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

                            continue;  

                        }  

                        newrow = row + offsety;  

                        newcol = col + offsetx;  

                        if(newrow &lt;0 || newrow &gt;=height) {  

                            newrow = 0;  

                        if(newcol &lt; 0 || newcol &gt;=width) {  

                            newcol = 0;  

                        index1 = newrow * width + newcol;  

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

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

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

                        tb1 = inpixels[index1] &amp; 0xff;  

                        if(tr1 == forgecolor.getred() &amp;&amp; tg1 == tb1) {  

                            dilation = true;  

                            break;  

                    }  

                    if(dilation){  

                        break;  

                }  

                if(dilation) {  

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

                } else {  

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

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

            }  

        }  

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

        return dest;  

}  

其实,膨胀还可以被用来进行对二值图像完成边缘提取,其基本做法如下:

1. 对一副黑白的图像完成膨胀操作

2.将膨胀以后的图像与原来的图像在每个像素位上相减

3.显示相减以后的图像,即得到边缘。