Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

guessing the rgb gradient from a map?

I have a map with a scale like this one: (the numbers are just an example)

Gradient which describes a single variable on a map. However, I don't have access to the original data and know pretty close to nothing about image processing. What I have done is use PIL to get the pixel-coordinates and RGB values of each point on the map. Simply using pix = im.load() and saving pix[x,y] for each x,y. Now I would like to guess the value of each point using the gradient above.

Is there a standard formula for such a gradient? Does it look very familiar to the trained eye? I have visited Digital Library of Mathematical Functions for some examples ... but I'm not sure if it's using the hue, the rgb height function or something else (to make things easier I'm also colorblind to some greens/brows/reds) :)

Any tips on how to proceed, libraries, links or ideas are appreciated. Thank you!

edit:

Following the replies and martineau's suggestion, I've tried to catch the colors at the top and bottom:

def rgb2hls(colotup):
    '''converts 225 based RGB to 360 based HLS
    `input`: (222,98,32) tuple'''

    dec_rgb = [x/255.0 for x in colotup] # use decimal 0.0 - 1.0 notation for RGB
    hsl_col = colorsys.rgb_to_hls(dec_rgb[0], dec_rgb[1], dec_rgb[2])
    # PIL uses hsl(360,x%,y%) notation and throws errors on float, so I use int
    return (int(hsl_col[0]*360), int(hsl_col[1]*100), int(hsl_col[2]*100))


def pil_hsl_string(hsltup):
    '''returns a string PIL can us as HSL color
    from a tuple (x,y,z) -> "hsl(x,y%,z%)"'''
    return 'hsl(%s,%s%%,%s%%)' % (hsltup[0], hsltup[1], hsltup[2])


BottomRed = (222,98,32) # taken with gimp
TopBlue =  (65, 24, 213) 

hue_red = pil_hsl_string(rgb2hls(BottomRed))
hue_blue = pil_hsl_string(rgb2hls(TopBlue))

However they come out pretty different ... which makes me worry about using the rgb_to_hls function to extract the values. Or I'm I doing something very wrong? Here's what the color s convert to with the code: enter image description here

like image 592
Massagran Avatar asked Oct 27 '12 15:10

Massagran


2 Answers

Interesting question..

If you do a clock-wise walk in HSL color-space from 250,85%,85% --> 21,85%,85% you get a gradient very close to the one you've shown. The obvious difference being that your image exhibits a fairly narrow band of greenish values.

So, if you have the 4 magic numbers then you can interpolate to any point within the map.

These of course being the first and last colour, also the first and last scale value. Here's the image I got with a straight linear gradient on the H channel (used the gimp). enter image description here

EDIT: I've since whipped up a program to grab the pixel values for each row, graphing the results. You can see that indeed, the Hue isn't linear, you can also see the S & V channels taking a definite dip at around 115 (115 pixels from top of image) This indeed corresponds with the green band.

Given the shape of the curves, I'm inclined to think that perhaps they are intended to model something. But don't have the experience in related fields to recognise the shape of the curves.

Below, I've added the graphs for the change in both the HSV and RGB models. The left of the graph represents the top of the bar. The X-axis labels represent pixels

Quite interesting, me thinks. Bookmarked.

enter image description here

enter image description here

like image 150
enhzflep Avatar answered Nov 17 '22 00:11

enhzflep


The scale in the image looks like an HSV gradient to me, something like what is mentioned in this question. If so, you could use the colorsys.rgb_to_hls() or colorsys.rgb_to_hsv() functions to obtain a hue color value between 0 and 1 from the r,g,b values in a pixel. That can then be mapped accordingly.

However, short of doing OCR, I have no idea how to determine the range of values being represented unless it's some consistent range you can just hardcode.

like image 2
martineau Avatar answered Nov 17 '22 01:11

martineau