sharp icon indicating copy to clipboard operation
sharp copied to clipboard

Picture auto enhancement

Open mathias22osterhagen22 opened this issue 4 years ago • 6 comments

I'm sorry if this question seems silly but I didn't see anywhere an "auto" mode other than for the rotation function.

What are you trying to achieve? We have a lot of documents arriving in our system that are scanned and often not easily readable, we are looking to auto enhances (if necessary) the picture, the contrast, and the luminosity (like the filter of windows). image

By "if necessary" I mean: that we don't want to apply a luminosity + 10 on an image already too bright or a - 10 on one already too dark.

Have you searched for similar questions? Yes, but I didn't found.

Are you able to provide a minimal, standalone code sample that demonstrates this question? An example:

  1. Input: input

  2. Output: output

Are you able to provide a sample image that helps explain the question? (Yes see the two above)

mathias22osterhagen22 avatar Sep 23 '21 16:09 mathias22osterhagen22

Hi, did you see the clahe and normalise operations?

lovell avatar Sep 23 '21 17:09 lovell

Hello @lovell , thank you a lot for your answer! I tried this morning the two functions you recommended but It doesn't seem to make a major improvement: image

Do I'm doing something wrong ?

const fs = require("fs");
const sharp = require("sharp");

let files = fs.readdirSync('./input');
console.log("Input files: ", files);
if (fs.existsSync('./output'))
    fs.rmdirSync('./output', { recursive: true, force: true });
fs.mkdirSync('./output');

files.forEach(fileName => {
    console.log('Convert: ./input/' + fileName + " -> ./output/" + fileName + " ...");
    let image = sharp('./input/' + fileName);
    image.metadata()
        .then(metadata => {
            image.clahe({ width: metadata.width, height: metadata.height })
                .normalise()
                .withMetadata()
                .toFile('./output/' + fileName);
        })
});

mathias22osterhagen22 avatar Sep 24 '21 08:09 mathias22osterhagen22

Perhaps try using CLAHE with a smaller local region.

https://en.wikipedia.org/wiki/Adaptive_histogram_equalization

For example using the following code with the above input:

sharp(input)
  .clahe({ width: 21, height: 21 })
  .normalise()
  ...

produces:

lovell avatar Sep 25 '21 19:09 lovell

Thank you @lovell, yes in fact with a smaller local region made it look better.

Is there a way to make it automatically brighter or darker?

mathias22osterhagen22 avatar Sep 29 '21 09:09 mathias22osterhagen22

The normalise operation will already have spread luminosity over the full range, so if you always want it to be brighter you'll need to use modulate and pass either lightness (additive) or brightness (multiplicative).

lovell avatar Nov 07 '21 10:11 lovell

Then there is no way to detect it automatically? :/

mathias22osterhagen22 avatar Nov 16 '21 10:11 mathias22osterhagen22

Let's track this at #200, which is about improving normalise and should help with this kind of scenario.

lovell avatar Aug 24 '22 17:08 lovell