0

I tried to create a normalized matrix of size 256*256*3 which represent the RGB cube like this,

RGB Cube

I tried the following code in opencv-(I imported numpy as np):

R = [np.true_divide(i, 256) for i in xrange(256)]
RGB_Cube = np.zeros((256, 256, 3), dtype=np.float64)
RGB_Cube[:, :, 0] = RGB_Cube[:, :, 1] = RGB_Cube[:, :, 2] = np.tile(R, (256,1))

and I got this:

Output of the code

I also tried this(without normalizing the channels):

R = [i for i in xrange(256)]
# R = np.linspace(0, 1, 256, endpoint=True)
RGB_Cube = np.zeros((256, 256, 3), dtype=np.float64)
RGB_Cube[:, :, 0] = RGB_Cube[:, :, 1] = RGB_Cube[:, :, 2] = np.tile(R, (256,1))

but I got a white image.

I want to partition this matrix into sub-cuboids. Then finding the mean value of these cuboids. After that I will use this information for segmentation of a given image!

I don't know how much easy this problem is, I couldn't find a way to solve it. Can anyone help?

Thanks

5
  • 1
    Are you aiming for the image especially? Or do you want a matrix that represents the full 8-bit RGB color space? Commented Apr 10, 2014 at 15:05
  • I just want a matrix that represents RGB cube! Commented Apr 10, 2014 at 15:06
  • Can I ask you to be more specific? You want an 256x256x256x3 array that has every possible 8-bit RGB value? I worry because in your question it says 256x256 which wouldn't cover the 8-bit RGB space. We can do this without building an enormous array by doing it procedurally. Or if you want to generate the same image that you posted, that is a totally different problem to be solved. Commented Apr 10, 2014 at 15:11
  • Yes, of course! I said 256x256x3 because you can split it in three channels of R,G,B each of them is 256(8-bit). so we have a 256x256x3 matrix! I think that makes sense! Commented Apr 10, 2014 at 15:14
  • It's still hard for me to understand exactly what you want. Instead of that, could you explain in the question how you would like to use the result data? That might help to make it clear for me. Commented Apr 10, 2014 at 15:20

1 Answer 1

2

Sorry I'm still not able to understand what you need yet. Assuming you want a "cube" that represents every possible 8-bit RGB value, you will need a 256 x 256 x 256 (x3) array. Not 3 256 x 256 (x3) arrays.

Please note - I really think you don't want to do this. The data for something like this (including sub-cubes) could be made procedurally without needing to store everything in memory. The code below stores all ~16 million values of 8-bit RGB space and takes about 140MB when pickled to disk.

Here it is anyway:

import pickle
import numpy as np

# full 8-bit RGB space
bits = 8
cube_dimension = 2**bits
full_rgb_space = np.ndarray((cube_dimension, cube_dimension, cube_dimension, 3),
                            dtype=np.uint8)

# this is really inefficient and will take a long time.
for i in range(cube_dimension):
    print(i)  # just to give some feedback while it's working
    for j in range(cube_dimension):
        for k in range(cube_dimension):
            position = color = (i, j, k)
            full_rgb_space[position] = color

# save it to see what you've got.
# this gives me a 140MB file.
with open('full_rgb_space.p', 'wb') as f:
    pickle.dump(full_rgb_space, f)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.