二值图像分析笔记(6)——膨胀(Dilation)

1 膨胀

  • DilationFilter
package binimage.dilation;

import binimage.binary.BinaryFilter;

import java.awt.image.BufferedImage;

public class DilationFilter extends BinaryFilter {
    // 前景像素值
    private int fcolor;

    public DilationFilter() {
        fcolor = 0; // 前景是黑色
    }

    @Override
    public BufferedImage process(BufferedImage image) {
        BufferedImage binImage = super.process(image);
        int width = binImage.getWidth();
        int height = binImage.getHeight();
        int[] pixels = new int[width * height];
        int[] output = new int[width * height];

        getRGB(binImage, 0, 0, width, height, pixels);
        System.arraycopy(pixels, 0, output, 0, pixels.length);

        int p1 = 0, p2 = 0, p3 = 0;
        int p4 = 0, p5 = 0, p6 = 0;
        int p7 = 0, p8 = 0, p9 = 0;
        int offset = 0;
        for (int row = 1; row < height - 1; row++) {
            offset = row * width;
            for (int col = 1; col < width - 1; col++) {
                p1 = (pixels[offset - width + col - 1] >> 16) & 0xff;
                p2 = (pixels[offset - width + col] >> 16) & 0xff;
                p3 = (pixels[offset - width + col + 1] >> 16) & 0xff;
                p4 = (pixels[offset + col - 1] >> 16) & 0xff;
                p5 = (pixels[offset + col] >> 16) & 0xff;
                p6 = (pixels[offset + col + 1] >> 16) & 0xff;
                p7 = (pixels[offset + width + col - 1] >> 16) & 0xff;
                p8 = (pixels[offset + width + col] >> 16) & 0xff;
                p9 = (pixels[offset + width + col + 1] >> 16) & 0xff;

                // 中心像素是背景像素
                int sum = p1 + p2 + p3 + p4 + p6 + p7 + p8 + p9;
                int bc = 255 - fcolor;
                int total = bc * 8;
                if (p5 == bc && sum != total) {
                    // 说明周围有一个是前景色,中心像素要设置为前景
                    output[offset + col] = (0xff << 24) | ((fcolor & 0xff) << 16) | ((fcolor & 0xff) << 8) | (fcolor & 0xff);
                }

            }
        }

        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        setRGB(bi, 0, 0, width, height, output);

        return bi;

    }

    public int getFcolor() {
        return fcolor;
    }

    public void setFcolor(int fcolor) {
        this.fcolor = fcolor;
    }
}

  • ImagePanel
 public void process() {
        DilationFilter filter = new DilationFilter();
        BufferedImage tempImage = null;
        filter.setFcolor(0);
        tempImage = filter.process(image);

        for (int i = 0; i < 5; i++) {
            tempImage = filter.process(tempImage);
        }

        resultImage = tempImage;
    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值