图像处理之基于Otsu阈值二值化

简介: 图像处理之基于Otsu阈值二值化

图像处理之基于Otsu阈值实现图像二值化

一:基本原理

该方法是图像二值化处理常见方法之一,在Matlab与OpenCV中均有实现。

Otsu Threshing方法是一种基于寻找合适阈值实现二值化的方法,其最重

要的部分是寻找图像二值化阈值,然后根据阈值将图像分为前景(白色)

或者背景(黑色)。假设有6x6的灰度图像,其像素数据及其对应的直方

图如下图:

阈值寻找方法首先假设是为T=3,则背景像素的比重、均值、方差的计算

结果如下:


根据前景像素直方图,计算比重、均值、方差的过程如下:


上述整个计算步骤与结果是假设阈值T=3时候的结果,同样计算假设阈值为

T=0、T=1、T=2、T=4、T=5的类内方差,比较类内方差之间的值,最小类

内方差使用的阈值T即为图像二值化的阈值。上述是假设图像灰度值级别为

0~5六个值,实际中图像灰度值取值范围为0~255之间,所以要循环计算

使用每个灰度值作为阈值,得到类内方差,最终取最小类内方差对应的灰度

值作为阈值实现图像二值化即可。

二:代码实现

package com.gloomyfish.filter.study;
 
import java.awt.image.BufferedImage;
 
public class OtsuBinaryFilter extends AbstractBufferedImageOp {
  
  public OtsuBinaryFilter()
  {
    System.out.println("Otsu Threshold Binary Filter...");
  }
 
  @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];
        getRGB( src, 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);
        inPixels[index]  = (ta << 24) | (gray << 16) | (gray << 8) | gray;
          }
        }
        // 获取直方图
        int[] histogram = new int[256];
        for(int row=0; row<height; row++) {
          int tr = 0;
          for(int col=0; col<width; col++) {
            index = row * width + col;
                tr = (inPixels[index] >> 16) & 0xff;
                histogram[tr]++;
          }
        }
        // 图像二值化 - OTSU 阈值化方法
        double total = width * height;
        double[] variances = new double[256];
        for(int i=0; i<variances.length; i++)
        {
          double bw = 0;
          double bmeans = 0;
          double bvariance = 0;
          double count = 0;
          for(int t=0; t<i; t++)
          {
            count += histogram[t];
            bmeans += histogram[t] * t;
          }
          bw = count / total;
          bmeans = (count == 0) ? 0 :(bmeans / count);
          for(int t=0; t<i; t++)
          {
            bvariance += (Math.pow((t-bmeans),2) * histogram[t]);
          }
          bvariance = (count == 0) ? 0 : (bvariance / count);
          double fw = 0;
          double fmeans = 0;
          double fvariance = 0;
          count = 0;
          for(int t=i; t<histogram.length; t++)
          {
            count += histogram[t];
            fmeans += histogram[t] * t;
          }
          fw = count / total;
          fmeans = (count == 0) ? 0 : (fmeans / count);
          for(int t=i; t<histogram.length; t++)
          {
            fvariance += (Math.pow((t-fmeans),2) * histogram[t]);
          }
          fvariance = (count == 0) ? 0 : (fvariance / count);
          variances[i] = bw * bvariance + fw * fvariance;
        }
 
        // find the minimum within class variance
        double min = variances[0];
        int threshold = 0;
        for(int m=1; m<variances.length; m++)
        {
          if(min > variances[m]){
            threshold = m;
            min = variances[m];
          }
        }
        // 二值化
        System.out.println("final threshold value : " + threshold);
        for(int row=0; row<height; row++) {
          for(int col=0; col<width; col++) {
            index = row * width + col;
                int gray = (inPixels[index] >> 8) & 0xff;
                if(gray > threshold)
                {
                  gray = 255;
                  outPixels[index]  = (0xff << 24) | (gray << 16) | (gray << 8) | gray;
                }
                else
                {
                  gray = 0;
                  outPixels[index]  = (0xff << 24) | (gray << 16) | (gray << 8) | gray;
                }
        
          }
        }
        setRGB(dest, 0, 0, width, height, outPixels );
        return dest;
  }
 
}

转载文章请注明,博主在此祝各位2015年工作顺利,请继续关注!

相关文章
|
Serverless Linux API
函数计算常见问题之镜像加速失败如何解决
函数计算(Function Compute, FC)是阿里云提供的无服务器计算服务,它允许用户在无需管理服务器的情况下运行代码,但在配置和执行过程中可能遇到报错,本合集致力于梳理FC服务中的常见报错和配置问题,并提供解决方案,帮助用户优化函数执行环境。
284 1
【鸿蒙4.0】ArkUI组件-Image
【鸿蒙4.0】ArkUI组件-Image应用及需要注意的问题
1167 3
|
SQL Oracle 关系型数据库
SQL 数据操作技巧:SELECT INTO、INSERT INTO SELECT 和 CASE 语
SELECT INTO 语句将数据从一个表复制到一个新表中。
955 1
|
Go 开发工具
百炼-千问模型通过openai接口构建assistant 等 go语言
由于阿里百炼平台通义千问大模型没有完善的go语言兼容openapi示例,并且官方答复assistant是不兼容openapi sdk的。 实际使用中发现是能够支持的,所以自己写了一个demo test示例,给大家做一个参考。
|
Unix Linux Docker
CentOS停更沉寂,RHEL巨变限制源代:Docker容器化技术的兴起助力操作系统新格局
操作系统是计算机系统的核心软件,管理和控制硬件与软件资源,为用户和应用程序提供高效、安全的运行环境。Linux作为开源、跨平台的操作系统,具有高度可定制性、稳定性和安全性,广泛应用于服务器、云计算、物联网等领域。其发展得益于庞大的社区支持,多种发行版如Ubuntu、Debian、Fedora等满足不同需求。
410 5
|
存储
硬盘数据恢复—硬盘出现物理坏道如何妙手回春?
硬盘故障: 硬盘存在物理坏道。 硬盘存在物理坏道的典型表现: 1、若硬盘为系统盘,故障表现通常为:操作系统异常缓慢、蓝屏,重启系统后引导失败并报告硬盘读取出错、系统自动反复重启。 2、若硬盘为移动硬盘,故障表现通常为:电脑无法加载盘符、提示硬盘需要格式化、频繁提示需要运行chkdsk来检查和修复磁盘、数据读取缓慢、死机。
|
机器学习/深度学习 Python
训练集、测试集与验证集:机器学习模型评估的基石
在机器学习中,数据集通常被划分为训练集、验证集和测试集,以评估模型性能并调整参数。训练集用于拟合模型,验证集用于调整超参数和防止过拟合,测试集则用于评估最终模型性能。本文详细介绍了这三个集合的作用,并通过代码示例展示了如何进行数据集的划分。合理的划分有助于提升模型的泛化能力。
|
C++
软件安装(一):VS2017安装和使用
本文主要介绍了如何下载和安装Visual Studio 2017,包括选择安装组件、修改安装位置以及解决安装过程中可能遇到的问题。
891 3
软件安装(一):VS2017安装和使用
|
存储 SQL 关系型数据库
SELECT INTO
【11月更文挑战第08天】
472 2