skip to Main Content

I have sRGB images with color casts. To remove it manually I usually use Photoshop Level Adjustments. Photoshop also have tools for that: Auto Contrast or even better Auto Tone which also takes shadows, midtones & highlights into account.

If I remove the cast manually I adjust each of the RGB channels individually so that the darkest pixels are set to pure black and the lightest to pure white and then redistribute all other values (spreading the histogram). This is a simple approach but shows good results for my images.

In my node.js app I’m using sharp for image processing which uses libvips as its processing engine. I tried to remove the cast with .normalize() but this command works on all channels together and not individual for each of the RGB channels. So it doesn’t work for me.
I also asked this question on the sharp project page. I tested the suggestion from lovell to try it with hist_local but the results are not useable for me.

Now I would like to find out how this could be done using the native libvips. I’ve played around with nip2 GUI and different commands but could not figure out how it could be achieved:

  1. Histogram > Equalise Histogram > Global => Picture looks over saturated
  2. Image > Levels > Scale to 0 – 255 => Channels ar not all spreading from 0 – 255 (I don’t understand exactly what this command does?)

Thanks for every hint!

Addition
Here is a example with pictures from Photoshop to show what I want.

The source image is a picture of a frame from a film negative.
Image before processing

Step1 Invert image
Image after inversion

Step2 using Auto tone in Photoshop (works the same way as my description above about manually remove the color cast)
Image after Auto Tone

This last picture is ok for me.

3

Answers


  1. Chosen as BEST ANSWER

    In the meantime I've found the Simplest Color Balance Algorithm which exactly describes the problem with color casts and there you can also find a C source code.

    It is exactly the same solution as John describes in his second answer but as a small piece of c-code.

    I'm now trying to use it as C/C++ addon with N-API under node.js.


  2. nip2 has a menu item for this.

    Load your image and mark a region on it containing the area you’d like to be neutral. It can be any lightness, it doesn’t need to be white.

    • Use File / Open to get the file dialog and you should see the image loaded in your workspace as a thumbnail.
    • Doubleclick on the thumbnail to open an image view window.
    • In the view window, zoom and pan to the right spot. The user guide (press F1) has a section on image navigation.
    • Hold down CTRL and click and drag down and right to mark a rectangular region.

    image view window with marked region

    Back in the main window, click Toolkits / Tasks / Capture / White balance. You should see something like:

    white balance widget

    You can drag an resize your region to change the neutral point. Use the colour picker to set what white means. You can make other whites with (for example) Colour / New / Colour from CCT and link them together.

    • Click Colour / New / Colour from CCT to make a colour picker from CCT (correlated colour temperature) — the temperature in Kelvin of that white.
    • Set it to something interesting, like 4800 for warm white.
    • Click on the formula for A5.white to edit it, and enter the cell of your CCT widget (A7 in this case).

    Now you can drag the region to adjust the pixels to set the neutral from, and drag the CCT slider to set the temperature.

    custom white

    It can be annoying to find things in the toolkit menu. There’s a thing for searching toolkits: in the main window, click View / Toolkit browser. You can enter something like “white” and it’ll show related toolkit entries.

    Login or Signup to reply.
  3. Here’s another answer, but using pyvips and responding to the previous comments. I didn’t want to delete the first answer as it still seemed useful.

    This version finds the image histogram, searches for thresholds which will select 0.5% and 99.5% of pixels in each image band, then rescales the image so that those pixel values become 0 and 255.

    import sys
    import pyvips
    
    # trim off this percentage of pixels from the top and bottom
    trim_percent = 0.5
    
    def percent(hist, percentage):
        """From a histogram, find the threshold above which lie 
        @percentage of pixels."""
        # normalised cumulative histogram
        norm = hist.hist_cum().hist_norm() 
    
        # column and row profile over percentage
        c, r = (norm > norm.width * percentage / 100).profile()
    
        return r.avg()
    
    image = pyvips.Image.new_from_file(sys.argv[1])
    
    # photographic negative
    image = image.invert()
    
    # find image histogram, split to set of separate bands
    bands = image.hist_find().bandsplit()
    
    # for each band, the low and high thresholds
    low = [percent(band, trim_percent) for band in bands]
    high = [percent(band, 100 - trim_percent) for band in bands]
    
    # rescale image 
    scale = [255.0 / (h - l) for h, l in zip(high, low)]
    image = (image - low) * scale
    
    image.write_to_file(sys.argv[2])
    

    It seems to give roughly similar results to the PS button. If I run:

    $ ./autolevel.py ~/pics/before.jpg x.jpg
    

    I see:

    result of script on sample image

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search