Skip to content

ISSUE: Loss of black text clarity in white-dominant images when scaling 2x, 3x, 4x #33

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

Closed
fusefib opened this issue May 6, 2024 · 6 comments
Labels
complete Performed

Comments

@fusefib
Copy link

fusefib commented May 6, 2024

Description:
I am experiencing difficulties with the normal binarizing feature in ScanTailor-Experimental. When attempting to process an image containing predominantly white background with little black text, the resulting binarized image displays faint or erased text when scaling up the resolution to 2x or higher. Adjusting the Curve/Sqr color settings from 0.5/0 to 1/1 provides some improvement but does not fully restore the text clarity. Changing the Threshold method from Otsu does, but keeping Otsu is preferred. Setting the thickness to the max doesn't help.

Expected Behavior:
The binarization process should maintain the clarity and legibility of the black text, even when scaling up the resolution.

Actual Behavior:
The black text becomes faint or disappears entirely in the binarized image when the resolution is increased.

Environment:

  • ScanTailor-Experimental version: 0.2024.05.03-X86-64-Qt6 ("threshold" from Releases)
  • Operating System: Windows 10

Steps to reproduce:

  • Add attached image to a new project.
  • Go directly to Stage 6 - Output.
  • Check Resolution Enhancement box and click 2x.
  • 4x disappears most of the black.

Notes:
I've just started using this variant of ScanTailor (awesome work!) and I haven't done extensive testing to determine if this is a regression and, if so, when it occurred. Would be happy to test if needed.

testpic

@zvezdochiot
Copy link
Member

Hi, @fusefib .

In Linux, the one you described cannot be played either on 2x or 4x.

@fusefib
Copy link
Author

fusefib commented May 6, 2024

I'll try to prepare a better picture for testing. In any case, it consistently does this for high resolution pictures that are mostly white and have almost no black, when scaling up. However, pages with enough text always turn out fine. Here is the result for me:
testpic-scaled.zip

2x:
image
4x:
image

@plzombie
Copy link
Contributor

plzombie commented May 7, 2024

@zvezdochiot Reproduced with Otsu, Dots8, EdgePlus(somehow), BlurDiv, EdgeDiv, Robust
Not reproducible in 0.2023.10.24
Reproducible in 0.2023.10.30 and so on
Looks like some bug introduced in 0.2023.10.30 (for all sizes) and partially fixed in 0.2023.11.19 (for size 1.0).
Also if you change image size manually by 50% and then upscale 2x in ScanTailor - everything is ok, but when you upscale 4x you will got the same result like original image with scale 2x (thin black lines).
So its probably something related to image size.

Also reproducible on 32-bit linux. So maybe should look for long (32bit on windows and 32bit linux, 64bit on 64bit linux) variables
изображение

I suggest to check aafb67f 587683b and 0.2023.11.10...0.2023.11.19#diff-4ddd4b67d244ac7c7864d278c0179f232b36742520daee44b20ec5b80e4f6324R870 (and maybe other code) and replace long with int64_t/uint64_t

@zvezdochiot
Copy link
Member

zvezdochiot commented May 7, 2024

@plzombie , this could be (?!) an overflow in:

unsigned long int im, iw, ib, histogram[histsize] = {0};

but the type is specified independent of the bit depth (!).

PS: All the thresholds you listed use binarizeBiModalValue.

  1. ❓ Maybe:
    Tb = (ib > 0) ? (Tb / ib) : 0.0;
    Tw = (iw > 0) ? (Tw / iw) : (double) histsize;

    to
        Tb = (ib > 0) ? (Tb / (double) ib) : 0.0;
        Tw = (iw > 0) ? (Tw / (double) iw) : (double) histsize;
  1. ❓ Maybe:
    for (k = 0; k < histsize; k++)
    {
    im = histogram[k];
    if (k < threshold)
    {
    Tb += (double) (im * k);
    ib += im;
    }
    else
    {
    Tw += (double) (im * k);
    iw += im;
    }
    }

    to:
        for (k = 0; k < histsize; k++)
        {
            im = histogram[k];
            if (k < threshold)
            {
                Tb += ((double) im * (double) k);
                ib += im;
            }
            else
            {
                Tw += ((double) im * (double) k);
                iw += im;
            }
        }
  1. ❓ Maybe:
    unsigned long int im, iw, ib, histogram[histsize] = {0};

    to:
    unsigned long int im, histogram[histsize] = {0};
    double iw = 0.0, ib = 0.0;

@plzombie plzombie reopened this May 7, 2024
@plzombie
Copy link
Contributor

plzombie commented May 7, 2024

@fusefib Could you try this build?
staging.zip
scantailor-experimental-0.2024.05.05-X86-64-Qt6-install.zip

@fusefib
Copy link
Author

fusefib commented May 8, 2024

Confirmed it works properly now with both zips. Thanks to both of you for the fix!

@fusefib fusefib closed this as completed May 10, 2024
@zvezdochiot zvezdochiot added the complete Performed label Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complete Performed
Projects
None yet
Development

No branches or pull requests

3 participants