Skip to content

关于answer_4.py #45

@thgpddl

Description

@thgpddl
def otsu_binarization(img, th=128):
    max_sigma = 0
    max_t = 0
    
    # determine threshold
    for _t in range(1, 255):
        v0 = out[np.where(out < _t)]
        m0 = np.mean(v0) if len(v0) > 0 else 0.
        w0 = len(v0) / (H * W)
        v1 = out[np.where(out >= _t)]
        m1 = np.mean(v1) if len(v1) > 0 else 0.
        w1 = len(v1) / (H * W)
        sigma = w0 * w1 * ((m0 - m1) ** 2)
        if sigma > max_sigma:
            max_sigma = sigma
            max_t = _t

    # Binarization
    print("threshold =", max_t)
    th = max_t
    out[out < th] = 0
    out[out >= th] = 255

    return out

作者v0 = out[np.where(out < _t)]中的out可能是指函数传入参的img吧,不是太理解

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions