图像处理之积分图应用四(基于局部均值的图像二值化算法)

简介: 图像处理之积分图应用四(基于局部均值的图像二值化算法)基本原理 均值法,选择的阈值是局部范围内像素的灰度均值(gray mean),该方法的一个变种是用常量C减去均值Mean,然后根据均值实现如下操作: pixel = (pixel > (mean - c)) ? object : background 其中默认情况下参数C取值为0。

图像处理之积分图应用四(基于局部均值的图像二值化算法)

基本原理
均值法,选择的阈值是局部范围内像素的灰度均值(gray mean),该方法的一个变种是用常量C减去均值Mean,然后根据均值实现如下操作:
pixel = (pixel > (mean - c)) ? object : background
其中默认情况下参数C取值为0。object表示前景像素,background表示背景像素。

实现步骤
1. 彩色图像转灰度图像
2. 获取灰度图像的像素数据,预计算积分图
3. 根据输入的参数窗口半径大小从积分图中获取像素总和,求得平均值
4.循环每个像素,根据局部均值实现中心像素的二值化赋值
5.输入二值图像

运行结果:
这里写图片描述

代码实现:

package com.gloomyfish.ii.demo;

import java.awt.image.BufferedImage;

public class FastMeanBinaryFilter extends AbstractImageOptionFilter {

    private int constant;
    private int radius;
    public FastMeanBinaryFilter() {
        constant = 10;
        radius = 7; // 1,2,3,4,5,6,7,8
    }

    public int getConstant() {
        return constant;
    }

    public void setConstant(int constant) {
        this.constant = constant;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    @Override
    public BufferedImage process(BufferedImage image) {
        int width = image.getWidth();
        int height = image.getHeight();

        BufferedImage dest = createCompatibleDestImage( image, null );
        // 图像灰度化
        int[] inPixels = new int[width*height];
        int[] outPixels = new int[width*height];
        byte[] binData = new byte[width*height];
        getRGB( image, 0, 0, width, height, inPixels );
        int index = 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;
                int gray= (int)(0.299 *tr + 0.587*tg + 0.114*tb);
                binData[index] = (byte)gray;
            }
        }

        // per-calculate integral image
        IntIntegralImage grayii = new IntIntegralImage();
        grayii.setImage(binData);
        grayii.process(width, height);
        int yr = radius;
        int xr = radius;
        int size = (yr * 2 + 1)*(xr * 2 + 1);
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                index = row * width + col;

                // 计算均值
                int sr = grayii.getBlockSum(col, row, (yr * 2 + 1), (xr * 2 + 1));
                int mean = sr / size;
                int pixel = binData[index]&0xff;

                // 二值化
                if(pixel > (mean-constant)) {
                    outPixels[row * width + col] = (0xff << 24) | (0xff << 16) | (0xff << 8) | 0xff;
                } else {
                    outPixels[row * width + col] = (0xff << 24) | (0x00 << 16) | (0x00 << 8) | 0x00;
                }
            }
        }

        // 返回结果
        setRGB(dest, 0, 0, width, height, outPixels);
        return dest;
    }


}

2017年已经开始啦!博客每个月都会有图像处理相关技术文章更新,欢迎大家继续关注!

目录
相关文章
|
14天前
|
算法 计算机视觉
使用积分图的自适应二值化算法
使用积分图的自适应二值化算法
|
26天前
|
机器学习/深度学习 存储 算法
sklearn应用线性回归算法
sklearn应用线性回归算法
24 0
|
1月前
|
算法 计算机视觉
图像处理常用算法—6个算子 !!
图像处理常用算法—6个算子 !!
28 2
|
1月前
|
存储 算法 测试技术
ArrayList集合的两个实例应用,有趣的洗牌算法与杨辉三角
ArrayList集合的两个实例应用,有趣的洗牌算法与杨辉三角
23 1
|
1月前
|
机器学习/深度学习 算法 数据库
KNN和SVM实现对LFW人像图像数据集的分类应用
KNN和SVM实现对LFW人像图像数据集的分类应用
34 0
|
1月前
|
算法
【MATLAB】语音信号识别与处理:一维信号NLM非局部均值滤波算法去噪及谱相减算法呈现频谱
【MATLAB】语音信号识别与处理:一维信号NLM非局部均值滤波算法去噪及谱相减算法呈现频谱
39 1
|
8天前
|
文字识别 算法 计算机视觉
图像倾斜校正算法的MATLAB实现:图像倾斜角检测及校正
图像倾斜校正算法的MATLAB实现:图像倾斜角检测及校正
15 0
|
8天前
|
数据采集 算法 数据可视化
R语言聚类算法的应用实例
R语言聚类算法的应用实例
85 18
R语言聚类算法的应用实例
|
8天前
|
算法 数据可视化 数据挖掘
R语言社区主题检测算法应用案例
R语言社区主题检测算法应用案例
11 0
|
13天前
|
算法 数据可视化 数据挖掘
使用Python实现K均值聚类算法
使用Python实现K均值聚类算法
17 1