brandonmpetty / Doxa

A Local Adaptive Thresholding framework for image binarization written in C++, with JS and Python bindings. Implementing: Otsu, Bernsen, Niblack, Sauvola, Wolf, Gatos, NICK, Su, T.R. Singh, WAN, ISauvola, Bataineh, Chan and Shafait.
https://brandonmpetty.github.io/Doxa/WebAssembly
Creative Commons Zero v1.0 Universal
164 stars 37 forks source link

specifying binarization method in python #15

Closed SB2020-eye closed 3 years ago

SB2020-eye commented 3 years ago

Hello!

I'm trying to specify binarization methods and/or use all at once using demo.py as my starter template.

Here's the latest that I tried:

# Loop through all binarization algorithms
binarize_algos = [BERNSEN, NIBLACK, SAUVOLA, WOLF, NICK, SU, TRSINGH, BATAINEH, ISAUVOLA, WAN, GATOS]
for ba in binarize_algos:
    algo = doxapy.Binarization(doxapy.Binarization.Algorithms.ba)
    algo.initialize(grayscale_image)
    algo.to_binary(binary_image, {"window": 75, "k": 0.2}) # algorithm params are optional

    # Display our resulting image
    Image.fromarray(binary_image).show()

I also tried with the list's methods in quotes, mixed in with 3 or so variations on this line: algo = doxapy.Binarization(doxapy.Binarization.Algorithms.ba) (str(ba), for example, pluses (for both str(ba) and just ba like you see above)

Any idea what I'm doing wrong?

brandonmpetty commented 3 years ago

You can do something like this:

for ba in doxapy.Binarization.Algorithms.__members__.values():
    algo = doxapy.Binarization(ba)

An issue you may run into is that each algorithm needs to be tuned with its own set of parameters. You can either omit the parameters, and stick with the library defaults, or you can create a dictionary that looks up the custom parameters in the loop for the algorithm. I hope that helps!

bertsky commented 2 years ago

An issue you may run into is that each algorithm needs to be tuned with its own set of parameters. You can either omit the parameters, and stick with the library defaults, or you can create a dictionary that looks up the custom parameters in the loop for the algorithm.

I'd like to do just that – get a glimpse at the default params for each algorithm. But how?

(So far all I have come up with is looking into the members and docstrings of each doxapy.Binarization.Algorithms – class and object. But nothing tells me what params are available and what are their defaults.)

EDIT So basically I am interested in this – from Python:

fgrep -r parameters.Get Doxa
Doxa/UnitTests/CalculatorTests.cpp:             const int windowSize = parameters.Get("window", 75);
Doxa/UnitTests/CalculatorTests.cpp:             const double k = parameters.Get("k", 0.2);
Doxa/Bernsen.hpp:           const int windowSize = parameters.Get("window", 75);
Doxa/Bernsen.hpp:           const int GT = parameters.Get("threshold", 100);
Doxa/Bernsen.hpp:           const int L = parameters.Get("contrast-limit", 25);
Doxa/Nick.hpp:          const int windowSize = parameters.Get("window", 75);
Doxa/Nick.hpp:          const double k = parameters.Get("k", -0.2);
Doxa/Niblack.hpp:           const int windowSize = parameters.Get("window", 75);
Doxa/Niblack.hpp:           const double k = parameters.Get("k", 0.2);
Doxa/TRSingh.hpp:           const int windowSize = parameters.Get("window", 75);
Doxa/TRSingh.hpp:           const double k = parameters.Get("k", 0.2);
Doxa/Gatos.hpp:         const int glyphSize = parameters.Get("glyph", 60);
Doxa/Sauvola.hpp:           const int windowSize = parameters.Get("window", 75);
Doxa/Sauvola.hpp:           const double k = parameters.Get("k", 0.2);
Doxa/Wolf.hpp:          const int windowSize = parameters.Get("window", 75);
Doxa/Wolf.hpp:          const double k = parameters.Get("k", 0.2);
Doxa/Wan.hpp:           const int windowSize = parameters.Get("window", 75);
Doxa/Wan.hpp:           const double k = parameters.Get("k", 0.2);
Doxa/Su.hpp:            int windowSize = parameters.Get("window", 0); // Based on Stroke Size
Doxa/Su.hpp:            int minN = parameters.Get("minN", windowSize); // Roughly basd on size of window

(plus maybe the C++ comments for each algorithm on the Doxa namespace, as a docstring)

brandonmpetty commented 2 years ago

@bertsky I have created: https://github.com/brandonmpetty/Doxa/issues/23

I will work on documenting it first on the github wiki and then in the code base.