C#,图像二值化(19)——全局阈值的香巴拉算法( Shanbhag Thresholding)及源程序

news2025/1/21 1:05:15

1 算法描述(凑数)

thresholdShanbhag由Robert Haase基于G.Landini和W.Rasband的工作。自动阈值器利用ImageJ中实现的Shanbhag阈值方法,使用GPU上确定的直方图创建尽可能类似于ImageJ“应用阈值”方法的二进制图像。

thresholdShanbhag By Robert Haase based on work by G. Landini and W. Rasband The automatic thresholder utilizes the Shanbhag threshold method implemented in ImageJ using a histogram determined on the GPU to create binary images as similar as possible to ImageJ ‘Apply Threshold’ method.

Method selects the algorithm to be applied (detailed below).

The Ignore black and Ignore white options set the image histogram bins for [0] and [255] greylevels to 0 respectively. This may be useful if the digitised image has under- or over- exposed pixels.

White object on black background sets to white the pixels with values above the threshold value (otherwise, it sets to white the values less or equal to the threshold).

Set Threshold instead of Threshold (single images) sets the thresholding LUT, without changing the pixel data. This works only for single images.

It you are processing a stack, two additional options are available: Stack can be used to process all the slices (the threshold of each slice will be computed separately). If this option is left unchecked, only the current slice will be processed. Use stack histogram first computes the histogram of the whole stack, then computes the threshold based on that histogram and finally binarises all the slices with that single value. Selecting this option also selects the Stack option above automatically.

Important notes:

1. This plugin is accessed through the Image › Auto Threshold menu entry, however the thresholding methods were also partially implemented in ImageJ's thresholder applet accessible through the Image › Adjust › Threshold... menu entry. While the Auto Threshold plugin can use or ignore the extremes of the image histogram (Ignore black, Ignore white) the applet cannot: the 'default' method ignores the histogram extremes but the others methods do not. This means that applying the two commands to the same image can produce apparently different results. In essence, the Auto Threshold plugin, with the correct settings, can reproduce the results of the applet, but not the way round.

2. From version 1.12 the plugin supports thresholding of 16-bit images. Since the Auto Threshold plugin processes the full greyscale space, it can be slow when dealing with 16-bit images. Note that the ImageJ thresholder applet also processes 16-bit images, but in reality ImageJ first computes a histogram with 256 bins. Therefore, there might be differences in the results obtained on 16-bit images when using the applet and the true 16-bit results obtained with this plugin. Note that for speeding up, the histogram is bracketed to include only the range of bins that contain data (and avoid processing empty histogram bins at both extremes).

3. The result of 16 bit images and stacks (when processing all slices) is an 8 bit container showing the result in white [255] to comply with the concept of "binary image" (i.e. 8 bits with 0 and 255 values). However, for stacks where only 1 slice is thresholded, the result is still a 16 bit container with the thresholded phase shown as white [65535]. This is to keep the data untouched in the remaining slices. The "Try all" option retains the 16 bit format to still show the images with methods that might fail to obtain a threshold. Images and stacks that are impossible to threshold remain unchanged.

4. The same image in 8 and 16 bits (without scaling) returns the same threshold value, however Li's method originally would return different values when the image data was offset (e.g. when adding a fixed value to all pixels). The current implementation avoids this offset-dependent problem.

5. The same image scaled by a fixed value (e.g. when multiplying all pixels by a fixed value) returns a similar threshold result (within 2 greyscale levels of the original unscaled image) for all methods except Huang, Li and Triangle due to the way these algorithms work. E.g. the Triangle method applied to an 8 bit image and to the same image converted to 16 bits with scaling can result in different threshold values. This is because the scaling from 8 to 16 bits creates empty bins in between the scaled grey values. The Triangle method (based on a geometric approach) finds those artefactual gaps in the new 16 bit histogram which satisfy the method constraints, but which would not exist in the original 8 bit image. This cannot be prevented (for example by detecting empty histogram bins) as it would interfere with the analysis when real empty bins (as oppose to artefactual ones) exist in the image.

Available methods

Try all

Which method segments your data best? One can attempt to answer this question using the Try all option. This produces a montage with results from all the methods, allowing to explore how the different algorithms perform on a particular image or stack. When using stacks, in some cases it might not be a good idea to segment each slice individually rather than with a single threshold for all slices (try the mri-stack.tif from the sample images to better understand this issue).

Error creating thumbnail: Unable to save thumbnail to destination

Original image

Error creating thumbnail: Unable to save thumbnail to destination

Try all methods.

When processing stacks with many slices, the montages can become very large (~16 times the original stack size) and one risks running out of RAM. A popup window will appear (when stacks have more than 25 slices) to confirm whether the procedure should display the montaged results. Select No to compute the threshold values and display them in the log window.

Default

This is the original method of auto thresholding available in ImageJ, which is a variation of the IsoData algorithm (described below). The Default option should return the same values as the ' Image › Adjust › Threshold › Auto', when selecting Ignore black and Ignore white. To indicate segmentation of the desired phase, use the White objects on black background option. The IsoData method is also known as iterative intermeans.

Huang

Implements Huang's fuzzy thresholding method. This uses Shannon's entropy function (one can also use Yager's entropy function).

Huang, L-K & Wang, M-J J (1995), "Image thresholding by minimizing the measure of fuzziness", Pattern Recognition 28(1): 41-51

Ported from ME Celebi's fourier_0.8 routines [1] and [2].

Huang2

This is an alternative implementation of Huang's method by J. Schindelin with an impressive speed advantage on 16 bit images. On some images, however the method gives returns different values than the original Huang method above.

Intermodes

This assumes a bimodal histogram. The histogram is iteratively smoothed using a running average of size 3, until there are only two local maxima: j and k. The threshold t is then computed as (j+k)/2. Images with histograms having extremely unequal peaks or a broad and flat valley are unsuitable for this method. method

Prewitt, JMS & Mendelsohn, ML (1966), "The analysis of cell images", Annals of the New York Academy of Sciences 128: 1035-1053

Ported from Antti Niemistö's MATLAB code. See here for an excellent slide presentation and his original MATLAB code.

IsoData

Iterative procedure based on the isodata algorithm of:

Ridler, TW & Calvard, S (1978), "Picture thresholding using an iterative selection method", IEEE Transactions on Systems, Man and Cybernetics 8: 630-632

The procedure divides the image into object and background by taking an initial threshold, then the averages of the pixels at or below the threshold and pixels above are computed. The averages of those two values are computed, the threshold is incremented and the process is repeated until the threshold is larger than the composite average. That is,

threshold = (average background + average objects)/2.

Several implementations of this method exist. See the source code for further comments.

Li

Implements Li's Minimum Cross Entropy thresholding method based on the iterative version (2nd reference below) of the algorithm.

Li, CH & Lee, CK (1993), "Minimum Cross Entropy Thresholding", Pattern Recognition 26(4): 617-625

Li, CH & Tam, PKS (1998), "An Iterative Algorithm for Minimum Cross Entropy Thresholding", Pattern Recognition Letters 18(8): 771-776

Sezgin, M & Sankur, B (2004), "Survey over Image Thresholding Techniques and Quantitative Performance Evaluation", Journal of Electronic Imaging 13(1): 146-165

Ported from ME Celebi's fourier_0.8 routines [3] and [4].

MaxEntropy

Implements Kapur-Sahoo-Wong (Maximum Entropy) thresholding method:

Kapur, JN; Sahoo, PK & Wong, ACK (1985), "A New Method for Gray-Level Picture Thresholding Using the Entropy of the Histogram", Graphical Models and Image Processing 29(3): 273-285

Ported from ME Celebi's fourier_0.8 routines [5] and [6].

Mean

Uses the mean of grey levels as the threshold. It is used by some other methods as a first guess threshold.

Glasbey, CA (1993), "An analysis of histogram-based thresholding algorithms", CVGIP: Graphical Models and Image Processing 55: 532-537

MinError(I)

An iterative implementation of Kittler and Illingworth's Minimum Error thresholding.

This implementation seems to converge more often than the original. Nevertheless, sometimes the algorithm does not converge to a solution. In that case a warning is reported to the log window and the result defaults to the initial estimate of the threshold which is computed using the Mean method. The Ignore black or Ignore white options might help to avoid this problem.

Kittler, J & Illingworth, J (1986), "Minimum error thresholding", Pattern Recognition 19: 41-47

Ported from Antti Niemistö's MATLAB code. See here for an excellent slide presentation and the original MATLAB code.

Minimum

Similarly to the Intermodes method, this assumes a bimodal histogram. The histogram is iteratively smoothed using a running average of size 3, until there are only two local maxima. The threshold t is such that yt−1 > yt <= yt+1.

Images with histograms having extremely unequal peaks or a broad and flat valley are unsuitable for this method.

Prewitt, JMS & Mendelsohn, ML (1966), "The analysis of cell images", Annals of the New York Academy of Sciences 128: 1035-1053

Ported from Antti Niemistö's MATLAB code. See here for an excellent slide presentation and the original MATLAB code.

Moments

Tsai's method attempts to preserve the moments of the original image in the thresholded result.

Tsai, W (1985), "Moment-preserving thresholding: a new approach", Computer Vision, Graphics, and Image Processing 29: 377-393

Ported from ME Celebi's fourier_0.8 routines [7] and [8].

Otsu

Otsu's threshold clustering algorithm searches for the threshold that minimizes the intra-class variance, defined as a weighted sum of variances of the two classes.

Otsu, N (1979), "A threshold selection method from gray-level histograms", IEEE Trans. Sys., Man., Cyber. 9: 62-66, doi:10.1109/TSMC.1979.4310076

See also the Wikipedia article on Otsu's method.

Ported from C++ code by Jordan Bevik.

Percentile

Assumes the fraction of foreground pixels to be 0.5.

Doyle, W (1962), "Operation useful for similarity-invariant pattern recognition", Journal of the Association for Computing Machinery 9: 259-267, doi:10.1145/321119.321123

Ported from Antti Niemistö's MATLAB code. See here for an excellent slide presentation and the original MATLAB code.

RenyiEntropy

Similar to the MaxEntropy method, but using Renyi's entropy instead.

Kapur, JN; Sahoo, PK & Wong, ACK (1985), "A New Method for Gray-Level Picture Thresholding Using the Entropy of the Histogram", Graphical Models and Image Processing 29(3): 273-285

Ported from ME Celebi's fourier_0.8 routines [9] and [10].

Shanbhag

Shanbhag, Abhijit G. (1994), "Utilization of information measure as a means of image thresholding", Graph. Models Image Process. (Academic Press, Inc.) 56 (5): 414--419, ISSN 1049-9652

Ported from ME Celebi's fourier_0.8 routines [11] and [12].

Triangle

This is an implementation of the Triangle method:

Zack GW, Rogers WE, Latt SA (1977), "Automatic measurement of sister chromatid exchange frequency", J. Histochem. Cytochem. 25 (7): 741–53, PMID 70454

Modified from Johannes Schindelin's plugin Triangle_Algorithm.

See also: http://www.ph.tn.tudelft.nl/Courses/FIP/noframes/fip-Segmenta.html#Heading118

The Triangle algorithm, a geometric method, cannot tell whether the data is skewed to one side or another, but assumes a maximum peak (mode) near one end of the histogram and searches towards the other end. This causes a problem in the absence of information of the type of image to be processed, or when the maximum is not near one of the histogram extremes (resulting in two possible threshold regions between that max and the extremes). Here the algorithm was extended to find on which side of the max peak the data goes the furthest and searches for the threshold within that largest range.

Yen

Implements Yen's thresholding method from:

Yen JC, Chang FJ, Chang S (1995), "A New Criterion for Automatic Multilevel Thresholding", IEEE Trans. on Image Processing 4 (3): 370-378, ISSN 1057-7149, doi:10.1109/83.366472

Sezgin, M & Sankur, B (2004), "Survey over Image Thresholding Techniques and Quantitative Performance Evaluation", Journal of Electronic Imaging 13(1): 146-165

Ported from ME Celebi's fourier_0.8 routines [13] and [14].

2 香巴拉算法( Shanbhag Thresholding)的C#源代码

二值算法综述请阅读:

C#,图像二值化(01)——二值化算法综述与二十三种算法目录

https://blog.csdn.net/beijinghorn/article/details/128425225?spm=1001.2014.3001.5502

支持函数请阅读:

C#,图像二值化(02)——用于图像二值化处理的一些基本图像处理函数之C#源代码

https://blog.csdn.net/beijinghorn/article/details/128425984?spm=1001.2014.3001.5502


namespace Legalsoft.Truffer.ImageTools
{
    public static partial class BinarizationHelper
    {
 
        #region 灰度图像二值化 全局算法 Shanbhag 算法 

        /// <summary>
        /// Shanbhag算法
        /// </summary>
        /// <param name="histogram"></param>
        /// <returns></returns>
        public static int Shanbhag_Threshold(int[] histogram)
        {
            int total = Histogram_Sum(histogram);
            double[] norm_histo = Histogram_Normalize(histogram);

            double[] P1 = new double[histogram.Length];
            double[] P2 = new double[histogram.Length];
            P1[0] = norm_histo[0];
            P2[0] = 1.0 - P1[0];
            for (int i = 1; i < histogram.Length; i++)
            {
                P1[i] = P1[i - 1] + norm_histo[i];
                P2[i] = 1.0 - P1[i];
            }

            int first_bin = 0;
            for (int i = 0; i < histogram.Length; i++)
            {
                if (Math.Abs(P1[i]) > float.Epsilon)
                {
                    first_bin = i;
                    break;
                }
            }

            int last_bin = histogram.Length - 1;
            for (int i = histogram.Length - 1; i >= first_bin; i--)
            {
                if (Math.Abs(P2[i]) > float.Epsilon)
                {
                    last_bin = i;
                    break;
                }
            }

            int threshold = -1;
            double min_ent = Double.MaxValue;
            for (int it = first_bin; it <= last_bin; it++)
            {
                double ent_back = 0.0;
                double term = 0.5 / P1[it];
                for (int ih = 1; ih <= it; ih++)
                {
                    ent_back -= norm_histo[ih] * Math.Log(1.0 - term * P1[ih - 1]);
                }
                ent_back *= term;

                double ent_obj = 0.0;
                term = 0.5 / P2[it];
                for (int ih = it + 1; ih < histogram.Length; ih++)
                {
                    ent_obj -= norm_histo[ih] * Math.Log(1.0 - term * P2[ih]);
                }
                ent_obj *= term;

                double tot_ent = Math.Abs(ent_back - ent_obj);
                if (tot_ent < min_ent)
                {
                    min_ent = tot_ent;
                    threshold = it;
                }
            }
            return threshold;
        }

        public static void Shanbhag_Algorithm(byte[,] data)
        {
            int[] histogram = Gray_Histogram(data);
            int threshold = Shanbhag_Threshold(histogram);
            Threshold_Algorithm(data, threshold);
        }

        #endregion

    }
}

3 香巴拉算法( Shanbhag Thresholding)计算效果

POWER BY 315SOFT.COM AND TRUFFER.CN

:-P

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/151954.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

「精致店主理人」:青年敢有所为,梦想掷地有声

第三期「精致店主理人」青年创业孵化营于12月16日在周大福顺德匠心智造中心&#xff0c;完美收官&#xff01;「精致店主理人」青年创业孵化营是在共青团深圳市委员会的指导下&#xff0c;由深圳市青少年发展基金会与周大福珠宝集团联合主办&#xff0c;郑家纯青年发展专项基金…

CPU基本结构和运行原理

1 CPU的基本结构 1.1 CPU是一个计算系统的核心 Control Unit&#xff0c;负责控制。如指令计数器&#xff0c;指令跳转。 Logic Unit&#xff0c;负责计算。如加减&#xff0c;比较大小等。 1.2 南北桥芯片将CPU与外设连接 北桥&#xff1a;CPU和内存、显卡等部件进行数据交…

Python解题 - CSDN周赛第22期 - 取数字

又是大放水的一期&#xff0c;连我都可以10分钟解决战斗了。得益于Python&#xff0c;前面三题5分钟内就pass了&#xff0c;而最后一题也是之前刷过类似的。。。于是相应地&#xff0c;这期的题解也会简短一些。 这次的好成绩代表不了实力&#xff0c;但也希望这样的好运气能一…

自然语言处理 概览理解 NLP specialization - Supervised ML

自然语言处理 概览理解 NLP specialization - Supervised ML Remember that understanding the data is one of the most critical steps in Data Science 自然语言处理可以实现语义识别&#xff0c;情感识别&#xff0c;文本翻译等等功能&#xff0c;当然最近情况下最火的便…

Java支付宝沙箱环境支付,官方Demo远程调试【内网穿透】

文章目录1. 下载当面付demo2. 修改配置文件3. 打包成web服务4. 局域网测试5. 内网穿透6. 测试公网访问7. 配置二级子域名8. 测试使用固定二级子域名访问在沙箱环境调试支付SDK的时候&#xff0c;往往沙箱环境部署在本地&#xff0c;局限性大&#xff0c;在沙箱环境中有多种支付…

MATLAB-一维插值运算

一维插值是指对一维函数进行插值。已知n1个结点(x,y,)&#xff0c;其中x,互不相同(j0&#xff0c;1&#xff0c;2&#xff0c;... n),求任意插值点x*处的插值y*。求解一维插值问题的主要思想是:设结点由未知的函数g(x)产生&#xff0c;函数g(x)为连续函数且g(x)y;(j0,1,...,n);…

北大硕士LeetCode算法专题课---算法复杂度介绍

算法复杂度中大O的含义 推荐教程&#xff1a;北大硕士带你手撕Leetcode算法题 大O复杂度表示法 算法面试中&#xff0c; 经常会被问到&#xff0c;你写的算法复杂度是多少&#xff0c; 或者设计一个复杂度为 O(nlogn) 的算法复杂度分析是估算算法执行效率的方法&#xff0c;…

大厂的格局在细节上是怎么被拉低的

讲几个例子&#xff0c;都是大厂&#xff0c;作为大厂小散沙客户&#xff0c;也就是平头老百姓的角度来反观一下几个问题 第一个【脉脉】 今天&#xff08;2023年1月9日&#xff09;偶尔相等一下买买查看一下信息&#xff0c;由于手机没有安装APP.想借助PC的web端登录一下 它提…

小程序中无法播放阿里云的视频点播

背景视频直接存在自己的服务器&#xff0c;视频大了会存在卡顿现象&#xff0c;只能用阿里云或其它服务商的相关服务。原来在APP中已有了视频播放的功能&#xff0c;而且已经能正常使用&#xff0c;但视频的url是 http。现在期望在小程序中也添加视频播放的功能。初期操作申请相…

css——扁平圆弧(底部弧度)

案例演示 使用伪类处理&#xff0c;先将元素自身定位为relative&#xff0c;伪类设置content:‘’&#xff0c;并相对定位为absolute&#xff0c;再设置下left ,top 值&#xff0c;然后通过改变width和和left就可以调节弧度。宽度需大于100%&#xff0c;将left设为&#xff08;…

微分方程(人口预测与传染病模型)

一、定义 微分方程&#xff1a;含导数或微分的方程 微分方程的阶数&#xff1a;所含导数或微分的最高阶数&#xff0c;如y’’’2y’’-2x0是三阶微分方程 微分方程的解&#xff1a;使得微分方程成立的函数 例如y’-2x0的解可以为x或者x1 微分方程的通解和特解&#xff1a;特…

基于Arduino Pro Micro的游戏手柄制作

基于Arduino Pro Micro的游戏手柄开发 1. Arduino IDE下载及安装 在Arduino官网Software | Arduino下载最新版Arduino IDE并安装。 或使用QQ群中提供的免安装版Arduino IDE&#xff0c;安装完成后Arduino Pro Micro的pin脚定义使用下面路径下的leonardo: 2. Arduino Pro Micr…

GrapeCity 文档处理商业系列Crack-6.0.2Version

GrapeCity 文档处理商业系列Crack文档 API 使用高速、直观的文档 API 全面控制您的文档 包括&#xff1a; Documents for Excel, .NET Documents for Data Viewer Documents for PDF Documents for PDF Viewer Documents for Word Documents for Imaging Documents for Image V…

数据库MySQL基础入门之MySQL隐式转换

一、问题描述 rootmysqldb 22:12: [xucl]> show create table t1\G *************************** 1. row *************************** Table: t1Create Table: CREATE TABLE t1 ( id varchar(255) DEFAULT NULL ) ENGINEInnoDB DEFAULT CHARSETutf8 1 row in set (0.0…

ArcGIS基础实验操作100例--实验74灾害影响缓冲区分析

本实验专栏参考自汤国安教授《地理信息系统基础实验操作100例》一书 实验平台&#xff1a;ArcGIS 10.6 实验数据&#xff1a;请访问实验1&#xff08;传送门&#xff09; 高级编辑篇--实验74 灾害影响缓冲区分析 目录 一、实验背景 二、实验数据 三、实验步骤 &#xff08;…

剑指offer----C语言版----第十四天

表示数值的字符串 原题链接&#xff1a;剑指 Offer 20. 表示数值的字符串 - 力扣&#xff08;LeetCode&#xff09;1.1 题目描述 请实现一个函数用来判断字符串是否表示数值&#xff08;包括整数和小数&#xff09;。数值&#xff08;按顺序&#xff09;可以分成以下几个部分&a…

常见胶片的分类和结构

胶片的分类方法非常多&#xff0c;本质上属于高分子材料范畴&#xff0c;很多类别已经超出笔者的认知范围&#xff0c;我们只对与档案数字资源长期保存和异质备份相关的胶片分类方法进行介绍。笔者整理了十大分类如下图所示&#xff1a; 各分类简要介绍 1. 按宽度尺寸分类 常…

【Linux】进程信号万字详解(上)

&#x1f387;Linux&#xff1a; 博客主页&#xff1a;一起去看日落吗分享博主的在Linux中学习到的知识和遇到的问题博主的能力有限&#xff0c;出现错误希望大家不吝赐教分享给大家一句我很喜欢的话&#xff1a; 看似不起波澜的日复一日&#xff0c;一定会在某一天让你看见坚持…

194: vue+openlayers 根据卫星lat,lon,alt,俯仰角,方位角,绘制地面的拍摄的区域

第194个 点击查看专栏目录 本示例的目的是介绍演示如何在vue+openlayers项目中研究卫星的拍摄范围。 根据卫星的高度,经度,纬度,方位角,俯仰角以及拍摄的幅宽,幅长等参数,绘制拍摄的矩形框和中心点。这里用到了一些公式,参考代码,尤其是大部分使用的是弧度而非角度,需…

青铜到王者,8本最好的Python从入门到进阶的书

春节长假还有1周了&#xff0c;是时候囤一些书充充电了&#xff01;新的一年群里很多小伙伴开始想学Python&#xff0c; 无论是准备转行的&#xff0c;还是想多学一份技能提高职场竞争力的&#xff0c;都想选择Python赛道&#xff0c;下面给大家推荐一些非常不错的Python入门到…