Good day. Uploading an image using

skimage.io.imread

getting

numpy.ndarray

with dimension

(474, 713, 3).

This results in a 3-dimensional array whose values ​​are the intensities of the colors in each pixel. \ N How does this ndarray convert to

pandas.dataFrame (or ndarray (337962, 3)),

so that the matrix of pixels (474, 713) turned into indices

(from 0 to 337961),

and color

(1, 2, 3) became a column.

String - this is respectively the value of the intensity of each color in a particular pixel. Thank.

For example. There are (np.arange (27) .reshape (3,3,3)):

([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], [[ 9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]) 

Need to:

  0 1 2 0 0 9 18 1 1 10 19 2 2 11 20 3 3 12 21 4 4 13 22 5 5 14 23 6 6 15 24 7 7 16 25 8 8 17 26 
  • Can you give an example? For example, take np.arange(27).reshape(3,3,3) as the source matrix and show how you imagine the resulting DF for this input matrix ... - MaxU
  • I'll do it now. I can't figure out the formatting. - Vetos
  • Overwhelmed. Added an example to the question. - Vetos

1 answer 1

You can do this as follows:

 In [122]: A Out[122]: array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], [[ 9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]) In [123]: pd.DataFrame(A.reshape(-1,).reshape(A.shape[2], A.shape[0] * A.shape[1]).T) Out[123]: 0 1 2 0 0 9 18 1 1 10 19 2 2 11 20 3 3 12 21 4 4 13 22 5 5 14 23 6 6 15 24 7 7 16 25 8 8 17 26 

Step by Step:

 In [118]: A.reshape(-1,) Out[118]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]) In [119]: A.reshape(-1,).reshape(A.shape[2], A.shape[0] * A.shape[1]) Out[119]: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8], [ 9, 10, 11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24, 25, 26]]) In [120]: A.reshape(-1,).reshape(A.shape[2], A.shape[0] * A.shape[1]).T Out[120]: array([[ 0, 9, 18], [ 1, 10, 19], [ 2, 11, 20], [ 3, 12, 21], [ 4, 13, 22], [ 5, 14, 23], [ 6, 15, 24], [ 7, 16, 25], [ 8, 17, 26]]) 
  • Thank you, this is what you need. - Vetos