0
votes

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.

1
Your code doesn't appear to load any images or call your luminance_edit() function? Your description says you plan to "mess" with luminance and saturation which isn't much help at all. What is the question and what is your incomplete code trying to achieve and what aspect doesn't work?Mark Setchell
Did my answer sort out your problem? If so, please consider accepting it as your answer - by clicking the hollow tick/checkmark beside the vote count. If not, please say what didn't work so that I, or someone else, can assist you further. Thanks. meta.stackexchange.com/questions/5234/…Mark Setchell

1 Answers

5
votes

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