C#,图像二值化(07)——全局阈值的迭代算法(Iteration Thresholding)及其源代码

1、 全局阈值的迭代算法

图像阈值分割---迭代算法

(1) 为全局阈值选择一个初始估计值T(图像的平均灰度)。
(2) 用T分割图像。产生两组像素:G1有灰度值大于T的像素组成,G2有小于等于T像素组成。
(3) 计算G1和G2像素的平均灰度值m1和m2;
(4) 计算一个新的阈值:T = (m1 + m2) / 2;
(5) 重复步骤2和4,直到连续迭代中的T值间的差小于一个预定义参数为止。
 

二值算法综述请阅读:

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

2、全局阈值的迭代算法的C#源程序

using System;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;

namespace Legalsoft.Truffer.ImageTools
{
    public static partial class BinarizationHelper
    {

        #region 灰度图像二值化 全局算法 迭代算法

        /// <summary>
        /// 阀值分割:迭代法
        /// https://blog.csdn.net/xw20084898/article/details/17564957
        /// </summary>
        /// <param name="data"></param>
        /// <param name="nMaxIter">最大迭代次数</param>
        /// <param name="iDiffRec">使用给定阀值确定的亮区与暗区平均灰度差异值</param>
        /// <returns></returns>
        private static int Iteration_Threshold(byte[,] data, int nMaxIter, out int iDiffRec)
        {
            int height = data.GetLength(0);
            int width = data.GetLength(1);

            iDiffRec = 0;

            int[] histogram = Gray_Histogram(data);
            int iMinGrayValue = Histogram_Left(histogram);
            int iMaxGrayValue = Histogram_Right(histogram);

            int iThrehold = 0;
            int iTotalGray = 0;
            int iTotalPixel = 0;
            int iNewThrehold = (iMinGrayValue + iMaxGrayValue) / 2;
            iDiffRec = iMaxGrayValue - iMinGrayValue;

            for (int a = 0; (Math.Abs(iThrehold - iNewThrehold) > 0.5) && a < nMaxIter; a++)
            {
                iThrehold = iNewThrehold;
                for (int i = iMinGrayValue; i < iThrehold; i++)
                {
                    iTotalGray += histogram[i] * i;
                    iTotalPixel += histogram[i];
                }
                int iMeanGrayValue1 = (iTotalGray / iTotalPixel);
                iTotalPixel = 0;
                iTotalGray = 0;
                for (int j = iThrehold + 1; j < iMaxGrayValue; j++)
                {
                    iTotalGray += histogram[j] * j;
                    iTotalPixel += histogram[j];
                }
                int iMeanGrayValue2 = (iTotalGray / iTotalPixel);

                iNewThrehold = (iMeanGrayValue2 + iMeanGrayValue1) / 2;
                iDiffRec = Math.Abs(iMeanGrayValue2 - iMeanGrayValue1);
            }

            return iThrehold;
        }

        public static void Iteration_Algorithm(byte[,] data)
        {
            int threshold = Iteration_Threshold(data, 100, out int iDiffRec);
            Threshold_Algorithm(data, threshold);
        }

        #endregion

    }
}

3、全局阈值的迭代算法的计算效果

Image threshold segmentation iterative algorithm

(1) Select an initial estimate T (the average gray level of the image) for the global threshold.

(2) Use T to segment the image. Two groups of pixels are generated: G1 consists of pixels with a gray value greater than T, and G2 consists of pixels with a gray value less than or equal to T.

(3) Calculate the average gray value m1 and m2 of G1 and G2 pixels;

(4) Calculate a new threshold: T=(m1+m2)/2;

(5) Repeat steps 2 and 4 until the difference between T values in successive iterations is less than a predefined parameter.

An iterative algorithm is the most common way to solve the data flow analysis equations. In this algorithm, we particularly have two states first one is in-state and the other one is out-state. The algorithm starts with an approximation of the in-state of each block and then computed by applying the transfer functions on the in-states.

Iterative algorithms (IA) of solving the inverse problems of the scalar theory of diffraction for the calculation of the DOEs, investigated in this section, do not cover the entire spectrum of the currently available iterative algorithms. No attention has been given to algorithms such as the method of direct stochastic search for the binary phase [49], the annealing modelling method [51], the error diffusion method [88], the Newton method for calculating the Dammann gratings [58], and others. These methods are less efficient in comparison with the parametric and gradient iterative algorithms investigated in this chapter, although they are characterised by convergence and reach a global minimum after a large number of iterations.

Although the IAs of the calculating DOEs have shortcomings (for example, the stagnation effect, i.e., as a result of the incorrect selection of the initial estimate of the sought DOE phase, the algorithm can reached a local minimum when the error does not change with the increase of the number of iterations), the obvious advantages of the IA explain why they are used widely in diffractive optics. Some of the advantages of the IA are described below.

微信扫码订阅
UP更新不错过~
关注
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
PageRank算法是Google搜索引擎中用于评估网站重要性的算法。它基于网页之间的链接关系来计算一个网页的重要性系数,这个系数决定了搜索引擎页面排名的顺序。以下是PageRank算法的Python实现及重要步骤的注释: ```python import numpy as np # 定义网站与链接关系的邻接矩阵,0表示无链接,1表示有链接 adjacency_matrix = np.array([[0, 0, 1, 1], [1, 0, 0, 0], [0, 0, 0, 1], [0, 1, 0, 0]]) # 将邻接矩阵转化为概率转移矩阵 transition_matrix = adjacency_matrix / adjacency_matrix.sum(axis=1, keepdims=True) # 定义PageRank算法迭代过程 def pagerank(transition_matrix, d=0.85, max_iterations=100, tolerance=1e-6): n = len(transition_matrix) # 初始化所有网站的PageRank值为1/n pagerank_vector = np.full((n,), 1/n) for i in range(max_iterations): # 计算新的PageRank值 new_pagerank_vector = (1-d)/n + d*np.dot(transition_matrix, pagerank_vector) # 计算PageRank值的变化量 delta = np.abs(new_pagerank_vector - pagerank_vector).sum() print(f"Iteration {i+1}: pagerank={new_pagerank_vector}, delta={delta}") # 若变化量小于收敛阈值,则停止迭代 if delta < tolerance: break pagerank_vector = new_pagerank_vector return pagerank_vector # 调用PageRank算法,输出每个网站的PageRank值 pagerank_vector = pagerank(transition_matrix) print(f"PageRank vector: {pagerank_vector}") # 输出每个网站的重要性排序 rank_order = np.argsort(pagerank_vector)[::-1] print("Rank order:") for i in rank_order: print(f"Site {i+1}: PageRank={pagerank_vector[i]}") ``` 重要步骤注释: 1. 定义网站与链接关系的邻接矩阵。 2. 将邻接矩阵转化为概率转移矩阵。 3. 定义PageRank算法迭代过程,其中d表示阻尼系数,max_iterations表示最大迭代次数,tolerance表示收敛阈值。 4. 在每次迭代中计算新的PageRank值,delta表示PageRank值的变化量。 5. 若变化量小于收敛阈值,则停止迭代。 6. 调用PageRank算法,输出每个网站的PageRank值。 7. 输出每个网站的重要性排序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

深度混淆

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值