2

I have the following array, which represents an image as a three-color numpy array.

array([[[165, 173, 184],
        [170, 178, 189],
        [171, 179, 190],
        ...,
        [145, 162, 180],
        [140, 157, 175],
        [142, 159, 177]],

       [[169, 177, 188],
        [170, 178, 189],
        [169, 177, 188],
        ...,
        [152, 169, 187],
        [149, 166, 184],
        [143, 160, 178]],

       [[170, 178, 189],
        [169, 177, 188],
        [168, 176, 187],
        ...,
        [143, 160, 178],
        [144, 161, 179],
        [142, 159, 177]],

       ...,

       [[  5,  13,   2],
        [  5,  13,   2],
        [  8,  16,   5],
        ...,
        [ 31,  27,  16],
        [ 28,  24,  13],
        [ 27,  23,  12]],

       [[  0,   8,   0],
        [  1,   9,   0],
        [  8,  16,   3],
        ...,
        [ 30,  26,  15],
        [ 19,  15,   4],
        [ 13,   9,   0]],

       [[  4,  12,   1],
        [  3,  11,   0],
        [  6,  14,   1],
        ...,
        [ 36,  32,  21],
        [ 27,  23,  12],
        [ 22,  18,   7]]], dtype=uint8)

This is (4032, 3024, 3)

The same picture was grayscaled like this.

array([[0.67058825, 0.6901961 , 0.69411767, ..., 0.61960787, 0.6       ,
        0.60784316],
       [0.6862745 , 0.6901961 , 0.6862745 , ..., 0.64705884, 0.63529414,
        0.6117647 ],
       [0.6901961 , 0.6862745 , 0.68235296, ..., 0.6117647 , 0.6156863 ,
        0.60784316],
       ...,
       [0.03529412, 0.03529412, 0.04705882, ..., 0.10196079, 0.09019608,
        0.08627451],
       [0.01568628, 0.01960784, 0.04705882, ..., 0.09803922, 0.05490196,
        0.03529412],
       [0.03137255, 0.02745098, 0.03921569, ..., 0.12156863, 0.08627451,
        0.06666667]], dtype=float32)

This is (4032, 3024)

I want to represent color and grayscale in a single array like (4032, 3024, 4) How to do? Or is there a way to express color and garyscale at once?

6
  • np.concatenate(arr_1, arr_2.reshape(4032, 3024, 1), axis=2). Though I don't know what this would represent conceptually, and I'm not sure anyone else would, either. Commented May 20, 2019 at 15:51
  • np.r_["2,3,0", RGB, gray] Commented May 20, 2019 at 15:54
  • Not sure what is your application, but you might also be interested in YUV, a three-channel color encoding system where one of the channels is directly the grayscale image (luminance). Commented May 20, 2019 at 15:59
  • @PMende it could represent RGBa with an alpha channel. Commented May 20, 2019 at 16:05
  • Surely the greyscale has just been calculated as a function of the RGB values, therefore it does not add any new information to the image - it is superfluous or redundant, I believe. Commented May 20, 2019 at 16:05

1 Answer 1

1

If I understand correctly, numpy.dstack is precisely what you want.

This is equivalent to concatenation along the third axis
...
This function makes most sense for arrays with up to 3 dimensions. For instance, for pixel-data with a height (first axis), width (second axis), and r/g/b channels (third axis).

image = np.arange(12).reshape([2, 2, 3])
grey = np.arange(4).reshape([2, 2]) 
stacked  = np.dstack([image, grey])

stacked.shape
# (2, 2, 4)

stacked:

   array([[
      [ 0,  1,  2,  0],
      [ 3,  4,  5,  1]],
      [[ 6,  7,  8,  2],
      [ 9, 10, 11,  3]]])
Sign up to request clarification or add additional context in comments.

2 Comments

But, when I`m doing this the shape is (4032, 3024, 2).
@youngyoung using dstack with a first parameter shaped [4032, 3024, 3] and a second shaped [4032, 3024] will result in a shape of [4032, 3024, 4]

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.