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

improve default gate value #28

Open
josenimo opened this issue Jun 7, 2024 · 1 comment
Open

improve default gate value #28

josenimo opened this issue Jun 7, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@josenimo
Copy link
Collaborator

josenimo commented Jun 7, 2024

double derivative of density plot

@josenimo josenimo added the enhancement New feature or request label Jun 7, 2024
@josenimo josenimo self-assigned this Jun 7, 2024
@josenimo
Copy link
Collaborator Author

idea is

import numpy as np

def kittler_illingworth_threshold(data):
    # Calculate histogram
    hist, bin_edges = np.histogram(data, bins=256, range=(0, 256))
    
    # Calculate probability of each intensity level
    P = hist / hist.sum()
    
    # Cumulative sums
    omega = np.cumsum(P)
    
    # Means
    mu = np.cumsum(P * np.arange(256))
    
    # Total mean
    mu_t = mu[-1]
    
    # Between class variance
    sigma_b_squared = (mu_t * omega - mu) ** 2 / (omega * (1 - omega))
    
    # Replace NaNs with zero
    sigma_b_squared = np.nan_to_num(sigma_b_squared)
    
    # Find threshold
    threshold = np.argmax(sigma_b_squared)
    
    return threshold

# Example usage
data = np.random.randint(0, 256, 1000)
threshold = kittler_illingworth_threshold(data)
print(f"Optimal threshold: {threshold}")

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

No branches or pull requests

1 participant