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

Contrast + Binary does not handle low light well #9

Closed
drbsoftware opened this issue Feb 11, 2014 · 1 comment
Closed

Contrast + Binary does not handle low light well #9

drbsoftware opened this issue Feb 11, 2014 · 1 comment

Comments

@drbsoftware
Copy link

The combination of contrast and binary does not really handle low-light images.

A better approach is in https://gist.github.com/tobytailor/421369, which is to find the minimum and the maximum and use the average as a threshold.

I replaced them with a single function:

function contrastBinary(data) {
    var min = 127 * 3;
    var max = 128 * 3;
    for (var i = 0, len = Image.width * Image.height * 4; i < len; i += 4) {
        var val = data[i] + data[i + 1] + data[i + 2];
        if (val < min) {
            min = val;
        } else if (val > max) {
            max = val;
        }
    }
    var threshold = (max + min) / 2;
    for (var i = 0, len = Image.width * Image.height * 4; i < len; i += 4) {
        ave = (data[i] + data[i + 1] + data[i + 2]);
        if (ave < threshold) {
            data[i] = data[i + 1] = data[i + 2] = 0;
        } else {
            data[i] = data[i + 1] = data[i + 2] = 255;
        }
        data[i + 3] = 255;
    }
}
@EddieLa
Copy link
Owner

EddieLa commented May 12, 2014

Added this an option in version update.

@EddieLa EddieLa closed this as completed May 12, 2014
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

2 participants