John Doe
John Doe

Reputation: 118

How to control image contrast based on HSV/RGB values

I was wondering if it was possible to modify the contrast of an image, by modifying its RGB, HSV (or similar) values.

I am currently doing the following to mess with luminance, saturation and hue (in python):

import numpy as np
from PIL import Image as img
import colorsys as cs

#Fix colorsys rgb_to_hsv function
#cs.rgb_to_hsv only works on arrays of shape: [112, 112,255] and non n-dimensional arrays
rgb_to_hsv = np.vectorize(cs.rgb_to_hsv)
hsv_to_rgb = np.vectorize(cs.hsv_to_rgb)

def luminance_edit(a, h, s, new_v):
    #Edits V - Luminance

    #Changes RGB based on new luminance value
    r, g, b = hsv_to_rgb(h, s, new_v)

    #Merges R,G,B,A values to form new array
    arr = np.dstack((r, g, b, a))

    return arr

I have a separate function to deal with converting to and fro RGB and HSV. A is the alpha channel, h is the hue, s is saturation and new_v is the new V value (luminance).

Is it possible to edit contrast based on these values, or am I missing something?

Edit: I have a separate function that imports images, extracts the RGBA values, and converts them into HSL/HSV. Lets call this function x.

In the code provided (function y), we take the hue(h), saturation(s), luminance (v) and the alpha channel (a) - the HSL values provided from function x, of some image.

The code edits the V value, or the luminance. It does not actually edit the contrast, It's just an example of what I'm aiming to achieve. Using the above data (HSL/HSV/RGB) or similar, I was wondering if it was possible to edit the contrast of an image.

Upvotes: 1

Views: 2956

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207365

I find it very hard to understand what you are trying to do in your question, so here is a "stab in the dark" that you are trying to increase contrast in an image without changing colours.

You are correct in going from RGB to HSL/HSV colourspace so that you can adjust luminance without affecting saturation and hue. So, I have basically taken the Luminance channel of a sombre image and normalised it so that the luminance now spans the entire brightness range from 0..255, and put it back into the image. I started with this image:

enter image description here

And ended up with this one:

enter image description here

Upvotes: 5

Related Questions