DanBloomberg / leptonica

Leptonica is an open source library containing software that is broadly useful for image processing and image analysis applications. The official github repository for Leptonica is: danbloomberg/leptonica. See leptonica.org for more documentation.
Other
1.72k stars 384 forks source link

pixDeskewGeneral(...) failing to detect a 45 degree skew? #738

Closed alexmera88 closed 4 months ago

alexmera88 commented 4 months ago

Hi all, I'm using the following code to deskew some images:

  // Deskew the image
    const int     redsweep = 0;
    const float   sweeprange = 50.0;
    const float   sweepdelta = 0.0;
    const int     redsearch = 0;
    const int     thresh = 0;
    float         angle;
    float         conf;
    Pix *deskewed = pixDeskewGeneral(image, redsweep, sweeprange, sweepdelta, redsearch, thresh, &angle, &conf);

This works just fine with text images (just black text over a white background) skewed by 5º, 10º....40º. But if the text is rotated 45º or 135º (clockwise), the detected angle is wrong, and the confidence is low.

Do you have any suggestions on how to handle these cases?

DanBloomberg commented 4 months ago

If you look in pixFindSkewSweepAndSearchScorePivot, you will see that we don't actually rotate the image. We do vertical shears, because they are much more efficient. The function pixDeskewGeneral() does these vertical shears about the upper-left corner.

The algorithm was never designed for very large rotation corrections. If you run this program, you'll see a possible problem; namely, the rotation about the corner removes most of the image!

    pix1 = pixRead("/tmp/feyn-fract.tif");
    pixGetDimensions(pix1, &w, &h, NULL);
    pix2 = pixRotate(pix1, 3.14159 / 4.0, L_ROTATE_SAMPLING, L_BRING_IN_WHITE,
                     0, 0);
    pixDisplay(pix2, 100, 100);
    pix3 = pixVShearCorner(NULL, pix2, -3.14159 / 4.0, L_BRING_IN_WHITE);
    pixDisplay(pix3, 600, 100);

If that is the problem, then before you apply the deskew function, add a very large border the the image, of at least 1/3 the width on left and right sides and 1/3 the height on top and bottom

alexmera88 commented 4 months ago

Excellent answer; thanks a lot, Dan!