Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fill the values in numpy to create a Spectrum

I have done the following code but do not understand properly what is going on there. Can anyone explain how to fill colors in Numpy?

Also I want to set in values in a way from 1 to 0 to give spectrum an intensity. E.g-: 0 means low intensity, 1 means high intensity

import numpy as np
import matplotlib.pyplot as plt 

a= np.zeros([256*6,256*6, 3], dtype=np.uint8) # init the array
# fill the array with rgb values to create the spectrum without the use of loops

#red
a[:,:,0] = np.concatenate(([255]*256, np.linspace(255,0,256), [0]*256, [0]*256, np.linspace(0,255,256), [255]*256))

#green
a[:,:,1] = np.concatenate((np.linspace(0,255,256), [255]*256, [255]*256, np.linspace(255,0,256), [0]*256,[0]*256))

#blue
a[:,:,2] = np.concatenate(([0]*256, [0]*256,np.linspace(0,255,256),[255]*256, [255]*256, np.linspace(255,0,256)))


plt.imshow(a)  # this is different than what I am looking for

Expected Output-:

Expected Output

like image 607
Coder Avatar asked Oct 30 '21 09:10

Coder


2 Answers


res=256
op=np.zeros([res,res, 3]) # init the array

#RGB

op[:,:,0]= np.linspace(0,1,res)

op[:,:,1]=np.linspace(0,1,res).reshape (256, 1)

op[:,:,2]= np.linspace(1,0,res)

plt.imshow(op)


it will give the exact thing that you are looking for!

let me know if it does not work

like image 106
jony Avatar answered Nov 10 '22 05:11

jony


First of all: The results here when I tried the code is different then what you displayed in the question.

The result image

Color

Monochromatic

Let's say we have a gray scaled picture. Each pixel would have a value of integers between [0, 255]. Sometimes these values can be floats between [0, 1].

Here 0 is black and 255 is white. The vales between (0, 255) are grays. Towards 0 it gets more gray, towards 255 its less gray.

Polychromatic

(I'm not sure about the term Polychromatic) Colored pixels are not so different then gray scaled ones. The only different is colored pixels storing 3 different values between [0, 255] for each Red, Green and Blue values.

Colors

see: https://www.researchgate.net/figure/The-additive-model-of-RGB-Red-green-and-blue-are-the-primary-stimuli-for-human-colour_fig2_328189604

Now let's see what what the image you are creating is like:

Creation:

You are crating a matrix of zeros with shape of: 256, 256 * 6, 3, which is: 256, 1536, 3.

R values

Then with the first line you are replacing the first column with something else:

a[:, :, 0] = np.concatenate(
    (
        [255] * 256,
        np.linspace(255, 0, 256),
        [0] * 256,
        [0] * 256,
        np.linspace(0, 255, 256),
        [255] * 256
    )
)

Lets see what this lines do:

np.concatenate is easy. It meregs the arrays give. What are the given arrays?

  1. [255] * 256

It is an array full of 255s with length of 256:

[255, 255, ..., 255, 255]

  1. np.linspace(255, 0, 256)

It is 256 values between [255, 0].:

[255, 254, 253, .., 2, 1, 0]

  1. [0] * 256

See 1

  1. [0] * 256

See 1

  1. np.linspace(0, 255, 256)

The reverse of 2. See 2.

  1. [255] * 256

See 1

G and B Values

You can follow the same logic for Green and ,Blue,

Let's see how these values are changing by plotting them.

The matrix a has the same value along y axis. So if we could plot R, G and B values of one line of the matrix. We can see how the values are changing:

plt.plot(a[0][:, 0], "r-", label="Red values along x axis")
plt.plot(a[0][:, 1], "g-", label="Green values along x axis")
plt.plot(a[0][:, 2], "b-", label="Blue values along x axis")


plt.legend(loc="upper left")
plt.show()

Values of R, G and Bs of a line on matrix a

like image 25
MSH Avatar answered Nov 10 '22 05:11

MSH