jafa7593
jafa7593

Reputation: 1

Python: Convert RGB image array to array of integers, where each specific RGB triplet equals some specific integer

I have an RGB numpy array img where img.shape returns (1694, 2012, 3) and where each pixel (e.g. img[0,0]) returns something like: array([ 13, 8, 135], dtype=uint8).

For every pixel in img, I want to convert the RGB triplet into a unique integer. In reality, img is an RGB image that uses the colors of the 'plasma' colormap from matplotlib, so each pixel value will be one found in

plasma_cmap = plt.get_cmap('plasma').colors

which returns

[[0.050383, 0.029803, 0.527975],
 [0.063536, 0.028426, 0.533124],
 [0.075353, 0.027206, 0.538007],
...

which I can convert to the form

[[13, 8, 135],
 [16, 7, 136],
 [19, 7, 137],
...

which matches the triplets found in my image (and plasma_cmap is of length 256). I'd like a way to convert each pixel in my img array to an array of integers where for each pixel, the resulting integer will be the "index" value of whatever triplet it is. E.g.

[13, 8, 135] --> 0
[16, 7, 136] --> 1
[19, 7, 137] --> 2
... etc.

or, using my initial example, img[0,0] returns 0. Desired end output is an array of the same shape 1694 by 2012.

This seems like it should be easy, but I can't find a way to map these integer values when starting with an array of RGB triplets (also, converting my image to grayscale will not work for my end purposes). Any insight would be appreciated!

Upvotes: 0

Views: 642

Answers (1)

NoDakker
NoDakker

Reputation: 4816

You might generate an encoded value of the tuple by multiplying the red value by 65536 (256 * 256), then adding the value of the green value by 256, and then adding the value of the blue value and store that in a variable. Following is a "proof of principle" program snippet how the RGB colors could be translated and stored in a single variable and then be translated back to an RGB triple.

red = 0
green = 0
blue = 0

while True:
    redx, greenx, bluex = input("Enter RGB triple: ").split()
    red = int(redx)
    green = int(greenx)
    blue = int(bluex)
    if red == 0 and green == 0 and blue == 0:
        break
        
    triple = red * 256 * 256 + green * 256 + blue
    
    print("Red:", red, "Green:", green, "Blue:", blue, "Triple number:", triple)
    
    # Translate back to a triple
    
    red = int(triple / (65536))
    green = int((triple - (red * 256 * 256)) / 256)
    blue = triple - (red * 256 * 256) - (green * 256)
    
    print("Decoded RGB values: Red:", red, "Green:", green, "Blue:", blue)

Here is some sample input to illustrate the algorithm.

@Una:~/Python_Programs/Triplets$ python3 Triplets.py 
Enter RGB triple: 100 100 100
Red: 100 Green: 100 Blue: 100 Triple number: 6579300
Decoded RGB values: Red: 100 Green: 100 Blue: 100
Enter RGB triple: 84 56 240
Red: 84 Green: 56 Blue: 240 Triple number: 5519600
Decoded RGB values: Red: 84 Green: 56 Blue: 240

That might give you food for thought.

Upvotes: 1

Related Questions