矩保持阈值:三层案例

问题描述

我目前正在 Python 3.6 中为 8 位图像中的三级案例实现 Tsai 提出的分割方法。这是代码,考虑到论文附件中解释的数学关系:

 ###  THRESHOLDS CALculaTION BEGIN ###
    
    # 0 Moment = 1
    m0 = 1.0
    
    # 1st Moment 
    m1 = np.cumsum((histogram[:,0])*histogram[:,2])[-1]    
    
    # 2nd Moment
    m2 = np.cumsum((histogram[:,0]**2)*histogram[:,2])[-1]  # Take the last value
    
    # 3rd Moment
    m3 = np.cumsum((histogram[:,0]**3)*histogram[:,2])[-1]
    
    # 4th Moment
    m4 = np.cumsum((histogram[:,0]**4)*histogram[:,2])[-1] 
    
    # 5th Moment
    m5 = np.cumsum((histogram[:,0]**5)*histogram[:,2])[-1]
    
    # Now we must find the value in the binary image that preserves these moments
    
    
    # We solve the equalities --> For solutions refer to Paper Annex A.2
    cd = (m0*m2*m4) + (m1*m3*m2) + (m1*m3*m2) - (m2*m2*m2) - (m1*m1*m4) - (m3*m3*m0)
    c0 = ((-m3*m2*m4) + (-m4*m3*m2) + (m1*m3*-m5) - (-m5*m2*m2) - (-m4*m1*m4) - (m3*m3*-m3)) / cd
    c1 = ((m0*-m4*m4) + (m1*-m5*m2) + (-m3*m3*m2) - (m2*-m4*m2) - (m1*-m3*m4) - (-m5*m3*m0)) / cd
    c2 = ((m0*m2*-m5) + (m1*m3*-m3) + (m1*-m4*m2) - (m2*m2*-m3) - (m1*m1*-m5) - (m3*-m4*m0)) /cd
    
    a1 = c0/2 - c1*c2/6 + (c2**3)/27
    a2 = (c0/2 - c1*c2/6 + (c2**3)/27)**2
    a3 = (c1/3 - (c2**2)/9)**3
    
    a = (a1 - cmath.sqrt(a2 + a3))**1/3 
    
    b = -(c1/3 - (c2**2)/9)/a
    w1 = -0.5 + 1j * (math.sqrt(3)/2)
    w2 = -0.5 - 1j * (math.sqrt(3)/2)
    
    z0 = -c2/3 - a - b
    z1 = -c2/3 - w1*a - w2*b
    z2 = -c2/3 - w2*a - w1*b
   
    pd = (z1*z2**2) + (z2*z0**2) + (z0*z1**2) - (z0**2*z1) - (z0*z2**2) - (z1**2*z2)
    p0 = ((m0*z1*z2**2) + (m1*z1**2) + (z2*m2) - (m2*z1) - (m1*z2**2) - (z1**2*z2*m0)) /pd
    p1 = ((m1*z2**2) + (z0*m2) + (m0*z2*z0**2) - (z0**2*m1) - (z0*m0*z2**2) - (m2*z2)) / pd # Fraction of the below-threshold pixels in the binary histogram

    th1 = 0.0
    th2 = 0.0
    dist1 = 10000000
    dist2 = 10000000
    
    for i in range(254):
        for j in range(i+1,255):
            # Select threshold --> closest to p0 from the normlaized histogram
            p0_orig = histogram[i,3]  # Take the cumulative relative frequency at the value p0
            p1_orig = histogram[j,3] - histogram[i,3]
            dist_i = abs(p0 - p0_orig)
            dist_j = abs(p1 - p1_orig)
            
            if dist_i < dist1 and dist_j < dist2:  # This one was the one mentioned by Tsai ("Minimize the distance")
                
                print(i,j,dist_i,dist_j)
                dist1 = dist_i
                dist2 = dist_j
                th1 = i
                th2 = j


但是,当我考虑到图 1 中描述的“虚拟”图像而应用它来重现他的结果时,我没有获得相同的阈值(在我的情况下,我得到 12 和 29,而不是 18 和 30)。>

有人对这种方法有经验,可以帮助我找出我的代码有什么问题吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)