Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于answer_4.py #45

Open
thgpddl opened this issue May 31, 2021 · 0 comments
Open

关于answer_4.py #45

thgpddl opened this issue May 31, 2021 · 0 comments

Comments

@thgpddl
Copy link

thgpddl commented May 31, 2021

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吧,不是太理解

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant