• 大小: 6KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-06
  • 语言: C#
  • 标签: 图像处理  

资源简介

基于直方图的自适应二值化阈值,基于c#编写。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Windows.Forms;

public static class Threshold
{
    /// 
    /// 基于灰度平均值的阈值
    /// 

    /// 灰度图像的直方图
    /// 
    public static int GetMeanThreshold(int[] HistGram)
    {
        int Sum = 0 Amount = 0;
        for (int Y = 0; Y < 256; Y++)
        {
            Amount += HistGram[Y];
            Sum += Y * HistGram[Y];
        }
        return Sum / Amount;
    }
    /// 
    /// 基于模糊集的黄式阈值算法
    /// http://www.ktl.elf.stuba.sk/study/vacso/Zadania-Cvicenia/Cvicenie_3/TimA2/Huang_E016529624.pdf
    /// 

    /// 灰度图像的直方图
    /// 

    public static int GetHuangFuzzyThreshold(int[] HistGram)
    {
        int X Y;
        int First Last;
        int Threshold = -1;
        double BestEntropy = Double.MaxValue Entropy;
        //   找到第一个和最后一个非0的色阶值
        for (First = 0; First < HistGram.Length && HistGram[First] == 0; First++) ;
        for (Last = HistGram.Length - 1; Last > First && HistGram[Last] == 0; Last--) ;
        if (First == Last) return First;                // 图像中只有一个颜色
        if (First + 1 == Last) return First;            // 图像中只有二个颜色

        // 计算累计直方图以及对应的带权重的累计直方图
        int[] S = new int[Last + 1];
        int[] W = new int[Last + 1];            // 对于特大图,此数组的保存数据可能会超出int的表示范围,可以考虑用long类型来代替
        S[0] = HistGram[0];
        for (Y = First > 1 ? First : 1; Y <= Last; Y++)
        {
            S[Y] = S[Y - 1] + HistGram[Y];
            W[Y] = W[Y - 1] + Y * HistGram[Y];
        }

        // 建立公式(4)及(6)所用的查找表
        double[] Smu = new double[Last + 1 - First];
        for (Y = 1; Y < Smu.Length; Y++)
        {
            double mu = 1 / (1 + (double)Y / (Last - First));               // 公式(4)
            Smu[Y] = -mu * Math.Log(mu) - (1 - mu) * Math.Log(1 - mu);      // 公式(6)
        }

        // 迭代计算最佳阈值
        for (Y = First; Y <= Last; Y++)
        {
            Entropy = 0;
            int mu = (int)Math.Round((double)W[Y] / S[Y]);             // 公式17
            for (X = First; X <= Y; X++)
                Entropy += Smu[Math.Abs(X - mu)] * HistGram[X];
            mu = (int)Math.Round((double)(W[Last] - W[Y]) / (S[Last] - S[Y]));  // 公式18       
            for (X = Y + 1; X <= Last; X++)
                Entropy += Smu[Math.Abs(X - mu)] * HistGram[X];       // 公式8
            if (BestEntropy > Entropy)
            {
                BestEntropy = Entropy;      // 取最小熵处为最佳阈值
                Threshold = Y;
            }
        }
        return Threshold;
    }


    /// 

    /// 基于谷底最小值的阈值
    /// 此方法实用于具有明显双峰直方图的图像,其寻找双峰的谷底作为阈值
    /// References: 
    /// J. M. S. Prewitt and M. L. Mendelsohn “The analysis of cell images“ in
    /// nnals of the New York Academy of Sciences vol. 128 pp. 1035-1053 1966.
    /// C. A. Gl

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      26822  2015-04-21 10:29  Threshold.cs

----------- ---------  ---------- -----  ----

                26822                    1


评论

共有 条评论