python - Slicing an image array into sub array -


import numpy np import numpy numpy import cv2 pil import image numpy import array img = cv2.imread('image test.jpg') gray = cv2.cvtcolor(img.cv2.color_bgr2gray) myarray = array(gray) slice = myarray[:8,:8] print slice print myarray  k = cv2.waitkey(0) if k == 27:     cv2.destroyallwindows() 

this code slicing array of image 8*8 block.i trying convert array 8*8 matrix resulted 3*8 matrix while running above python code. information regarding helpful.

import numpy np import cv2 img = cv2.imread('image test.jpg') gray = cv2.cvtcolor(img.cv2.color_bgr2gray) data = np.asarray(gray) data = np.split(data, data.shape[0]/8) res = [] arr in data:     res.extend(np.split(arr,arr.shape[1]/8, axis = 1)     print res[0] k = cv2.waitkey(0) if k == 27:     cv2.destroyallwindows()     [[6 6 6 6 5 5 6 6]  [7 7 7 6 6 6 7 7]  [8 7 7 7 7 8 8 8]  [8 7 6 6 7 8 8 8]  [7 6 5 6 6 7 7 7]  [6 5 5 5 6 6 7 7]  [6 5 5 5 6 6 7 8]  [6 6 6 6 6 7 8 9]] 

this output repeated thrice.when print res[40] given "list index out of range" error displayed.

i guess array i.e., myarray of shape 3*n, n>=8. possible reason behavior.

demo:

>>> import numpy np >>> = np.ones((16,16)) >>> slice = a[:8,:8] >>> slice.shape (8, 8) >>> = np.ones((3,16)) >>> slice = a[:8,:8] >>> slice.shape (3, 8) 

code split data 8x8 sub-array:

>>> import numpy np >>> data = np.random.randint(0,10,size=(320,240)) # create random array of 320x240 >>> data = np.split(data, data.shape[0]/8)  # first split 8x240, 8x240,... sub-arrays, i.e., split rows of 8 first. >>> res = [] >>> arr in data: ...     res.extend(np.split(arr,arr.shape[1]/8, axis=1)) # each 8x240 sub-array split column-wise 8x8, 8x8,... arrays ...  >>> res[0] array([[5, 7, 3, 0, 2, 7, 5, 2],        [8, 1, 8, 6, 3, 8, 8, 5],        [7, 4, 6, 7, 9, 5, 1, 6],        [0, 2, 4, 3, 1, 2, 0, 3],        [4, 4, 8, 8, 5, 7, 4, 2],        [2, 0, 8, 2, 9, 8, 9, 3],        [6, 4, 0, 3, 3, 3, 5, 8],        [6, 2, 8, 5, 0, 5, 1, 3]])        .        .        . >>> res[1199] array([[8, 5, 5, 9, 1, 7, 5, 4],        [0, 1, 8, 0, 3, 8, 5, 9],        [2, 5, 3, 6, 7, 2, 8, 8],        [1, 1, 7, 0, 0, 4, 3, 1],        [5, 5, 8, 6, 6, 6, 5, 7],        [9, 4, 2, 2, 7, 2, 1, 1],        [6, 9, 5, 2, 5, 9, 3, 4],        [1, 8, 1, 9, 7, 6, 7, 0]]) 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -